{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# <div style='background:#8db8f7;border-radius:42px 42px;padding:40px;text-align: center;font-size: 32px;color:#FFFFFF;'>Predict Student Performance from Game Play</div>\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport missingno as msno","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:35:31.576974Z","iopub.execute_input":"2023-02-22T14:35:31.577502Z","iopub.status.idle":"2023-02-22T14:35:32.591666Z","shell.execute_reply.started":"2023-02-22T14:35:31.577396Z","shell.execute_reply":"2023-02-22T14:35:32.589921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span style=\"color:#8db8f7;\">Useful functions </span>","metadata":{}},{"cell_type":"markdown","source":"#### Reducing the amount of RAM consumption","metadata":{}},{"cell_type":"code","source":"# Yep it's here\ndef reduce_mem_usage(df):\n    start_mem = df.memory_usage().sum() / 1024**2\n    print(f'Memory usage of dataframe is {start_mem:.2f} MB')\n    for col in df.columns:\n        if df[col].dtype == 'object':\n            df[col] = df[col].astype('category')\n        elif df[col].dtype == 'int':\n            int_types = [np.int8, np.int16, np.int32, np.int64]\n            for int_type in int_types:\n                if df[col].min() >= np.iinfo(int_type).min and df[col].max() <= np.iinfo(int_type).max:\n                    df[col] = df[col].astype(int_type)\n                    break\n        elif df[col].dtype == 'float':\n            float_types = [np.float16, np.float32, np.float64]\n            for float_type in float_types:\n                if df[col].min() >= np.finfo(float_type).min and df[col].max() <= np.finfo(float_type).max:\n                    df[col] = df[col].astype(float_type)\n                    break\n    mem_usage = df.memory_usage().sum() / 1024**2 \n    print(f\"Memory usage became: {mem_usage:.2f} MB\")\n    return df","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:35:32.594921Z","iopub.execute_input":"2023-02-22T14:35:32.595343Z","iopub.status.idle":"2023-02-22T14:35:32.608383Z","shell.execute_reply.started":"2023-02-22T14:35:32.595309Z","shell.execute_reply":"2023-02-22T14:35:32.606821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span style=\"color:#8db8f7;\">Goal of the Competition</span>","metadata":{}},{"cell_type":"markdown","source":"**The goal of this competition is to predict student performance during game-based learning in real-time. You'll develop a model trained on one of the largest open datasets of game logs.**\n<br>\n<br>\n**Your work will help advance research into knowledge-tracing methods for game-based learning. You'll be supporting developers of educational games to create more effective learning experiences for students.**","metadata":{}},{"cell_type":"markdown","source":"# <span style=\"color:#8db8f7;\">F-score\n</span>","metadata":{}},{"cell_type":"markdown","source":"Submissions are evaluated using the **F1** score (F1):\n\n**$F_1 = 2\\frac{Precision \\cdot Recall}{Precision+Recall}$**\n\nwhere:\n\n**$Precision = \\frac{TP}{TP+FP}$**\n\n**$Recall = \\frac{TP}{TP+FN}$**\n\n","metadata":{}},{"cell_type":"markdown","source":"# <span style=\"color:#8db8f7;\">Data structure</span>","metadata":{}},{"cell_type":"markdown","source":"<ul>\n<li>train.csv - the training set</li>\n<li>test.csv - the test set</li>\n<li>sample_submission.csv - a sample submission file in the correct format</li>\n<li>train_labels.csv - correct value for all 18 questions for each session in the training set</li>\n</ul>","metadata":{}},{"cell_type":"markdown","source":"#### train.csv ","metadata":{}},{"cell_type":"markdown","source":"  - session_id - the ID of the session the event took place in\n  - index - the index of the event for the session\n  - elapsed_time - how much time has passed (in milliseconds) between the start of the session and when the event was recorded\n  - event_name - the name of the event type\n  - name - the event name (e.g. identifies whether a notebook_click is is opening or closing the notebook)\n  - level - what level of the game the event occurred in (0 to 22)\n  - page - the page number of the event (only for notebook-related events)\n  - room_coor_x - the coordinates of the click in reference to the in-game room (only for click events)\n  - room_coor_y - the coordinates of the click in reference to the in-game room (only for click events)\n  - screen_coor_x - the coordinates of the click in reference to the player’s screen (only for click events)\n  - screen_coor_y - the coordinates of the click in reference to the player’s screen (only for click events)\n  - hover_duration - how long (in milliseconds) the hover happened for (only for hover events)\n  - text - the text the player sees during this event\n  - fqid - the fully qualified ID of the event\n  - room_fqid - the fully qualified ID of the room the event took place in\n  - text_fqid - the fully qualified ID of the text\n  - fullscreen - whether the player is in fullscreen mode\n  - hq - whether the game is in high-quality\n  - music - whether the game music is on or off\n  - level_group - which group of levels - and group of questions - this row belongs to (0-4, 5-12, 13-22)","metadata":{}},{"cell_type":"code","source":"train = reduce_mem_usage(pd.read_csv(\"/kaggle/input/predict-student-performance-from-game-play/train.csv\"))\ntrain.info()","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:35:32.610389Z","iopub.execute_input":"2023-02-22T14:35:32.611741Z","iopub.status.idle":"2023-02-22T14:37:11.478078Z","shell.execute_reply.started":"2023-02-22T14:35:32.611662Z","shell.execute_reply":"2023-02-22T14:37:11.476534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:37:11.480999Z","iopub.execute_input":"2023-02-22T14:37:11.481437Z","iopub.status.idle":"2023-02-22T14:37:11.492184Z","shell.execute_reply.started":"2023-02-22T14:37:11.481397Z","shell.execute_reply":"2023-02-22T14:37:11.490774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:37:11.494454Z","iopub.execute_input":"2023-02-22T14:37:11.495048Z","iopub.status.idle":"2023-02-22T14:37:11.542039Z","shell.execute_reply.started":"2023-02-22T14:37:11.495009Z","shell.execute_reply":"2023-02-22T14:37:11.540067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##### Missing values","metadata":{}},{"cell_type":"code","source":"msno.matrix(train[0:60000], figsize = (20,10))","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:37:11.544026Z","iopub.execute_input":"2023-02-22T14:37:11.544598Z","iopub.status.idle":"2023-02-22T14:37:12.286142Z","shell.execute_reply.started":"2023-02-22T14:37:11.544561Z","shell.execute_reply":"2023-02-22T14:37:12.285001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2023-02-22T14:37:12.287355Z","iopub.execute_input":"2023-02-22T14:37:12.287769Z","iopub.status.idle":"2023-02-22T14:37:12.293862Z","shell.execute_reply.started":"2023-02-22T14:37:12.287733Z","shell.execute_reply":"2023-02-22T14:37:12.292423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### test.csv ","metadata":{}},{"cell_type":"code","source":"test = reduce_mem_usage(pd.read_csv(\"/kaggle/input/predict-student-performance-from-game-play/test.csv\"))\ntest.info()","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:37:12.296240Z","iopub.execute_input":"2023-02-22T14:37:12.296835Z","iopub.status.idle":"2023-02-22T14:37:12.383270Z","shell.execute_reply.started":"2023-02-22T14:37:12.296797Z","shell.execute_reply":"2023-02-22T14:37:12.381768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:37:12.385047Z","iopub.execute_input":"2023-02-22T14:37:12.386535Z","iopub.status.idle":"2023-02-22T14:37:12.394139Z","shell.execute_reply.started":"2023-02-22T14:37:12.386473Z","shell.execute_reply":"2023-02-22T14:37:12.393008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:37:12.398827Z","iopub.execute_input":"2023-02-22T14:37:12.400011Z","iopub.status.idle":"2023-02-22T14:37:12.438460Z","shell.execute_reply.started":"2023-02-22T14:37:12.399963Z","shell.execute_reply":"2023-02-22T14:37:12.437200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"msno.matrix(test[0:60000], figsize = (20,10))","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:37:12.440042Z","iopub.execute_input":"2023-02-22T14:37:12.440674Z","iopub.status.idle":"2023-02-22T14:37:12.875019Z","shell.execute_reply.started":"2023-02-22T14:37:12.440636Z","shell.execute_reply":"2023-02-22T14:37:12.873834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del test","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2023-02-22T14:37:12.876667Z","iopub.execute_input":"2023-02-22T14:37:12.877847Z","iopub.status.idle":"2023-02-22T14:37:12.883374Z","shell.execute_reply.started":"2023-02-22T14:37:12.877798Z","shell.execute_reply":"2023-02-22T14:37:12.882180Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### submission file","metadata":{}},{"cell_type":"code","source":"pd.read_csv(\"/kaggle/input/predict-student-performance-from-game-play/sample_submission.csv\").head()","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:37:12.885479Z","iopub.execute_input":"2023-02-22T14:37:12.886752Z","iopub.status.idle":"2023-02-22T14:37:12.913250Z","shell.execute_reply.started":"2023-02-22T14:37:12.886667Z","shell.execute_reply":"2023-02-22T14:37:12.912011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span style=\"color:#8db8f7;\">Number of unique elements</span>","metadata":{}},{"cell_type":"code","source":"train = reduce_mem_usage(pd.read_csv(\"/kaggle/input/predict-student-performance-from-game-play/train.csv\"))","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:37:12.914834Z","iopub.execute_input":"2023-02-22T14:37:12.915363Z","iopub.status.idle":"2023-02-22T14:38:16.727219Z","shell.execute_reply.started":"2023-02-22T14:37:12.915326Z","shell.execute_reply":"2023-02-22T14:38:16.725585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_unique = {}\npdn_unique = {}\nfor col in list(train.columns):\n    ls = []\n    ls.append(train[col].nunique())\n    pdn_unique[col] = ls \n    n_unique[col] = train[col].nunique()","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:38:16.729793Z","iopub.execute_input":"2023-02-22T14:38:16.730476Z","iopub.status.idle":"2023-02-22T14:38:23.998490Z","shell.execute_reply.started":"2023-02-22T14:38:16.730422Z","shell.execute_reply":"2023-02-22T14:38:23.996339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(pdn_unique)","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:38:24.001591Z","iopub.execute_input":"2023-02-22T14:38:24.004015Z","iopub.status.idle":"2023-02-22T14:38:24.033232Z","shell.execute_reply.started":"2023-02-22T14:38:24.003934Z","shell.execute_reply":"2023-02-22T14:38:24.031874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfig, ax = plt.subplots(figsize=(16,8))\nax.set_title(\"Number of unique elements\")\nplt.xticks(rotation=60)\nax.bar(n_unique.keys(), list(n_unique.values()))\nplt.show()\n\ndel fig\ndel ax","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:39:01.342522Z","iopub.execute_input":"2023-02-22T14:39:01.344623Z","iopub.status.idle":"2023-02-22T14:39:01.690584Z","shell.execute_reply.started":"2023-02-22T14:39:01.344553Z","shell.execute_reply":"2023-02-22T14:39:01.689473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span style=\"color:#8db8f7;\">Count of elements by each level group</span>","metadata":{}},{"cell_type":"markdown","source":"#### 0-4","metadata":{}},{"cell_type":"code","source":"train.groupby(['session_id', 'level_group']).count().reset_index().query(\"level_group == '0-4'\").elapsed_time.describe()","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:42:19.055559Z","iopub.execute_input":"2023-02-22T14:42:19.056055Z","iopub.status.idle":"2023-02-22T14:42:21.700922Z","shell.execute_reply.started":"2023-02-22T14:42:19.056020Z","shell.execute_reply":"2023-02-22T14:42:21.699811Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 5-12","metadata":{}},{"cell_type":"code","source":"train.groupby(['session_id', 'level_group']).count().reset_index().query(\"level_group == '5-12'\").elapsed_time.describe()","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:43:01.100673Z","iopub.execute_input":"2023-02-22T14:43:01.101136Z","iopub.status.idle":"2023-02-22T14:43:03.588000Z","shell.execute_reply.started":"2023-02-22T14:43:01.101092Z","shell.execute_reply":"2023-02-22T14:43:03.586549Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### 13-22","metadata":{}},{"cell_type":"code","source":"train.groupby(['session_id', 'level_group']).count().reset_index().query(\"level_group == '13-22'\").elapsed_time.describe()","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:44:45.218757Z","iopub.execute_input":"2023-02-22T14:44:45.219209Z","iopub.status.idle":"2023-02-22T14:44:47.703837Z","shell.execute_reply.started":"2023-02-22T14:44:45.219175Z","shell.execute_reply":"2023-02-22T14:44:47.702823Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span style=\"color:#8db8f7;\">Distributions distributions distributions...</span>","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:44:54.114208Z","iopub.execute_input":"2023-02-22T14:44:54.114635Z","iopub.status.idle":"2023-02-22T14:44:54.119518Z","shell.execute_reply.started":"2023-02-22T14:44:54.114603Z","shell.execute_reply":"2023-02-22T14:44:54.118596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nfig, ax = plt.subplots(figsize=(16,8))\nax.set_title(\"Count event_name\")\nsns.countplot(train[\"event_name\"], ax =ax)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:44:54.359065Z","iopub.execute_input":"2023-02-22T14:44:54.359507Z","iopub.status.idle":"2023-02-22T14:44:55.162200Z","shell.execute_reply.started":"2023-02-22T14:44:54.359474Z","shell.execute_reply":"2023-02-22T14:44:55.161040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(16,8))\nax.set_title(\"Distribution event name\")\nsns.histplot(data =train, x=\"elapsed_time\",  ax = ax, stat=\"probability\")\nplt.show()\n\ndel fig\ndel ax","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:46:15.306519Z","iopub.execute_input":"2023-02-22T14:46:15.307013Z","iopub.status.idle":"2023-02-22T14:51:16.488793Z","shell.execute_reply.started":"2023-02-22T14:46:15.306975Z","shell.execute_reply":"2023-02-22T14:51:16.487491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(16,8))\nax.set_title(\"Count event name\")\nsns.countplot(train[\"name\"], ax = ax)\nplt.show()\n\ndel fig\ndel ax","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:51:16.491448Z","iopub.execute_input":"2023-02-22T14:51:16.492251Z","iopub.status.idle":"2023-02-22T14:51:17.266085Z","shell.execute_reply.started":"2023-02-22T14:51:16.492212Z","shell.execute_reply":"2023-02-22T14:51:17.265024Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(16,8))\nax.set_title(\"Count level\")\nsns.countplot(train[\"level\"], ax = ax)\nplt.show()\n\ndel fig\ndel ax","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:51:17.267272Z","iopub.execute_input":"2023-02-22T14:51:17.268454Z","iopub.status.idle":"2023-02-22T14:51:19.092810Z","shell.execute_reply.started":"2023-02-22T14:51:17.268416Z","shell.execute_reply":"2023-02-22T14:51:19.091651Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(16,8))\nax.set_title(\"Count page\")\nsns.countplot(train[\"page\"], ax = ax)\nplt.show()\n\ndel fig\ndel ax","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:51:19.094903Z","iopub.execute_input":"2023-02-22T14:51:19.095730Z","iopub.status.idle":"2023-02-22T14:51:21.162137Z","shell.execute_reply.started":"2023-02-22T14:51:19.095661Z","shell.execute_reply":"2023-02-22T14:51:21.160311Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(16,8))\nax.set_title(\"Count level_group\")\nsns.countplot(train[\"level_group\"], ax = ax)\nplt.show()\n\ndel fig\ndel ax","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:51:21.163748Z","iopub.execute_input":"2023-02-22T14:51:21.164107Z","iopub.status.idle":"2023-02-22T14:51:21.853547Z","shell.execute_reply.started":"2023-02-22T14:51:21.164076Z","shell.execute_reply":"2023-02-22T14:51:21.852186Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(16,8))\nax.set_title(\"Count level_group\")\nsns.countplot(train[\"level_group\"], ax = ax)\nplt.show()\n\ndel fig\ndel ax","metadata":{"execution":{"iopub.status.busy":"2023-02-22T14:51:21.855177Z","iopub.execute_input":"2023-02-22T14:51:21.855658Z","iopub.status.idle":"2023-02-22T14:51:22.536764Z","shell.execute_reply.started":"2023-02-22T14:51:21.855625Z","shell.execute_reply":"2023-02-22T14:51:22.535680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# <span style=\"color:#8db8f7;\">To be continued....\n</span>","metadata":{}}]}