{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport pandas as pd, numpy as np, gc\nfrom sklearn.model_selection import KFold, GroupKFold\nfrom xgboost import XGBClassifier\nfrom sklearn.metrics import f1_score\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-24T16:22:07.635501Z","iopub.execute_input":"2023-05-24T16:22:07.635942Z","iopub.status.idle":"2023-05-24T16:22:09.845150Z","shell.execute_reply.started":"2023-05-24T16:22:07.635907Z","shell.execute_reply":"2023-05-24T16:22:09.844123Z"},"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': np.int8,\n          'hq': np.int8,\n          'music': np.int8,\n          'level_group': 'category'}\ndata=pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train.csv',dtype=dtypes)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:22:09.850988Z","iopub.execute_input":"2023-05-24T16:22:09.854253Z","iopub.status.idle":"2023-05-24T16:24:34.035768Z","shell.execute_reply.started":"2023-05-24T16:22:09.854208Z","shell.execute_reply":"2023-05-24T16:24:34.033982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.head(3)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:34.037420Z","iopub.execute_input":"2023-05-24T16:24:34.039572Z","iopub.status.idle":"2023-05-24T16:24:34.085987Z","shell.execute_reply.started":"2023-05-24T16:24:34.039528Z","shell.execute_reply":"2023-05-24T16:24:34.085023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:34.087264Z","iopub.execute_input":"2023-05-24T16:24:34.088303Z","iopub.status.idle":"2023-05-24T16:24:34.140495Z","shell.execute_reply.started":"2023-05-24T16:24:34.088267Z","shell.execute_reply":"2023-05-24T16:24:34.139506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels=pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train_labels.csv')\nlabels.head(3)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:34.144918Z","iopub.execute_input":"2023-05-24T16:24:34.145350Z","iopub.status.idle":"2023-05-24T16:24:34.617899Z","shell.execute_reply.started":"2023-05-24T16:24:34.145315Z","shell.execute_reply":"2023-05-24T16:24:34.616866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.info()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:34.619645Z","iopub.execute_input":"2023-05-24T16:24:34.620509Z","iopub.status.idle":"2023-05-24T16:24:34.777895Z","shell.execute_reply.started":"2023-05-24T16:24:34.620473Z","shell.execute_reply":"2023-05-24T16:24:34.776871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x='20090312431273200_q1'.split('_')[0]\nx","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:34.779801Z","iopub.execute_input":"2023-05-24T16:24:34.780603Z","iopub.status.idle":"2023-05-24T16:24:34.787555Z","shell.execute_reply.started":"2023-05-24T16:24:34.780570Z","shell.execute_reply":"2023-05-24T16:24:34.786534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['session_id_correct']=labels.session_id.apply(lambda x:x.split('_')[0])","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:34.788952Z","iopub.execute_input":"2023-05-24T16:24:34.789766Z","iopub.status.idle":"2023-05-24T16:24:35.042160Z","shell.execute_reply.started":"2023-05-24T16:24:34.789688Z","shell.execute_reply":"2023-05-24T16:24:35.040029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,8))\nsns.countplot(data=labels,x='correct')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:35.044028Z","iopub.execute_input":"2023-05-24T16:24:35.044501Z","iopub.status.idle":"2023-05-24T16:24:35.285769Z","shell.execute_reply.started":"2023-05-24T16:24:35.044456Z","shell.execute_reply":"2023-05-24T16:24:35.284888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### we have a highly imbalanced dataset","metadata":{}},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"markdown","source":"# Columns \n\n- Object_Hover - In each chapter of the game, some tasks have to be performed by the student, like clicking the slip on the t-shirt in the 1st Chapter of the game. This slip is an object. If the student takes the mouse pointer above this object, an object_hover is recorded.The duration for which the pointer stays above this object, is recorded in the hover_duration. If he/she clicks on the object, it's an object_click!\n\n- Map_Hover - In the map, there are many places a student can click to go there. When he/she takes the pointer above any place in the map, a map_hover event is recorded.Just like in object_hover, hover_duration is the duration for which the pointer stays above that place.And when the student clicks on any place, it's a map_click!\n\n","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:35.287026Z","iopub.execute_input":"2023-05-24T16:24:35.287522Z","iopub.status.idle":"2023-05-24T16:24:35.329248Z","shell.execute_reply.started":"2023-05-24T16:24:35.287493Z","shell.execute_reply":"2023-05-24T16:24:35.327989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check relation between event_name and level and elapsed time\n\ngrouped_event=data.groupby(['event_name'])['elapsed_time'].mean().reset_index()\ngrouped_event.sort_values(inplace=True,by='elapsed_time')","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:35.331246Z","iopub.execute_input":"2023-05-24T16:24:35.331662Z","iopub.status.idle":"2023-05-24T16:24:35.721453Z","shell.execute_reply.started":"2023-05-24T16:24:35.331630Z","shell.execute_reply":"2023-05-24T16:24:35.720024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,6))\nplt.barh(y=grouped_event['event_name'],width=grouped_event['elapsed_time'])\nplt.xlabel('Elapsed Time')\nplt.ylabel('Event name')\nplt.title('Elapsed Time by Event Name')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:35.723264Z","iopub.execute_input":"2023-05-24T16:24:35.723623Z","iopub.status.idle":"2023-05-24T16:24:35.997152Z","shell.execute_reply.started":"2023-05-24T16:24:35.723595Z","shell.execute_reply":"2023-05-24T16:24:35.996069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grouped_level=data.groupby(['level'])['elapsed_time'].mean().reset_index()\ngrouped_level.sort_values(inplace=True,by='elapsed_time')","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:35.998958Z","iopub.execute_input":"2023-05-24T16:24:35.999689Z","iopub.status.idle":"2023-05-24T16:24:36.764901Z","shell.execute_reply.started":"2023-05-24T16:24:35.999652Z","shell.execute_reply":"2023-05-24T16:24:36.763920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,6))\nplt.barh(y=grouped_level['level'],width=grouped_level['elapsed_time'])\nplt.xlabel('Elapsed Time')\nplt.ylabel('level')\nplt.title('Elapsed Time by level')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:36.770507Z","iopub.execute_input":"2023-05-24T16:24:36.771306Z","iopub.status.idle":"2023-05-24T16:24:37.021356Z","shell.execute_reply.started":"2023-05-24T16:24:36.771268Z","shell.execute_reply":"2023-05-24T16:24:37.020292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check the most occuring \n\ngroupe=data.groupby(['level','event_name']).agg({\"elapsed_time\":\"mean\",\"event_name\":\"count\"}).rename(columns={'event_name':'count'}).reset_index()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:37.022983Z","iopub.execute_input":"2023-05-24T16:24:37.023628Z","iopub.status.idle":"2023-05-24T16:24:38.923858Z","shell.execute_reply.started":"2023-05-24T16:24:37.023596Z","shell.execute_reply":"2023-05-24T16:24:38.922813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(16,8))\n\nsns.kdeplot(data=groupe,x='elapsed_time',hue='event_name')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:38.925676Z","iopub.execute_input":"2023-05-24T16:24:38.926476Z","iopub.status.idle":"2023-05-24T16:24:39.396047Z","shell.execute_reply.started":"2023-05-24T16:24:38.926426Z","shell.execute_reply":"2023-05-24T16:24:39.394887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"groupe","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:39.397170Z","iopub.execute_input":"2023-05-24T16:24:39.397509Z","iopub.status.idle":"2023-05-24T16:24:39.419602Z","shell.execute_reply.started":"2023-05-24T16:24:39.397472Z","shell.execute_reply":"2023-05-24T16:24:39.418142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"event_names=groupe.event_name.unique()\nfor event_name in event_names:\n    event_data = groupe[groupe['event_name'] == event_name]\n    plt.figure(figsize=(10, 5))\n    sns.barplot(x='level', y='elapsed_time', data=event_data, palette='viridis')\n    plt.axvline(12)\n    plt.xlabel('Level')\n    plt.ylabel('Mean Elapsed Time (ms)')\n    plt.title(f'Mean Elapsed Time by Level for {event_name}')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:39.421904Z","iopub.execute_input":"2023-05-24T16:24:39.422299Z","iopub.status.idle":"2023-05-24T16:24:43.891998Z","shell.execute_reply.started":"2023-05-24T16:24:39.422268Z","shell.execute_reply":"2023-05-24T16:24:43.890330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"event_names=groupe.event_name.unique()\nfor event_name in event_names:\n    event_data = groupe[groupe['event_name'] == event_name]\n    plt.figure(figsize=(10, 5))\n    sns.barplot(x='level', y='count', data=event_data, palette='viridis')\n    plt.axvline(12)\n    plt.xlabel('Level')\n    plt.ylabel('Count (ms)')\n    plt.title(f'how much {event_name} occured by level')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:43.893658Z","iopub.execute_input":"2023-05-24T16:24:43.894033Z","iopub.status.idle":"2023-05-24T16:24:48.165023Z","shell.execute_reply.started":"2023-05-24T16:24:43.894001Z","shell.execute_reply":"2023-05-24T16:24:48.164100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Resume \n- J'ai remarqué que elapsed_time increased when we pass the level 12 like : object_hover,notification_click, navigate_clik, map_hover, map_clik\n- notebook_clik its occured when the user click on the notebook some mainly he will do it if he need to resppnd to a question. So what it explains fact that for level 12 till 22 have an elapsed_time for notebook_click superior is that it easiest level don't nedd to check the notebook response are trivial.","metadata":{}},{"cell_type":"code","source":"most_occuring=groupe.groupby('level').apply(lambda x:x.loc[x['count'].idxmax()]).reset_index(drop=True)\nmost_occuring","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:48.166277Z","iopub.execute_input":"2023-05-24T16:24:48.166809Z","iopub.status.idle":"2023-05-24T16:24:48.204629Z","shell.execute_reply.started":"2023-05-24T16:24:48.166778Z","shell.execute_reply":"2023-05-24T16:24:48.203068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"groupe[groupe['level']==0].sort_values(by='count',ascending=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:48.206820Z","iopub.execute_input":"2023-05-24T16:24:48.207661Z","iopub.status.idle":"2023-05-24T16:24:48.232325Z","shell.execute_reply.started":"2023-05-24T16:24:48.207620Z","shell.execute_reply":"2023-05-24T16:24:48.230832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"groupe[groupe['level']==22].sort_values(by='count',ascending=False)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:48.234469Z","iopub.execute_input":"2023-05-24T16:24:48.234895Z","iopub.status.idle":"2023-05-24T16:24:48.259821Z","shell.execute_reply.started":"2023-05-24T16:24:48.234859Z","shell.execute_reply":"2023-05-24T16:24:48.258451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# how elapsed time evolue by level\n\ngroup_data=data.groupby('level')['elapsed_time'].mean().reset_index()\n\nfig,(ax1,ax2)=plt.subplots(nrows=2,ncols=1)\n\nsns.lineplot(data=group_data,x='level',y='elapsed_time',ax=ax1)\nax1.axvline(12)\n\nsns.kdeplot(data=group_data,x='elapsed_time',ax=ax2)\nax2.axvline(0.4*(10**7))\nplt.legend()\nfig.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:48.261721Z","iopub.execute_input":"2023-05-24T16:24:48.262176Z","iopub.status.idle":"2023-05-24T16:24:49.391777Z","shell.execute_reply.started":"2023-05-24T16:24:48.262141Z","shell.execute_reply":"2023-05-24T16:24:49.390772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"group_data[group_data['elapsed_time']<=0.4*(10**7)]","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:49.393298Z","iopub.execute_input":"2023-05-24T16:24:49.393889Z","iopub.status.idle":"2023-05-24T16:24:49.406617Z","shell.execute_reply.started":"2023-05-24T16:24:49.393855Z","shell.execute_reply":"2023-05-24T16:24:49.405580Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# there is any relation between time where the user is playing and the correctness ? \n","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:49.408087Z","iopub.execute_input":"2023-05-24T16:24:49.408662Z","iopub.status.idle":"2023-05-24T16:24:49.422207Z","shell.execute_reply.started":"2023-05-24T16:24:49.408628Z","shell.execute_reply":"2023-05-24T16:24:49.421021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['question']=labels['session_id'].astype(str).apply(lambda x:x.split('_')[1])","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:49.423766Z","iopub.execute_input":"2023-05-24T16:24:49.424355Z","iopub.status.idle":"2023-05-24T16:24:49.715197Z","shell.execute_reply.started":"2023-05-24T16:24:49.424321Z","shell.execute_reply":"2023-05-24T16:24:49.714191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['year'] = labels['session_id'].str.slice(start=0, stop=2).astype(np.int8)\nlabels['month'] = labels['session_id'].str.slice(start=2, stop=4).astype(np.int8)\n\nlabels['day'] = labels['session_id'].str.slice(start=4, stop=6).astype(np.int8)\n\nlabels['hour'] = labels[\"hour\"] = labels[\"session_id\"].str.slice(start=6, stop=8).astype(np.uint8)\n\nlabels = labels.set_index('session_id')\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:49.716720Z","iopub.execute_input":"2023-05-24T16:24:49.717263Z","iopub.status.idle":"2023-05-24T16:24:51.046369Z","shell.execute_reply.started":"2023-05-24T16:24:49.717232Z","shell.execute_reply":"2023-05-24T16:24:51.045356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"days ={0:\"Monday\",\n      1:\"Tuesday\",\n      2:\"Wednesday\",\n      3:\"Thursday\",\n      4:\"Friday\",\n      5:\"Saturday\",\n      6:\"Sunday\"}\n\nlabels['day_name']=labels.day.map(days)\n","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:51.047891Z","iopub.execute_input":"2023-05-24T16:24:51.048466Z","iopub.status.idle":"2023-05-24T16:24:51.071708Z","shell.execute_reply.started":"2023-05-24T16:24:51.048432Z","shell.execute_reply":"2023-05-24T16:24:51.070015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# check the accuracy of correctness by hour we can differentiate between day, afternoon, night \n\nhour_correcteness=labels.groupby('hour')[\"correct\"].mean().reset_index()\n\nplt.figure(figsize=(12,6))\nplt.axvline(6)\nsns.lineplot(data=hour_correcteness,x='hour',y=\"correct\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:51.073891Z","iopub.execute_input":"2023-05-24T16:24:51.074312Z","iopub.status.idle":"2023-05-24T16:24:51.328222Z","shell.execute_reply.started":"2023-05-24T16:24:51.074281Z","shell.execute_reply":"2023-05-24T16:24:51.326973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Resume \n- High performance at 6 am \n- very low one at 5 am which is normal -> no one plays at this time, maybe i should focus on days during week and week-end ","metadata":{}},{"cell_type":"code","source":"day_number= {\n    'Monday': 0,\n    'Tuesday': 1,\n    'Wednesday': 2,\n    'Thursday': 3,\n    'Friday': 4,\n    'Saturday': 5,\n    'Sunday': 6\n}\n\na=labels.groupby('day_name')[\"correct\"].mean().reset_index()\n\na['day_number']=a['day_name'].map(day_number)\n\na=a.sort_values('day_number',ascending=True)\n\nplt.figure(figsize=(12,8))\nsns.lineplot(data=a,x='day_name',y=\"correct\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:51.330046Z","iopub.execute_input":"2023-05-24T16:24:51.330763Z","iopub.status.idle":"2023-05-24T16:24:51.615735Z","shell.execute_reply.started":"2023-05-24T16:24:51.330713Z","shell.execute_reply":"2023-05-24T16:24:51.614670Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Resume \n- logic insight i think, high performance in monday and in week-end \n","metadata":{}},{"cell_type":"code","source":"daily_performance=labels.groupby(['day_name','hour'])['correct'].mean().reset_index()\ndaily_performance","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:51.617611Z","iopub.execute_input":"2023-05-24T16:24:51.618390Z","iopub.status.idle":"2023-05-24T16:24:51.697944Z","shell.execute_reply.started":"2023-05-24T16:24:51.618355Z","shell.execute_reply":"2023-05-24T16:24:51.696886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:51.699737Z","iopub.execute_input":"2023-05-24T16:24:51.700528Z","iopub.status.idle":"2023-05-24T16:24:51.713306Z","shell.execute_reply.started":"2023-05-24T16:24:51.700490Z","shell.execute_reply":"2023-05-24T16:24:51.711867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"daily_performance[daily_performance['day_name']==\"Friday\"]['correct'].mean()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:51.715226Z","iopub.execute_input":"2023-05-24T16:24:51.716044Z","iopub.status.idle":"2023-05-24T16:24:51.729180Z","shell.execute_reply.started":"2023-05-24T16:24:51.716004Z","shell.execute_reply":"2023-05-24T16:24:51.728094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"day_name=list(labels.day_name.unique())\n\nfor dn in day_name:\n    \n    df=daily_performance[daily_performance['day_name']==dn]\n    \n    sns.lineplot(data=df,x='hour',y='correct')\n    \n    plt.title(f\"performance in day {dn}\")\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:51.731103Z","iopub.execute_input":"2023-05-24T16:24:51.731871Z","iopub.status.idle":"2023-05-24T16:24:53.311236Z","shell.execute_reply.started":"2023-05-24T16:24:51.731833Z","shell.execute_reply":"2023-05-24T16:24:53.306813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"question_correctness=labels.groupby(['question'])['correct'].mean().reset_index()\nquestion_correctness=question_correctness.sort_values(by=\"correct\",ascending=False)\nplt.figure(figsize=(12,8))\nax=sns.barplot(data=question_correctness,x='correct',y='question')\nfor p in ax.patches:\n    ax.annotate(\n        '{:.2f}'.format(p.get_width()),\n        (p.get_width(), p.get_y() + p.get_height() / 2),\n        ha='left',\n        va='center',\n        xytext=(5, 0),\n        textcoords='offset points'\n    )\n\nplt.show()\n\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:53.313098Z","iopub.execute_input":"2023-05-24T16:24:53.313509Z","iopub.status.idle":"2023-05-24T16:24:53.761625Z","shell.execute_reply.started":"2023-05-24T16:24:53.313472Z","shell.execute_reply":"2023-05-24T16:24:53.760507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"question_correctness[question_correctness['question']=='q2']['correct']","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:53.762969Z","iopub.execute_input":"2023-05-24T16:24:53.764052Z","iopub.status.idle":"2023-05-24T16:24:53.774892Z","shell.execute_reply.started":"2023-05-24T16:24:53.764016Z","shell.execute_reply":"2023-05-24T16:24:53.773542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- WE can notice that questions are devided to easy and difficult, acctualy some question are realy hard to answer, but the user can achieve higher accuracy on these question if he play inmorning? ","metadata":{}},{"cell_type":"code","source":"# are the correctness of questions related somehow to days or hour for example maybe q13 have a better accuracy if user played in morning \nquestions_hour_correctenss=labels.groupby(['question','hour'])['correct'].mean().reset_index()\n\nquestions=questions_hour_correctenss.question.unique()\n\nfor q in questions:\n    \n    plt.figure(figsize=(12,8))\n    \n    d=questions_hour_correctenss[questions_hour_correctenss['question']==q]\n    \n    sns.lineplot(data=d,x='hour',y='correct')\n    \n    plt.axhline(0.5)\n    \n    plt.title(f\"accuracy of question {q} through the day, the mean for this question is {question_correctness[question_correctness['question']==q]['correct'].values[0]:.2f} with a std of {round(d['correct'].std(),2)}\")\n    \n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:53.776444Z","iopub.execute_input":"2023-05-24T16:24:53.776949Z","iopub.status.idle":"2023-05-24T16:24:59.036243Z","shell.execute_reply.started":"2023-05-24T16:24:53.776917Z","shell.execute_reply":"2023-05-24T16:24:59.035208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Resume \n- Questions that are cosidered like q13, his accuracy is independant from time but for q15 at some hours we can achieve higher accuracy 0.6\n- by looking to standard deviation, i think that hour of the day does not have a strong impact on the performance. ","metadata":{}},{"cell_type":"markdown","source":"# Performing statistical test\n\n- here i want to perform statistical test to see if there is an impact of days on performance ","metadata":{}},{"cell_type":"code","source":"import scipy.stats as stats\nfrom scipy.stats import chi2_contingency\n","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:59.037450Z","iopub.execute_input":"2023-05-24T16:24:59.038032Z","iopub.status.idle":"2023-05-24T16:24:59.045665Z","shell.execute_reply.started":"2023-05-24T16:24:59.038000Z","shell.execute_reply":"2023-05-24T16:24:59.044014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Verify if there is a signigicant difference in accuracy across hours\n\n- H0 : there is no significant difference in the accuracy distributions across diffrent hours\n- H1: there is a significant differenece in the accuracy distributions of hours in other words the median is not the same for all hours","metadata":{}},{"cell_type":"code","source":"contingency_table = labels.pivot_table(index='hour', columns='correct', aggfunc='size', fill_value=0)\n\nchi2_stat, p_value, dof, ex = chi2_contingency(contingency_table)\n\nprint(\"Chi2 Stat:\", chi2_stat)\nprint(\"P Value:\", p_value)\nprint(\"Degrees of Freedom:\", dof)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:59.049732Z","iopub.execute_input":"2023-05-24T16:24:59.050580Z","iopub.status.idle":"2023-05-24T16:24:59.102793Z","shell.execute_reply.started":"2023-05-24T16:24:59.050530Z","shell.execute_reply":"2023-05-24T16:24:59.101515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- we can reject the null hypothesis and accept H1","metadata":{}},{"cell_type":"code","source":"daiyl_accuracy = labels.pivot_table(index='day_name', columns='correct', aggfunc='size', fill_value=0)\n\nchi2_stat, p_value, dof, ex = chi2_contingency(daiyl_accuracy)\n\nprint(\"Chi2 Stat:\", chi2_stat)\nprint(\"P Value:\", p_value)\nprint(\"Degrees of Freedom:\", dof)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:59.104410Z","iopub.execute_input":"2023-05-24T16:24:59.104902Z","iopub.status.idle":"2023-05-24T16:24:59.177321Z","shell.execute_reply.started":"2023-05-24T16:24:59.104872Z","shell.execute_reply":"2023-05-24T16:24:59.176348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- we can reject the null hypothesis either","metadata":{}},{"cell_type":"code","source":"# i want to give a look to etxt_fqid\nimport random\n\nrandom_index=random.randint(0,len(data)-1)\n\ntext=data.loc[random_index,'fqid']\nprint(text)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:59.185650Z","iopub.execute_input":"2023-05-24T16:24:59.186597Z","iopub.status.idle":"2023-05-24T16:24:59.260785Z","shell.execute_reply.started":"2023-05-24T16:24:59.186557Z","shell.execute_reply":"2023-05-24T16:24:59.259603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Feature Engineering","metadata":{}},{"cell_type":"code","source":"data.head(1)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:59.262617Z","iopub.execute_input":"2023-05-24T16:24:59.263622Z","iopub.status.idle":"2023-05-24T16:24:59.289213Z","shell.execute_reply.started":"2023-05-24T16:24:59.263575Z","shell.execute_reply":"2023-05-24T16:24:59.288303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['year'] = data['session_id'].str.slice(start=0, stop=2).astype(np.int8)\ndata['month'] = data['session_id'].str.slice(start=2, stop=4).astype(np.int8)\n\ndata['day'] = data['session_id'].str.slice(start=4, stop=6).astype(np.int8)\n\ndata['hour'] =  data[\"session_id\"].str.slice(start=6, stop=8).astype(np.uint8)\n\ndata['difficulty']=np.where(data['level']<=12,'easy','hard')\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:24:59.290927Z","iopub.execute_input":"2023-05-24T16:24:59.291596Z","iopub.status.idle":"2023-05-24T16:25:20.087101Z","shell.execute_reply.started":"2023-05-24T16:24:59.291563Z","shell.execute_reply":"2023-05-24T16:25:20.084883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_new_feature(data):\n    data['session_id']=data['session_id'].astype(str)\n    data['year'] = data['session_id'].str.slice(start=0, stop=2).astype(np.int8)\n    data['month'] = data['session_id'].str.slice(start=2, stop=4).astype(np.int8)\n\n    data['day'] = data['session_id'].str.slice(start=4, stop=6).astype(np.int8)\n\n    data['hour'] =  data[\"session_id\"].str.slice(start=6, stop=8).astype(np.uint8)\n\n    data['difficulty']=np.where(data['level']<=12,'easy','hard')\n    \n    return data\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:25:20.088688Z","iopub.execute_input":"2023-05-24T16:25:20.089072Z","iopub.status.idle":"2023-05-24T16:25:20.101852Z","shell.execute_reply.started":"2023-05-24T16:25:20.089042Z","shell.execute_reply":"2023-05-24T16:25:20.099604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Nums= ['elapsed_time','hover_duration']\ncats=['event_name','fqid','room_fqid']\ntime=['day','hour','difficulty']\nevents = ['navigate_click','person_click','cutscene_click','object_click',\n          'map_hover','notification_click','map_click','observation_click',\n          'checkpoint']","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:25:20.103863Z","iopub.execute_input":"2023-05-24T16:25:20.105020Z","iopub.status.idle":"2023-05-24T16:25:20.121046Z","shell.execute_reply.started":"2023-05-24T16:25:20.104975Z","shell.execute_reply":"2023-05-24T16:25:20.119703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def feature_engineering(train):\n    \n    train=create_new_feature(train)\n    \n    df=[]\n    \n    for c in cats:\n        \n        tmp=train.groupby(['session_id','level_group','hour'])[c].agg('nunique')\n        \n        tmp.name=tmp.name+'_nunique'\n        \n        df.append(tmp)\n        \n    \n    for c in Nums:\n        \n        tmp=train.groupby(['session_id','level_group','hour'])[c].agg('mean')\n        \n        tmp.name=tmp.name+'_mean'\n        \n        df.append(tmp)\n        \n    for c in events:\n        \n        train[c]=(train.event_name==c).astype('int8')\n        \n    for c in events:\n        \n        tmp=train.groupby(['session_id','level_group','hour'])[c].agg('mean')\n        \n        tmp.name=tmp.name+'_mean'\n        \n        df.append(tmp)\n        \n    train=train.drop(events,axis=1)\n    \n    df=pd.concat(df,axis=1)\n    \n    df=df.fillna(-1)\n    \n    df=df.reset_index()\n    \n    df=df.set_index('session_id')\n    \n    return df \n    \n        \n    ","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:25:20.122983Z","iopub.execute_input":"2023-05-24T16:25:20.125688Z","iopub.status.idle":"2023-05-24T16:25:20.143067Z","shell.execute_reply.started":"2023-05-24T16:25:20.125648Z","shell.execute_reply":"2023-05-24T16:25:20.141844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train = pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train.csv')\n# df = feature_engineering(train)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:25:20.144564Z","iopub.execute_input":"2023-05-24T16:25:20.146137Z","iopub.status.idle":"2023-05-24T16:25:20.162130Z","shell.execute_reply.started":"2023-05-24T16:25:20.146087Z","shell.execute_reply":"2023-05-24T16:25:20.160814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = pd.read_csv(\"/kaggle/input/predict-student-performance-from-game-play/train.csv\",usecols=[0])\ntmp = tmp.groupby('session_id').session_id.agg('count')\n\n# COMPUTE READS AND SKIPS\nPIECES = 10\nCHUNK = int( np.ceil(len(tmp)/PIECES) )\n\nreads = []\nskips = [0]\nfor k in range(PIECES):\n    a = k*CHUNK\n    b = (k+1)*CHUNK\n    if b>len(tmp): b=len(tmp)\n    r = tmp.iloc[a:b].sum()\n    reads.append(r)\n    skips.append(skips[-1]+r)\n    \nprint(f'To avoid memory error, we will read train in {PIECES} pieces of sizes:')\nprint(reads)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:25:20.164451Z","iopub.execute_input":"2023-05-24T16:25:20.165398Z","iopub.status.idle":"2023-05-24T16:26:48.191831Z","shell.execute_reply.started":"2023-05-24T16:25:20.165346Z","shell.execute_reply":"2023-05-24T16:26:48.190569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"all_pieces = []\nfor k in range(PIECES) :\n    print(k, ',', end = ' ')\n    SKIPS = 0\n    if k>0 : SKIPS = range(1, skips[k] + 1)\n    train = pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train.csv', nrows = reads[k], skiprows = SKIPS, dtype = dtypes, low_memory = True)\n    df = feature_engineering(train)\n    all_pieces.append(df)\n    del train; del df; gc.collect()\nprint('\\n')\ntrain_df = pd.concat(all_pieces, axis = 0)\nprint(f'Shape of Train DF : {train_df.shape}')\ndisplay(train_df.head())\n","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:26:48.193776Z","iopub.execute_input":"2023-05-24T16:26:48.194536Z","iopub.status.idle":"2023-05-24T16:38:20.642380Z","shell.execute_reply.started":"2023-05-24T16:26:48.194502Z","shell.execute_reply":"2023-05-24T16:38:20.639369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[train_df['elapsed_time_mean']!=-1]","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:38:20.653197Z","iopub.execute_input":"2023-05-24T16:38:20.654094Z","iopub.status.idle":"2023-05-24T16:38:20.837443Z","shell.execute_reply.started":"2023-05-24T16:38:20.654057Z","shell.execute_reply":"2023-05-24T16:38:20.836123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_features=[c for c in train_df.columns if c!='level_group']\nprint(\"we will train on {}\".format(len(training_features)))\n\nprint(\"training on {}\".format(len(train_df.index.unique())))","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:38:20.838591Z","iopub.execute_input":"2023-05-24T16:38:20.838927Z","iopub.status.idle":"2023-05-24T16:38:21.163581Z","shell.execute_reply.started":"2023-05-24T16:38:20.838899Z","shell.execute_reply":"2023-05-24T16:38:21.162544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.reset_index('session_id',inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:38:21.164854Z","iopub.execute_input":"2023-05-24T16:38:21.165651Z","iopub.status.idle":"2023-05-24T16:38:21.177193Z","shell.execute_reply.started":"2023-05-24T16:38:21.165614Z","shell.execute_reply":"2023-05-24T16:38:21.175846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['q'] = labels.session_id.apply(lambda x: int(x.split('_')[-1][1:]) )\n","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:38:21.179108Z","iopub.execute_input":"2023-05-24T16:38:21.179661Z","iopub.status.idle":"2023-05-24T16:38:21.763839Z","shell.execute_reply.started":"2023-05-24T16:38:21.179616Z","shell.execute_reply":"2023-05-24T16:38:21.762558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:38:21.765505Z","iopub.execute_input":"2023-05-24T16:38:21.766027Z","iopub.status.idle":"2023-05-24T16:38:21.788726Z","shell.execute_reply.started":"2023-05-24T16:38:21.765993Z","shell.execute_reply":"2023-05-24T16:38:21.787531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import GroupKFold\nfrom xgboost import XGBClassifier\nkfold = GroupKFold(n_splits=5)\n\ndf=pd.DataFrame(data=np.zeros((len(train_df.index.unique()),18)),index=train_df.index.unique())\n\nmodels={}\n\n\nfor i, (train_index,test_index) in enumerate(kfold.split(X=train_df,groups=train_df.index)):\n    \n    \n    print('## Fold',i+1)\n    \n    print('#'*25)\n    \n    xgb_params={\n        \n        'objective':'binary:logistic',\n        'eval_metric':'logloss',\n        'learning_rate':0.001,\n        'max_depth':6,\n        'n_estimators':1000,\n        'early_stopping_rounds':50,\n        'tree_method':'hist',\n        'subsample':0.8,\n        'colsample_bytree':0.4,\n        'use_label_encoder':False\n    }\n    \n    \n    for t in range(1,19):\n        \n        if t<=3:\n            grp='0-4'\n        elif t<=13:\n            \n            grp='5-12'\n        elif t<=22:\n            \n            grp=\"13-22\"\n            \n        train_x=train_df.iloc[train_index]\n        train_x=train_x.loc[train_x.level_group==grp]\n        train_users=train_x.index.values\n        \n        train_y=labels.loc[labels.q==t].set_index('session_id_correct').loc[train_users]\n        \n        \n        valid_x=train_df.iloc[test_index]\n        \n        valid_x=valid_x.loc[valid_x.level_group==grp]\n        valid_users=valid_x.index.values\n        valid_y=labels.loc[labels.q==t].set_index('session_id_correct').loc[valid_users]\n        \n        \n        model=XGBClassifier(**xgb_params)\n        \n        model.fit(train_x[training_features].astype('float32'),train_y['correct'],\n                 eval_set=[(valid_x[training_features].astype('float32'),valid_y['correct'])],\n                 verbose=0)\n        \n        print(f'{t}({model.best_ntree_limit}), ',end='')\n        \n        models[f'{grp}_{t}'] = model\n        df.loc[valid_users, t-1] = model.predict_proba(valid_x[training_features].astype('float32'))[:,1]\n        \n    print()\n\nprint('end of training')","metadata":{"execution":{"iopub.status.busy":"2023-05-24T16:38:21.790699Z","iopub.execute_input":"2023-05-24T16:38:21.791508Z","iopub.status.idle":"2023-05-24T19:05:54.716461Z","shell.execute_reply.started":"2023-05-24T16:38:21.791475Z","shell.execute_reply":"2023-05-24T19:05:54.713827Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2023-05-24T19:11:32.009546Z","iopub.execute_input":"2023-05-24T19:11:32.012090Z","iopub.status.idle":"2023-05-24T19:11:32.051355Z","shell.execute_reply.started":"2023-05-24T19:11:32.012008Z","shell.execute_reply":"2023-05-24T19:11:32.050263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}