{"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":"code","source":"\n\nimport numpy as np \nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-04T20:57:41.217791Z","iopub.execute_input":"2023-03-04T20:57:41.218321Z","iopub.status.idle":"2023-03-04T20:57:41.232482Z","shell.execute_reply.started":"2023-03-04T20:57:41.218236Z","shell.execute_reply":"2023-03-04T20:57:41.231489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading data","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train.csv')\ntrain_labels = pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train_labels.csv')\n","metadata":{"execution":{"iopub.status.busy":"2023-03-04T20:57:41.234614Z","iopub.execute_input":"2023-03-04T20:57:41.235895Z","iopub.status.idle":"2023-03-04T20:58:48.174332Z","shell.execute_reply.started":"2023-03-04T20:57:41.235838Z","shell.execute_reply":"2023-03-04T20:58:48.173267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(3)","metadata":{"execution":{"iopub.status.busy":"2023-03-04T20:58:48.376314Z","iopub.execute_input":"2023-03-04T20:58:48.376876Z","iopub.status.idle":"2023-03-04T20:58:48.397763Z","shell.execute_reply.started":"2023-03-04T20:58:48.376840Z","shell.execute_reply":"2023-03-04T20:58:48.396320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# First look ","metadata":{}},{"cell_type":"code","source":"# group the dataset by session length and by the number of events in them\ntrain_df_grouped_by_sessions = train_df.groupby('session_id').agg({'elapsed_time': ['min', 'max'], 'event_name': 'count'})\n# get rid of the multi-index in columns\ntrain_df_grouped_by_sessions.columns = ['_'.join(x) for x in train_df_grouped_by_sessions.columns]\ntrain_df_grouped_by_sessions.reset_index(inplace = True)\n# convert to minutes\ntrain_df_grouped_by_sessions['elapsed_time_max'] = train_df_grouped_by_sessions['elapsed_time_max']/1000/60\ntrain_df_grouped_by_sessions.head(3)","metadata":{"execution":{"iopub.status.busy":"2023-03-04T20:58:48.399459Z","iopub.execute_input":"2023-03-04T20:58:48.400520Z","iopub.status.idle":"2023-03-04T20:58:49.513053Z","shell.execute_reply.started":"2023-03-04T20:58:48.400464Z","shell.execute_reply":"2023-03-04T20:58:49.512158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (15, 5))\nfig.suptitle('Session Length Distribution', fontsize = 20)\n\nax1.hist(train_df_grouped_by_sessions['elapsed_time_max'], bins = 50)\nax2.boxplot(train_df_grouped_by_sessions['elapsed_time_max'], patch_artist = True, vert = 0)\nax1.set(xlabel='Session duration', ylabel='Number of sessions')\nax2.set(xlabel='Session duration')\nax2.set_yticklabels([''])\n\nax1.grid()\nax2.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-04T20:58:49.514180Z","iopub.execute_input":"2023-03-04T20:58:49.514888Z","iopub.status.idle":"2023-03-04T20:58:49.929322Z","shell.execute_reply.started":"2023-03-04T20:58:49.514851Z","shell.execute_reply":"2023-03-04T20:58:49.928016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"__It can be seen that there are very long sessions, most likely these are outliers.__","metadata":{}},{"cell_type":"markdown","source":"I will look at the distribution of the number of events in the session","metadata":{}},{"cell_type":"code","source":"fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (15, 5))\nfig.suptitle('Events Distribution', fontsize = 20)\n\nax1.hist(train_df_grouped_by_sessions['event_name_count'], bins = 50)\nax2.boxplot(train_df_grouped_by_sessions['event_name_count'], patch_artist = True, vert = 0)\nax1.set(xlabel='Count events', ylabel='Count of sessions')\nax2.set(xlabel='Count events')\nax2.set_yticklabels([''])\n\nax1.grid()\nax2.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-04T20:58:49.930893Z","iopub.execute_input":"2023-03-04T20:58:49.933532Z","iopub.status.idle":"2023-03-04T20:58:50.425350Z","shell.execute_reply.started":"2023-03-04T20:58:49.933466Z","shell.execute_reply":"2023-03-04T20:58:50.424356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"__This picture seems prettier, I'll see how the number of events depends on the duration of the session__","metadata":{}},{"cell_type":"code","source":"plt.scatter(x = train_df_grouped_by_sessions['elapsed_time_max'], y = train_df_grouped_by_sessions['event_name_count'], alpha = 0.5)\n# plt.title('Dependence of the number of events on the duration of the session')\n# set(xlabel='Session duration', ylabel='Count events')\nplt.gca().update(dict(title='Dependence of the number of events on \\n the duration of the session'\n                      , xlabel='Session duration', ylabel='Count events'))\nplt.grid()","metadata":{"execution":{"iopub.status.busy":"2023-03-04T20:58:50.426831Z","iopub.execute_input":"2023-03-04T20:58:50.427523Z","iopub.status.idle":"2023-03-04T20:58:50.720378Z","shell.execute_reply.started":"2023-03-04T20:58:50.427484Z","shell.execute_reply":"2023-03-04T20:58:50.719137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.columns","metadata":{"execution":{"iopub.status.busy":"2023-03-04T20:58:50.723087Z","iopub.execute_input":"2023-03-04T20:58:50.723690Z","iopub.status.idle":"2023-03-04T20:58:50.729652Z","shell.execute_reply.started":"2023-03-04T20:58:50.723637Z","shell.execute_reply":"2023-03-04T20:58:50.728874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"question_number = train_labels['session_id'].str.split('_', expand = True).rename(columns = {0: \"session_id\", 1: 'question_number'})\nquestion_number = pd.concat([question_number, train_labels[['correct']]], axis = 1)\nquestion_number.head(3)","metadata":{"execution":{"iopub.status.busy":"2023-03-04T21:29:17.323628Z","iopub.execute_input":"2023-03-04T21:29:17.324040Z","iopub.status.idle":"2023-03-04T21:29:18.268510Z","shell.execute_reply.started":"2023-03-04T21:29:17.324004Z","shell.execute_reply":"2023-03-04T21:29:18.267335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"question_number_group = question_number.groupby(['question_number', 'correct'], as_index = False).agg({'session_id': pd.Series.nunique})\nquestion_number_group['id'] = question_number_group['question_number'].str.replace('q', '').astype('int')\nquestion_number_group = question_number_group.sort_values(by = ['id', 'correct']).reset_index(drop = True)","metadata":{"execution":{"iopub.status.busy":"2023-03-04T21:43:28.252317Z","iopub.execute_input":"2023-03-04T21:43:28.252801Z","iopub.status.idle":"2023-03-04T21:43:28.400150Z","shell.execute_reply.started":"2023-03-04T21:43:28.252763Z","shell.execute_reply":"2023-03-04T21:43:28.398996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize = (10, 5))\nquestion_number_group.groupby(['correct']).plot(x = 'id', y = 'session_id', ax =ax  )\nax.legend([\"Incorrect answer\",\"Correct answer\"])\nplt.xticks(range(1, question_number_group['id'].max()+1))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-04T21:59:24.133312Z","iopub.execute_input":"2023-03-04T21:59:24.133860Z","iopub.status.idle":"2023-03-04T21:59:24.392367Z","shell.execute_reply.started":"2023-03-04T21:59:24.133816Z","shell.execute_reply":"2023-03-04T21:59:24.391047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"__The most difficult question is question number 13. For some reason, it has the fewest correct answers__","metadata":{}}]}