{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":45533,"databundleVersionId":5748852,"sourceType":"competition"}],"dockerImageVersionId":30458,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import the Required Libraries","metadata":{"id":"zAXHC6-Tn2O5"}},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_addons as tfa\nimport tensorflow_decision_forests as tfdf\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"id":"IanlX-Eqn2O5","execution":{"iopub.status.busy":"2023-12-04T10:49:01.901427Z","iopub.execute_input":"2023-12-04T10:49:01.902312Z","iopub.status.idle":"2023-12-04T10:49:01.908175Z","shell.execute_reply.started":"2023-12-04T10:49:01.902259Z","shell.execute_reply":"2023-12-04T10:49:01.907016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"TensorFlow Decision Forests v\" + tfdf.__version__)\nprint(\"TensorFlow Addons v\" + tfa.__version__)\nprint(\"TensorFlow v\" + tf.__version__)","metadata":{"id":"gLpK2yAen2O7","execution":{"iopub.status.busy":"2023-12-04T10:49:01.920651Z","iopub.execute_input":"2023-12-04T10:49:01.920992Z","iopub.status.idle":"2023-12-04T10:49:01.926868Z","shell.execute_reply.started":"2023-12-04T10:49:01.920956Z","shell.execute_reply":"2023-12-04T10:49:01.925764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load the Dataset","metadata":{"id":"22DpLVFLn2O7"}},{"cell_type":"code","source":"# Reference: https://www.kaggle.com/competitions/predict-student-performance-from-game-play/discussion/384359\ndtypes={\n    'elapsed_time':np.int32,\n    'event_name':'category',\n    'name':'category',\n    'level':np.uint8,\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'}\n\ndataset_df = pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train.csv', dtype=dtypes)\nprint(\"Full train dataset shape is {}\".format(dataset_df.shape))","metadata":{"id":"_XItl24kn2O7","execution":{"iopub.status.busy":"2023-12-04T10:49:01.943454Z","iopub.execute_input":"2023-12-04T10:49:01.944111Z","iopub.status.idle":"2023-12-04T10:50:51.771053Z","shell.execute_reply.started":"2023-12-04T10:49:01.944077Z","shell.execute_reply":"2023-12-04T10:50:51.768758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display the first 5 examples\ndataset_df","metadata":{"id":"-RTRVRiWn2O8","execution":{"iopub.status.busy":"2023-12-04T10:50:51.773760Z","iopub.execute_input":"2023-12-04T10:50:51.774181Z","iopub.status.idle":"2023-12-04T10:50:51.824580Z","shell.execute_reply.started":"2023-12-04T10:50:51.774139Z","shell.execute_reply":"2023-12-04T10:50:51.823394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load the labels","metadata":{"id":"ReNY-i3bn2O8"}},{"cell_type":"code","source":"labels = pd.read_csv('/kaggle/input/predict-student-performance-from-game-play/train_labels.csv')","metadata":{"id":"KD4uayl2n2O9","execution":{"iopub.status.busy":"2023-12-04T10:52:05.484179Z","iopub.execute_input":"2023-12-04T10:52:05.485060Z","iopub.status.idle":"2023-12-04T10:52:05.744716Z","shell.execute_reply.started":"2023-12-04T10:52:05.485011Z","shell.execute_reply":"2023-12-04T10:52:05.743527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['session'] = labels.session_id.apply(lambda x: int(x.split('_')[0]) )\nlabels['q'] = labels.session_id.apply(lambda x: int(x.split('_')[-1][1:]) )","metadata":{"id":"Kva8_Dbqn2O9","execution":{"iopub.status.busy":"2023-12-04T10:52:07.113365Z","iopub.execute_input":"2023-12-04T10:52:07.113851Z","iopub.status.idle":"2023-12-04T10:52:08.130562Z","shell.execute_reply.started":"2023-12-04T10:52:07.113809Z","shell.execute_reply":"2023-12-04T10:52:08.129338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# labels = pd.get_dummies(labels, columns=['q'], prefix='q').groupby('session').sum()\nlabels","metadata":{"id":"0eD-KZMvn2O-","execution":{"iopub.status.busy":"2023-12-04T10:52:10.111527Z","iopub.execute_input":"2023-12-04T10:52:10.112412Z","iopub.status.idle":"2023-12-04T10:52:10.126964Z","shell.execute_reply.started":"2023-12-04T10:52:10.112366Z","shell.execute_reply":"2023-12-04T10:52:10.125832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Bar chart for label column: correct","metadata":{"id":"JLHAQ1EEn2O-"}},{"cell_type":"code","source":"plt.figure(figsize=(3, 3))\nplot_df = labels.correct.value_counts()\nplot_df.plot(kind=\"bar\", color=['b', 'c'])","metadata":{"id":"-l9wBCTYn2O_","execution":{"iopub.status.busy":"2023-12-04T10:52:13.102739Z","iopub.execute_input":"2023-12-04T10:52:13.103497Z","iopub.status.idle":"2023-12-04T10:52:13.306836Z","shell.execute_reply.started":"2023-12-04T10:52:13.103449Z","shell.execute_reply":"2023-12-04T10:52:13.305779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 20))\nplt.subplots_adjust(hspace=0.5, wspace=0.5)\nplt.suptitle(\"\\\"Correct\\\" column values for each question\", fontsize=14, y=0.94)\nfor n in range(1,19):\n    #print(n, str(n))\n    ax = plt.subplot(6, 3, n)\n\n    # filter df and plot ticker on the new subplot axis\n    plot_df = labels.loc[labels.q == n]\n    plot_df = plot_df.correct.value_counts()\n    plot_df.plot(ax=ax, kind=\"bar\", color=['b', 'c'])\n    \n    # chart formatting\n    ax.set_title(\"Question \" + str(n))\n    ax.set_xlabel(\"\")\n","metadata":{"id":"X238--97n2O_","execution":{"iopub.status.busy":"2023-12-04T10:52:15.310514Z","iopub.execute_input":"2023-12-04T10:52:15.311375Z","iopub.status.idle":"2023-12-04T10:52:17.457711Z","shell.execute_reply.started":"2023-12-04T10:52:15.311333Z","shell.execute_reply":"2023-12-04T10:52:17.456646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Prepare the dataset","metadata":{"id":"y5fK05dsn2O_"}},{"cell_type":"code","source":"CATEGORICAL = ['event_name', 'name','fqid', 'room_fqid', 'text_fqid']\nNUMERICAL = ['elapsed_time','level','page','room_coor_x', 'room_coor_y', \n        'screen_coor_x', 'screen_coor_y', 'hover_duration']","metadata":{"id":"cCZWGiL_n2PA","execution":{"iopub.status.busy":"2023-12-04T10:52:17.784457Z","iopub.execute_input":"2023-12-04T10:52:17.784881Z","iopub.status.idle":"2023-12-04T10:52:17.790668Z","shell.execute_reply.started":"2023-12-04T10:52:17.784841Z","shell.execute_reply":"2023-12-04T10:52:17.789392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reference: https://www.kaggle.com/code/cdeotte/random-forest-baseline-0-664/notebook\n\ndef feature_engineer(dataset_df):\n    dfs = []\n    for c in CATEGORICAL:\n        tmp = dataset_df.groupby(['session_id','level_group'])[c].agg('nunique')\n        tmp.name = tmp.name + '_nunique'\n        dfs.append(tmp)\n    for c in NUMERICAL:\n        tmp = dataset_df.groupby(['session_id','level_group'])[c].agg('mean')\n        dfs.append(tmp)\n    for c in NUMERICAL:\n        tmp = dataset_df.groupby(['session_id','level_group'])[c].agg('std')\n        tmp.name = tmp.name + '_std'\n        dfs.append(tmp)\n    dataset_df = pd.concat(dfs,axis=1)\n    dataset_df = dataset_df.fillna(-1)\n    dataset_df = dataset_df.reset_index()\n    dataset_df = dataset_df.set_index('session_id')\n    return dataset_df","metadata":{"id":"nHWhAOtTn2PA","execution":{"iopub.status.busy":"2023-12-04T10:52:19.065683Z","iopub.execute_input":"2023-12-04T10:52:19.066485Z","iopub.status.idle":"2023-12-04T10:52:19.075537Z","shell.execute_reply.started":"2023-12-04T10:52:19.066445Z","shell.execute_reply":"2023-12-04T10:52:19.074460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_df = feature_engineer(dataset_df)\nprint(\"Full prepared dataset shape is {}\".format(dataset_df.shape))","metadata":{"id":"JKcoPoemn2PA","execution":{"iopub.status.busy":"2023-12-04T10:52:21.060605Z","iopub.execute_input":"2023-12-04T10:52:21.061608Z","iopub.status.idle":"2023-12-04T10:52:58.531574Z","shell.execute_reply.started":"2023-12-04T10:52:21.061564Z","shell.execute_reply":"2023-12-04T10:52:58.530311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Basic exploration of the prepared dataset","metadata":{"id":"Ij7TT3x-n2PB"}},{"cell_type":"code","source":"result_df = pd.merge(dataset_df, labels, left_on=['session_id'], right_on=['session'], how='inner')\nresult_df","metadata":{"execution":{"iopub.status.busy":"2023-12-04T10:52:58.534069Z","iopub.execute_input":"2023-12-04T10:52:58.534419Z","iopub.status.idle":"2023-12-04T10:52:59.121162Z","shell.execute_reply.started":"2023-12-04T10:52:58.534384Z","shell.execute_reply":"2023-12-04T10:52:59.119893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows=6, ncols=3, figsize=(10, 15))\n\nfor i in range(1, 19):\n    row = (i - 1) // 3\n    col = (i - 1) % 3\n    corr_df = result_df[result_df['q'] == i].corr()['correct'].drop(['q', 'correct'])\n    sns.barplot(x=corr_df.index, y=corr_df.values, color='r', alpha=0.55, ax=axes[row, col])\n    axes[row, col].set_title('Question %d' % i, fontsize=7)\n    axes[row, col].set_xticklabels(axes[row, col].get_xticklabels(), rotation=90, ha='right', fontsize=5)\n    axes[row, col].tick_params(axis='y', labelsize=5)\n\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-04T10:52:59.122471Z","iopub.execute_input":"2023-12-04T10:52:59.122837Z","iopub.status.idle":"2023-12-04T10:53:07.018777Z","shell.execute_reply.started":"2023-12-04T10:52:59.122802Z","shell.execute_reply":"2023-12-04T10:53:07.017523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display the first 5 examples\ndataset_df.head(5)","metadata":{"id":"mvQEsdV1n2PB","execution":{"iopub.status.busy":"2023-12-04T10:26:35.401357Z","iopub.execute_input":"2023-12-04T10:26:35.401806Z","iopub.status.idle":"2023-12-04T10:26:35.434073Z","shell.execute_reply.started":"2023-12-04T10:26:35.401761Z","shell.execute_reply":"2023-12-04T10:26:35.432995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_df.describe()","metadata":{"id":"DRusg-N1n2PB","execution":{"iopub.status.busy":"2023-12-04T10:26:35.435534Z","iopub.execute_input":"2023-12-04T10:26:35.435844Z","iopub.status.idle":"2023-12-04T10:26:35.564068Z","shell.execute_reply.started":"2023-12-04T10:26:35.435811Z","shell.execute_reply":"2023-12-04T10:26:35.563008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Numerical data distribution¶","metadata":{"id":"xboIq9oDn2PB"}},{"cell_type":"code","source":"figure, axis = plt.subplots(3, 2, figsize=(10, 10))\n\nfor name, data in dataset_df.groupby('level_group'):\n    axis[0, 0].plot(range(1, len(data['room_coor_x_std'])+1), data['room_coor_x_std'], label=name)\n    axis[0, 1].plot(range(1, len(data['room_coor_y_std'])+1), data['room_coor_y_std'], label=name)\n    axis[1, 0].plot(range(1, len(data['screen_coor_x_std'])+1), data['screen_coor_x_std'], label=name)\n    axis[1, 1].plot(range(1, len(data['screen_coor_y_std'])+1), data['screen_coor_y_std'], label=name)\n    axis[2, 0].plot(range(1, len(data['hover_duration'])+1), data['hover_duration_std'], label=name)\n    axis[2, 1].plot(range(1, len(data['elapsed_time_std'])+1), data['elapsed_time_std'], label=name)\n    \n\naxis[0, 0].set_title('room_coor_x')\naxis[0, 1].set_title('room_coor_y')\naxis[1, 0].set_title('screen_coor_x')\naxis[1, 1].set_title('screen_coor_y')\naxis[2, 0].set_title('hover_duration')\naxis[2, 1].set_title('elapsed_time_std')\n\nfor i in range(3):\n    axis[i, 0].legend()\n    axis[i, 1].legend()\n\nplt.show()","metadata":{"id":"mXIiaq_bn2PC","execution":{"iopub.status.busy":"2023-12-04T10:23:53.978169Z","iopub.status.idle":"2023-12-04T10:23:53.978560Z","shell.execute_reply.started":"2023-12-04T10:23:53.978359Z","shell.execute_reply":"2023-12-04T10:23:53.978379Z"},"trusted":true},"execution_count":null,"outputs":[]}]}