{"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":"# EDA :","metadata":{}},{"cell_type":"markdown","source":"### Goal:\n* **Understand the data the best we can**\n* **Develop a first modeling strategy**\n\n### Starting checklist (train.csv):\n* **Rows and columns**: (13174211, 20)\n* **Variable types**: 7 categorical features and 13 numerical\n* **NaN value analysis**: 3 Columns full of NaN (fullscreen, hq, music), some Column have NaN due to their dependency to a defined event type, page has a lot of NaN for unknown reasons at the moment\n\n### Starting checklist (train_labels.csv):\n* **Target Column**: \"correct\"\n* **Rows and columns**: (212022, 2) \n* **Variable types**: 2 categorical features\n* **NaN value analysis**: No NaN\n* **Target Visualisation:** 70.4% of positive rows\n\n\n### Column explaination (Most of it from original Explaination) :\n* **session_id**: The session identifier, each student can have multiples but can't share them **Categorical**\n* **id**: the event identifier **Categorical**\n* **elapsed_time** :how much time has passed (in milliseconds) between the start of the session and when the event was recorded **Numerical**\n* **event_name** : the name of the event type **Categorical**\n* **name** : the event name (e.g. identifies whether a notebook_click is is opening or closing the notebook) **Categorical**\n* **level** : what level of the game the event occurred in (0 to 22) **Numerical**\n* **page** : the page number of the event (only for notebook-related events) **Numerical**\n* **room_coor_x** : the coordinates of the click in reference to the in-game room (only for click events) **Numerical**\n* **room_coor_y** : the coordinates of the click in reference to the in-game room (only for click events) **Numerical**\n* **screen_coor_x** : the coordinates of the click in reference to the player’s screen (only for click events) **Numerical**\n* **screen_coor_y** : the coordinates of the click in reference to the player’s screen (only for click events) **Numerical**\n* **hover_duration** : how long (in milliseconds) the hover happened for (only for hover events) **Numerical**\n* **text** : the text the player sees during this event **Categorical/Text**\n* **fqid** : the fully qualified ID of the event **Categorical**\n* **room_fqid** : the fully qualified ID of the room the event took place in **Categorical**\n* **text_fqid** : the fully qualified ID of the room the text appeared in **Categorical**\n* **fullscreen** : whether the player is in fullscreen mode (all NaN) **Categorical**\n* **hq** : whether the game is in high-quality (all NaN) **Categorical**\n* **music** : whether the game music is on or off  (all NaN) **Categorical**\n* **level_group** : which group of levels - and group of questions - this row belongs to (0-4, 5-12, 13-22) **Categorical**\n* **correct** : Target column, whether or not the right answer was given **Classification**\n","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport xgboost as xgb\nsns.set()\npd.set_option('display.max_column', 100)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-02-18T20:46:41.787226Z","iopub.execute_input":"2023-02-18T20:46:41.787666Z","iopub.status.idle":"2023-02-18T20:46:43.102085Z","shell.execute_reply.started":"2023-02-18T20:46:41.787578Z","shell.execute_reply":"2023-02-18T20:46:43.101216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtypes={'session_id':'category', \n'elapsed_time':np.int32,\n    'event_name':'category',\n    'name':'category',\n    'level':np.uint8,\n    'page':'category',\n    'room_coor_x':np.float32,\n    'room_coor_y':np.float32,\n    'screen_coor_x':np.float32,\n    'screen_coor_y':np.float32,\n    'hover_duration':np.float32,\n     'text':'category',\n     'fqid':'category',\n     'room_fqid':'category',\n     'text_fqid':'category',\n     'fullscreen':'category',\n     'hq':'category',\n     'music':'category',\n     'level_group':'category'}","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:46:43.106780Z","iopub.execute_input":"2023-02-18T20:46:43.109347Z","iopub.status.idle":"2023-02-18T20:46:43.117006Z","shell.execute_reply.started":"2023-02-18T20:46:43.109300Z","shell.execute_reply":"2023-02-18T20:46:43.116196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#data_test = pd.read_csv(\"/kaggle/input/predict-student-performance-from-game-play/test.csv\")\ndata_train =  pd.read_csv(\"/kaggle/input/predict-student-performance-from-game-play/train.csv\")\ndata_train_label = pd.read_csv(\"/kaggle/input/predict-student-performance-from-game-play/train_labels.csv\")","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-02-18T20:46:43.121103Z","iopub.execute_input":"2023-02-18T20:46:43.123379Z","iopub.status.idle":"2023-02-18T20:47:47.142401Z","shell.execute_reply.started":"2023-02-18T20:46:43.123338Z","shell.execute_reply":"2023-02-18T20:47:47.141374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df =  data_train.copy()\ndf_labels =  data_train_label.copy()\n\ndf = df.sort_values(['session_id','elapsed_time'])","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-02-18T20:47:47.144535Z","iopub.execute_input":"2023-02-18T20:47:47.144945Z","iopub.status.idle":"2023-02-18T20:47:59.433860Z","shell.execute_reply.started":"2023-02-18T20:47:47.144916Z","shell.execute_reply":"2023-02-18T20:47:59.432720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:47:59.435108Z","iopub.execute_input":"2023-02-18T20:47:59.435437Z","iopub.status.idle":"2023-02-18T20:47:59.468127Z","shell.execute_reply.started":"2023-02-18T20:47:59.435409Z","shell.execute_reply":"2023-02-18T20:47:59.467172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(df.isna().sum()/df.shape[0]).sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:47:59.477334Z","iopub.execute_input":"2023-02-18T20:47:59.478320Z","iopub.status.idle":"2023-02-18T20:48:03.767267Z","shell.execute_reply.started":"2023-02-18T20:47:59.478284Z","shell.execute_reply":"2023-02-18T20:48:03.766277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The music, fullscreen and full quality columns seems to all contain NaN values, the 9 other Nan containing columns seem to be due to their dependecy to a type of event (ig: click only event for screen_coor_y)**\n\nTo check that we can look at an heatmap using the following code: **sns.heatmap(df.isna(), cbar=False)** but that tends to take to much RAM for Kaggle Notebooks","metadata":{}},{"cell_type":"code","source":"for col in df.select_dtypes('object'):\n    if col not in ['text','fqid', 'room_fqid','text_fqid']: print(f'{col :-<20}: {df[col].unique()}')","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:03.778339Z","iopub.execute_input":"2023-02-18T20:48:03.778772Z","iopub.status.idle":"2023-02-18T20:48:07.991143Z","shell.execute_reply.started":"2023-02-18T20:48:03.778741Z","shell.execute_reply":"2023-02-18T20:48:07.990077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for col in df.select_dtypes('object'):\n    if col not in ['text','fqid', 'room_fqid','text_fqid']:plt.title(col); df[col].value_counts().plot.pie(autopct='%1.1f%%'); plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:07.995455Z","iopub.execute_input":"2023-02-18T20:48:07.995798Z","iopub.status.idle":"2023-02-18T20:48:12.253031Z","shell.execute_reply.started":"2023-02-18T20:48:07.995767Z","shell.execute_reply":"2023-02-18T20:48:12.252253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['isclick'] = df['event_name'].astype('str').str[-5:] == 'click'\nplt.figure(figsize=(5,5))\nplt.title('Click event ratio')\n(df['isclick'].value_counts(normalize=True)*100).plot.pie(labels = [\"Click\", \"other\"], autopct='%1.1f%%')\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-02-18T20:48:12.254261Z","iopub.execute_input":"2023-02-18T20:48:12.254727Z","iopub.status.idle":"2023-02-18T20:48:18.136783Z","shell.execute_reply.started":"2023-02-18T20:48:12.254699Z","shell.execute_reply":"2023-02-18T20:48:18.135805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Lots of clicks, see [here](https://www.kaggle.com/code/cdeotte/game-room-click-eda) for click EDA by Chris Deotte**","metadata":{}},{"cell_type":"code","source":"sns.displot(df['level'])","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:18.138096Z","iopub.execute_input":"2023-02-18T20:48:18.138401Z","iopub.status.idle":"2023-02-18T20:48:29.980507Z","shell.execute_reply.started":"2023-02-18T20:48:18.138372Z","shell.execute_reply":"2023-02-18T20:48:29.979439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.session_id.value_counts().mean()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:29.981920Z","iopub.execute_input":"2023-02-18T20:48:29.982264Z","iopub.status.idle":"2023-02-18T20:48:30.133047Z","shell.execute_reply.started":"2023-02-18T20:48:29.982235Z","shell.execute_reply":"2023-02-18T20:48:30.131936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Each session as a mean of 1118 events**","metadata":{}},{"cell_type":"markdown","source":"## Let's now take a look at df_labels and change it a little bit:","metadata":{}},{"cell_type":"code","source":"df_labels.tail(10)","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:30.134689Z","iopub.execute_input":"2023-02-18T20:48:30.135107Z","iopub.status.idle":"2023-02-18T20:48:30.149848Z","shell.execute_reply.started":"2023-02-18T20:48:30.135072Z","shell.execute_reply":"2023-02-18T20:48:30.149050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(5,5))\nplt.title('Correct_rate')\n(df_labels['correct'].value_counts(normalize=True)*100).plot.pie(labels = [\"correct\", \"Not correct\"], autopct='%1.1f%%')","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:30.151410Z","iopub.execute_input":"2023-02-18T20:48:30.152024Z","iopub.status.idle":"2023-02-18T20:48:30.256743Z","shell.execute_reply.started":"2023-02-18T20:48:30.151965Z","shell.execute_reply":"2023-02-18T20:48:30.255894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_labels = df_labels.rename(columns={\"session_id\": \"session_id_q\"})\ndf_labels['session_id'] = df_labels['session_id_q'].astype(str).str[:17].astype(int)\ndf_labels['question'] = df_labels['session_id_q'].astype(str).str[19:].astype(int)","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:30.258201Z","iopub.execute_input":"2023-02-18T20:48:30.258788Z","iopub.status.idle":"2023-02-18T20:48:30.480832Z","shell.execute_reply.started":"2023-02-18T20:48:30.258755Z","shell.execute_reply":"2023-02-18T20:48:30.479818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_labels = df_labels.loc[:,[\"session_id\",\"question\",\"correct\"]]","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:30.482404Z","iopub.execute_input":"2023-02-18T20:48:30.483053Z","iopub.status.idle":"2023-02-18T20:48:30.493259Z","shell.execute_reply.started":"2023-02-18T20:48:30.483014Z","shell.execute_reply":"2023-02-18T20:48:30.492252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:30.494778Z","iopub.execute_input":"2023-02-18T20:48:30.495420Z","iopub.status.idle":"2023-02-18T20:48:30.505714Z","shell.execute_reply.started":"2023-02-18T20:48:30.495385Z","shell.execute_reply":"2023-02-18T20:48:30.504663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_labels[df_labels['session_id'] == 21100511290882536]","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:30.507054Z","iopub.execute_input":"2023-02-18T20:48:30.507739Z","iopub.status.idle":"2023-02-18T20:48:30.524768Z","shell.execute_reply.started":"2023-02-18T20:48:30.507703Z","shell.execute_reply":"2023-02-18T20:48:30.523763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Lets create a new df that informs us of each session information","metadata":{}},{"cell_type":"code","source":"df_sessions = pd.DataFrame(df.session_id.astype(int))\ndf_sessions =df_sessions.drop_duplicates().reset_index(drop=True)\n\ndf_sessions.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:30.526010Z","iopub.execute_input":"2023-02-18T20:48:30.526399Z","iopub.status.idle":"2023-02-18T20:48:30.836775Z","shell.execute_reply.started":"2023-02-18T20:48:30.526371Z","shell.execute_reply":"2023-02-18T20:48:30.835755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp=pd.DataFrame(df.session_id.value_counts()).rename_axis('session_id0').reset_index()\ntemp = temp.rename(columns={\"session_id0\": \"session_id\",\"session_id\": \"num_events\"})","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-02-18T20:48:30.837972Z","iopub.execute_input":"2023-02-18T20:48:30.838389Z","iopub.status.idle":"2023-02-18T20:48:30.988632Z","shell.execute_reply.started":"2023-02-18T20:48:30.838352Z","shell.execute_reply":"2023-02-18T20:48:30.987565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sessions = pd.merge(temp,df_sessions, on = ['session_id'])\ndf_sessions.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:30.990046Z","iopub.execute_input":"2023-02-18T20:48:30.990493Z","iopub.status.idle":"2023-02-18T20:48:31.017024Z","shell.execute_reply.started":"2023-02-18T20:48:30.990442Z","shell.execute_reply":"2023-02-18T20:48:31.016217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,5))\nsns.histplot(df_sessions.num_events, kde=True)\nplt.title('num_events histogram')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:31.018890Z","iopub.execute_input":"2023-02-18T20:48:31.019426Z","iopub.status.idle":"2023-02-18T20:48:32.575344Z","shell.execute_reply.started":"2023-02-18T20:48:31.019393Z","shell.execute_reply":"2023-02-18T20:48:32.574233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Above 2500 clicks can be considered an execption","metadata":{}},{"cell_type":"code","source":"df_sessions['lots_events'] = df_sessions.num_events > 2500\ndf_sessions['few_events'] = df_sessions.num_events < 700\nprint(f\"Few events count: {len(df_sessions[df_sessions.few_events])}/{len(df_sessions)}\")\nprint(f\"Lots of events count: {len(df_sessions[df_sessions.lots_events])}/{len(df_sessions)}\")","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:32.576499Z","iopub.execute_input":"2023-02-18T20:48:32.576790Z","iopub.status.idle":"2023-02-18T20:48:32.587599Z","shell.execute_reply.started":"2023-02-18T20:48:32.576764Z","shell.execute_reply":"2023-02-18T20:48:32.586688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp1 = pd.pivot_table(df_labels, values = 'correct', index=['session_id'], columns = 'question')\ndf_sessions = pd.merge(temp1,df_sessions, on = ['session_id'])\ndf_sessions = df_sessions.rename(columns={1 : \"q_1\",\n 2 : \"q_2\",\n 3 : \"q_3\",\n 4 : \"q_4\",\n 5 : \"q_5\",\n 6 : \"q_6\",\n 7 : \"q_7\",\n 8 : \"q_8\",\n 9 : \"q_9\",\n 10 : \"q_10\",\n 11 : \"q_11\",\n 12 : \"q_12\",\n 13 : \"q_13\",\n 14 : \"q_14\",\n 15 : \"q_15\",\n 16 : \"q_16\",\n 17 : \"q_17\",\n 18 : \"q_18\",})","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:32.589103Z","iopub.execute_input":"2023-02-18T20:48:32.590190Z","iopub.status.idle":"2023-02-18T20:48:32.712698Z","shell.execute_reply.started":"2023-02-18T20:48:32.590155Z","shell.execute_reply":"2023-02-18T20:48:32.711811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sessions['accuracy'] = (df_sessions.q_1 + df_sessions.q_2 + df_sessions.q_3 + df_sessions.q_4 + df_sessions.q_5+ df_sessions.q_6 + df_sessions.q_7 +df_sessions.q_8 + df_sessions.q_9+ df_sessions.q_10 + df_sessions.q_11 + df_sessions.q_12 + df_sessions.q_13+ df_sessions.q_14 + df_sessions.q_15 + df_sessions.q_16 + df_sessions.q_17+ df_sessions.q_18)/18","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2023-02-18T20:48:32.714080Z","iopub.execute_input":"2023-02-18T20:48:32.715090Z","iopub.status.idle":"2023-02-18T20:48:32.725792Z","shell.execute_reply.started":"2023-02-18T20:48:32.715055Z","shell.execute_reply":"2023-02-18T20:48:32.724737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(df_sessions.accuracy)\nplt.title('accuracy distribution for all sessions')\nplt.show()\nprint(f\"Average accuracy for all sessions : {df_sessions.accuracy.mean()} \")","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:32.727169Z","iopub.execute_input":"2023-02-18T20:48:32.727464Z","iopub.status.idle":"2023-02-18T20:48:33.035675Z","shell.execute_reply.started":"2023-02-18T20:48:32.727438Z","shell.execute_reply":"2023-02-18T20:48:33.033794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nsns.histplot(df_sessions.accuracy[df_sessions.lots_events == True])\nax.set_xlim(0,1)\nplt.title('accuracy distribution for sessions with lots of events')\nplt.show()\nprint(f\"Average accuracy for sessions with lots of events : {df_sessions.accuracy[df_sessions.lots_events == True].mean()} \")","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:33.040974Z","iopub.execute_input":"2023-02-18T20:48:33.041336Z","iopub.status.idle":"2023-02-18T20:48:33.269326Z","shell.execute_reply.started":"2023-02-18T20:48:33.041306Z","shell.execute_reply":"2023-02-18T20:48:33.268299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nsns.histplot(df_sessions.accuracy[df_sessions.few_events == True])\nax.set_xlim(0,1)\nplt.title('accuracy distribution for sessions with few events')\nplt.show()\nprint(f\"Average accuracy for sessions with few events : {df_sessions.accuracy[df_sessions.few_events == True].mean()} \")","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:33.270773Z","iopub.execute_input":"2023-02-18T20:48:33.271100Z","iopub.status.idle":"2023-02-18T20:48:33.490008Z","shell.execute_reply.started":"2023-02-18T20:48:33.271071Z","shell.execute_reply":"2023-02-18T20:48:33.488964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\ncols_to_drop = ['session_id', 'num_events', 'accuracy','few_events','lots_events']\ndata=df_sessions.drop(cols_to_drop, axis=1).sum() / df_sessions.shape[0]\nplt.bar(data.index, data.values)\nplt.title('accuracy per question')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:33.491355Z","iopub.execute_input":"2023-02-18T20:48:33.491718Z","iopub.status.idle":"2023-02-18T20:48:33.760000Z","shell.execute_reply.started":"2023-02-18T20:48:33.491689Z","shell.execute_reply":"2023-02-18T20:48:33.759046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\ncols_to_drop = ['session_id', 'num_events', 'accuracy','few_events','lots_events']\n\ndata=df_sessions[df_sessions.lots_events == True].drop(cols_to_drop, axis=1).sum() / df_sessions[df_sessions.lots_events == True].shape[0]\nplt.bar(data.index, data.values)\nplt.title('accuracy per question for sessions with lots of events')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:33.761379Z","iopub.execute_input":"2023-02-18T20:48:33.761710Z","iopub.status.idle":"2023-02-18T20:48:34.048961Z","shell.execute_reply.started":"2023-02-18T20:48:33.761681Z","shell.execute_reply":"2023-02-18T20:48:34.046542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,5))\ncols_to_drop = ['session_id', 'num_events', 'accuracy','few_events','lots_events']\n\ndata=df_sessions[df_sessions.few_events == True].drop(cols_to_drop, axis=1).sum() / df_sessions[df_sessions.few_events == True].shape[0]\nplt.bar(data.index, data.values)\nplt.title('accuracy per question for sessions with few events')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:34.050268Z","iopub.execute_input":"2023-02-18T20:48:34.050568Z","iopub.status.idle":"2023-02-18T20:48:34.329759Z","shell.execute_reply.started":"2023-02-18T20:48:34.050541Z","shell.execute_reply":"2023-02-18T20:48:34.328935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,5))\ncols_to_drop = ['session_id', 'num_events', 'accuracy','few_events','lots_events']\ndata1=df_sessions.drop(cols_to_drop, axis=1).sum() / df_sessions.shape[0]\ndata2=df_sessions[df_sessions.few_events == True].drop(cols_to_drop, axis=1).sum() / df_sessions[df_sessions.few_events == True].shape[0]\ndata3=df_sessions[df_sessions.lots_events == True].drop(cols_to_drop, axis=1).sum() / df_sessions[df_sessions.lots_events == True].shape[0]\nplt.bar(data1.index, data1.values, alpha = 0.5,color=['red'])\nplt.bar(data2.index, data1.values,alpha = 0.5,color = ['green'])\nplt.bar(data3.index, data1.values,alpha = 0.5, color = ['blue'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:34.331117Z","iopub.execute_input":"2023-02-18T20:48:34.331410Z","iopub.status.idle":"2023-02-18T20:48:34.704364Z","shell.execute_reply.started":"2023-02-18T20:48:34.331384Z","shell.execute_reply":"2023-02-18T20:48:34.703439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**We see a clear difference for sessions with a lot of clicks, we could check if specific questions are answered wrong**","metadata":{}},{"cell_type":"code","source":"grouped = df.groupby('session_id')\nlast_elapsed_time = grouped['session_id','event_name','fqid'].tail(1)\n\nresult = last_elapsed_time.reset_index().rename(columns={'elapsed_time': 'last_elapsed_time'})\nresult['has_finished'] = (result.event_name == 'checkpoint') & (result.fqid == 'chap4_finale_c')\ndf_sessions = df_sessions.merge(result[['session_id','has_finished']], on='session_id')\ndf_sessions.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:34.705773Z","iopub.execute_input":"2023-02-18T20:48:34.706378Z","iopub.status.idle":"2023-02-18T20:48:35.698305Z","shell.execute_reply.started":"2023-02-18T20:48:34.706345Z","shell.execute_reply":"2023-02-18T20:48:35.696600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Average accuracy for all sessions : {df_sessions.accuracy.mean()} \")\nprint(f\"Average accuracy for unfinished sessions : {df_sessions.accuracy[df_sessions.has_finished == False].mean()} \")","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:35.700150Z","iopub.execute_input":"2023-02-18T20:48:35.700708Z","iopub.status.idle":"2023-02-18T20:48:35.711957Z","shell.execute_reply.started":"2023-02-18T20:48:35.700649Z","shell.execute_reply":"2023-02-18T20:48:35.711197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grouped = df.groupby('session_id')\nlast_elapsed_time = grouped['session_id','elapsed_time'].tail(1)\nresult = last_elapsed_time.reset_index().rename(columns={'elapsed_time': 'last_elapsed_time'})\ndf_sessions = df_sessions.merge(result[['session_id','last_elapsed_time']], on='session_id')\ndf_sessions.last_elapsed_time = df_sessions.last_elapsed_time // 1000\n\ndf_sessions.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:35.713206Z","iopub.execute_input":"2023-02-18T20:48:35.713938Z","iopub.status.idle":"2023-02-18T20:48:36.434505Z","shell.execute_reply.started":"2023-02-18T20:48:35.713885Z","shell.execute_reply":"2023-02-18T20:48:36.433483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sessions.last_elapsed_time.max()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:36.435948Z","iopub.execute_input":"2023-02-18T20:48:36.436272Z","iopub.status.idle":"2023-02-18T20:48:36.442385Z","shell.execute_reply.started":"2023-02-18T20:48:36.436244Z","shell.execute_reply":"2023-02-18T20:48:36.441492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots()\nsns.histplot(df_sessions.last_elapsed_time)\nax.set_ylim(0,1000)\nax.set_xlim(0,df_sessions.last_elapsed_time.quantile(0.95))\nplt.title('last_elapsed_time histogram')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:48:36.443852Z","iopub.execute_input":"2023-02-18T20:48:36.444156Z","iopub.status.idle":"2023-02-18T20:49:00.655465Z","shell.execute_reply.started":"2023-02-18T20:48:36.444130Z","shell.execute_reply":"2023-02-18T20:49:00.654659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Average accuracy for all sessions : {df_sessions.accuracy.mean()} \")\ndf_sessions['long_session'] = df_sessions.last_elapsed_time >= df_sessions.last_elapsed_time.quantile(0.90)\ndf_sessions['short_session'] = df_sessions.last_elapsed_time <= df_sessions.last_elapsed_time.quantile(0.10) \nprint(f\"Average accuracy for sessions with long last elapsed : {df_sessions.accuracy[df_sessions.long_session == True].mean()} \")\nprint(f\"Average accuracy for sessions with short last elapsed : {df_sessions.accuracy[df_sessions.short_session == True].mean()} \")","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:49:00.656510Z","iopub.execute_input":"2023-02-18T20:49:00.657404Z","iopub.status.idle":"2023-02-18T20:49:00.669019Z","shell.execute_reply.started":"2023-02-18T20:49:00.657371Z","shell.execute_reply":"2023-02-18T20:49:00.668166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sessions.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:49:00.670112Z","iopub.execute_input":"2023-02-18T20:49:00.671106Z","iopub.status.idle":"2023-02-18T20:49:00.694908Z","shell.execute_reply.started":"2023-02-18T20:49:00.671073Z","shell.execute_reply":"2023-02-18T20:49:00.693839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sessions.to_csv(\"/kaggle/working/df_sessions.csv\")\ndf_labels.to_csv(\"/kaggle/working/df_labels_updated.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-02-18T20:49:00.696180Z","iopub.execute_input":"2023-02-18T20:49:00.696747Z","iopub.status.idle":"2023-02-18T20:49:01.168064Z","shell.execute_reply.started":"2023-02-18T20:49:00.696704Z","shell.execute_reply":"2023-02-18T20:49:01.167111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"# To be continued..","metadata":{}}]}