{"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":"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","metadata":{"execution":{"iopub.status.busy":"2023-05-06T12:04:49.757627Z","iopub.execute_input":"2023-05-06T12:04:49.758185Z","iopub.status.idle":"2023-05-06T12:05:00.125017Z","shell.execute_reply.started":"2023-05-06T12:04:49.758139Z","shell.execute_reply":"2023-05-06T12:05:00.123851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install tensorflow_addons","metadata":{"execution":{"iopub.status.busy":"2023-05-06T12:05:45.146708Z","iopub.execute_input":"2023-05-06T12:05:45.147145Z","iopub.status.idle":"2023-05-06T12:05:58.133811Z","shell.execute_reply.started":"2023-05-06T12:05:45.147113Z","shell.execute_reply":"2023-05-06T12:05:58.132503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install tensorflow","metadata":{"execution":{"iopub.status.busy":"2023-05-06T12:06:10.917071Z","iopub.execute_input":"2023-05-06T12:06:10.917522Z","iopub.status.idle":"2023-05-06T12:06:35.44788Z","shell.execute_reply.started":"2023-05-06T12:06:10.91749Z","shell.execute_reply":"2023-05-06T12:06:35.446512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install tensorflow__decision_forests","metadata":{"execution":{"iopub.status.busy":"2023-05-06T12:07:34.237263Z","iopub.execute_input":"2023-05-06T12:07:34.237715Z","iopub.status.idle":"2023-05-06T12:07:45.283123Z","shell.execute_reply.started":"2023-05-06T12:07:34.237679Z","shell.execute_reply":"2023-05-06T12:07:45.281982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_addons as tfa\nimport tensorflow_decision_forests as tfdf","metadata":{"execution":{"iopub.status.busy":"2023-05-06T12:08:52.497368Z","iopub.execute_input":"2023-05-06T12:08:52.497854Z","iopub.status.idle":"2023-05-06T12:08:52.506077Z","shell.execute_reply.started":"2023-05-06T12:08:52.497818Z","shell.execute_reply":"2023-05-06T12:08:52.50508Z"},"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":{"execution":{"iopub.status.busy":"2023-05-06T12:08:56.533382Z","iopub.execute_input":"2023-05-06T12:08:56.534107Z","iopub.status.idle":"2023-05-06T12:08:56.538993Z","shell.execute_reply.started":"2023-05-06T12:08:56.534067Z","shell.execute_reply":"2023-05-06T12:08:56.538227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dtypes={\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))\n","metadata":{"execution":{"iopub.status.busy":"2023-05-06T12:09:55.694281Z","iopub.execute_input":"2023-05-06T12:09:55.694704Z","iopub.status.idle":"2023-05-06T12:12:22.696453Z","shell.execute_reply.started":"2023-05-06T12:09:55.694673Z","shell.execute_reply":"2023-05-06T12:12:22.694981Z"},"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')","metadata":{"execution":{"iopub.status.busy":"2023-05-06T12:14:24.106545Z","iopub.execute_input":"2023-05-06T12:14:24.107349Z","iopub.status.idle":"2023-05-06T12:14:24.530685Z","shell.execute_reply.started":"2023-05-06T12:14:24.10731Z","shell.execute_reply":"2023-05-06T12:14:24.529636Z"},"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":{"execution":{"iopub.status.busy":"2023-05-06T12:17:00.177436Z","iopub.execute_input":"2023-05-06T12:17:00.177914Z","iopub.status.idle":"2023-05-06T12:17:00.848115Z","shell.execute_reply.started":"2023-05-06T12:17:00.177883Z","shell.execute_reply":"2023-05-06T12:17:00.847171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T12:18:27.917752Z","iopub.execute_input":"2023-05-06T12:18:27.918196Z","iopub.status.idle":"2023-05-06T12:18:27.946713Z","shell.execute_reply.started":"2023-05-06T12:18:27.918166Z","shell.execute_reply":"2023-05-06T12:18:27.945934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(3, 3))\nplot_df = labels.correct.value_counts()\nplot_df.plot(kind=\"bar\", color=['b', 'c'])","metadata":{"execution":{"iopub.status.busy":"2023-05-06T12:18:58.703869Z","iopub.execute_input":"2023-05-06T12:18:58.704275Z","iopub.status.idle":"2023-05-06T12:18:58.93648Z","shell.execute_reply.started":"2023-05-06T12:18:58.704245Z","shell.execute_reply":"2023-05-06T12:18:58.935664Z"},"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(\"\")","metadata":{"execution":{"iopub.status.busy":"2023-05-06T12:19:34.132117Z","iopub.execute_input":"2023-05-06T12:19:34.132573Z","iopub.status.idle":"2023-05-06T12:19:36.336358Z","shell.execute_reply.started":"2023-05-06T12:19:34.132527Z","shell.execute_reply":"2023-05-06T12:19:36.335481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2023-05-06T12:23:15.184586Z","iopub.execute_input":"2023-05-06T12:23:15.187593Z","iopub.status.idle":"2023-05-06T12:23:15.203932Z","shell.execute_reply.started":"2023-05-06T12:23:15.187494Z","shell.execute_reply":"2023-05-06T12:23:15.201386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def 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":{"execution":{"iopub.status.busy":"2023-05-06T12:24:13.210128Z","iopub.execute_input":"2023-05-06T12:24:13.210712Z","iopub.status.idle":"2023-05-06T12:24:13.225972Z","shell.execute_reply.started":"2023-05-06T12:24:13.210673Z","shell.execute_reply":"2023-05-06T12:24:13.22485Z"},"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":{"execution":{"iopub.status.busy":"2023-05-06T12:24:26.806208Z","iopub.execute_input":"2023-05-06T12:24:26.806729Z","iopub.status.idle":"2023-05-06T12:25:08.837606Z","shell.execute_reply.started":"2023-05-06T12:24:26.80669Z","shell.execute_reply":"2023-05-06T12:25:08.836624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-05-06T12:39:24.135313Z","iopub.execute_input":"2023-05-06T12:39:24.135857Z","iopub.status.idle":"2023-05-06T12:39:24.175491Z","shell.execute_reply.started":"2023-05-06T12:39:24.135824Z","shell.execute_reply":"2023-05-06T12:39:24.173086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-05-06T12:39:33.886144Z","iopub.execute_input":"2023-05-06T12:39:33.886599Z","iopub.status.idle":"2023-05-06T12:39:34.060381Z","shell.execute_reply.started":"2023-05-06T12:39:33.886554Z","shell.execute_reply":"2023-05-06T12:39:34.059325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2023-05-06T12:40:49.894825Z","iopub.execute_input":"2023-05-06T12:40:49.895349Z","iopub.status.idle":"2023-05-06T12:40:52.678206Z","shell.execute_reply.started":"2023-05-06T12:40:49.895315Z","shell.execute_reply":"2023-05-06T12:40:52.671983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def split_dataset(dataset, test_ratio=0.20):\n    USER_LIST = dataset_df.index.unique()\n    split = int(len(USER_LIST) * (1 - 0.20))\n    return dataset.loc[USER_LIST[:split]], dataset.loc[USER_LIST[split:]]\n\ntrain_x, valid_x = split_dataset(dataset_df)\nprint(\"{} examples in training, {} examples in testing.\".format(\n    len(train_x), len(valid_x)))","metadata":{"execution":{"iopub.status.busy":"2023-05-06T12:41:26.628729Z","iopub.execute_input":"2023-05-06T12:41:26.629186Z","iopub.status.idle":"2023-05-06T12:41:26.70613Z","shell.execute_reply.started":"2023-05-06T12:41:26.629154Z","shell.execute_reply":"2023-05-06T12:41:26.705282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tfdf.keras.get_all_models()","metadata":{"execution":{"iopub.status.busy":"2023-05-06T12:41:54.715139Z","iopub.execute_input":"2023-05-06T12:41:54.715597Z","iopub.status.idle":"2023-05-06T12:41:54.723919Z","shell.execute_reply.started":"2023-05-06T12:41:54.71555Z","shell.execute_reply":"2023-05-06T12:41:54.722836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_decision_forests as tfdf\nimport pandas as pd\n\ndataset = pd.read_csv(\"/kaggle/input/predict-student-performance-from-game-play/train.csv\")\ntf_dataset = tfdf.keras.pd_dataframe_to_tf_dataset(dataset, label=\"my_label\")\n\nmodel = tfdf.keras.GradientBoostedTreesModel()\nmodel.fit(tf_dataset)\n\nprint(model.summary())","metadata":{"execution":{"iopub.status.busy":"2023-05-06T13:17:56.821876Z","iopub.execute_input":"2023-05-06T13:17:56.823253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rf = tfdf.keras.GradientBoostedTreesModel(hyperparameter_template=\"benchmark_rank1\")","metadata":{"execution":{"iopub.status.busy":"2023-05-06T13:22:45.023489Z","iopub.execute_input":"2023-05-06T13:22:45.024543Z","iopub.status.idle":"2023-05-06T13:22:45.424514Z","shell.execute_reply.started":"2023-05-06T13:22:45.024486Z","shell.execute_reply":"2023-05-06T13:22:45.421742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip upgrade numpy","metadata":{"execution":{"iopub.status.busy":"2023-05-06T13:27:48.605505Z","iopub.execute_input":"2023-05-06T13:27:48.605999Z","iopub.status.idle":"2023-05-06T13:27:50.225718Z","shell.execute_reply.started":"2023-05-06T13:27:48.605962Z","shell.execute_reply":"2023-05-06T13:27:50.224489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}