{"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)\n\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-04-30T07:25:30.635625Z","iopub.execute_input":"2023-04-30T07:25:30.636569Z","iopub.status.idle":"2023-04-30T07:25:30.643569Z","shell.execute_reply.started":"2023-04-30T07:25:30.636515Z","shell.execute_reply":"2023-04-30T07:25:30.642651Z"},"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\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-04-30T07:26:58.186073Z","iopub.execute_input":"2023-04-30T07:26:58.186700Z","iopub.status.idle":"2023-04-30T07:27:09.698843Z","shell.execute_reply.started":"2023-04-30T07:26:58.186651Z","shell.execute_reply":"2023-04-30T07:27:09.696997Z"},"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-04-30T07:27:21.607805Z","iopub.execute_input":"2023-04-30T07:27:21.608818Z","iopub.status.idle":"2023-04-30T07:27:21.616504Z","shell.execute_reply.started":"2023-04-30T07:27:21.608768Z","shell.execute_reply":"2023-04-30T07:27:21.614699Z"},"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))","metadata":{"execution":{"iopub.status.busy":"2023-04-30T07:27:43.598380Z","iopub.execute_input":"2023-04-30T07:27:43.598834Z","iopub.status.idle":"2023-04-30T07:29:59.520620Z","shell.execute_reply.started":"2023-04-30T07:27:43.598798Z","shell.execute_reply":"2023-04-30T07:29:59.519587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display the first 5 examples\ndataset_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T07:30:25.683434Z","iopub.execute_input":"2023-04-30T07:30:25.685712Z","iopub.status.idle":"2023-04-30T07:30:25.751466Z","shell.execute_reply.started":"2023-04-30T07:30:25.685644Z","shell.execute_reply":"2023-04-30T07:30:25.750328Z"},"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-04-30T07:31:14.292015Z","iopub.execute_input":"2023-04-30T07:31:14.292531Z","iopub.status.idle":"2023-04-30T07:31:14.568549Z","shell.execute_reply.started":"2023-04-30T07:31:14.292489Z","shell.execute_reply":"2023-04-30T07:31:14.567474Z"},"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-04-30T07:31:26.528907Z","iopub.execute_input":"2023-04-30T07:31:26.529357Z","iopub.status.idle":"2023-04-30T07:31:27.270400Z","shell.execute_reply.started":"2023-04-30T07:31:26.529307Z","shell.execute_reply":"2023-04-30T07:31:27.269408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display the first 5 examples\nlabels.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T07:31:36.199074Z","iopub.execute_input":"2023-04-30T07:31:36.199596Z","iopub.status.idle":"2023-04-30T07:31:36.212396Z","shell.execute_reply.started":"2023-04-30T07:31:36.199552Z","shell.execute_reply":"2023-04-30T07:31:36.211219Z"},"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-04-30T07:31:48.531464Z","iopub.execute_input":"2023-04-30T07:31:48.531915Z","iopub.status.idle":"2023-04-30T07:31:48.787279Z","shell.execute_reply.started":"2023-04-30T07:31:48.531879Z","shell.execute_reply":"2023-04-30T07:31:48.786408Z"},"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-04-30T07:32:01.865153Z","iopub.execute_input":"2023-04-30T07:32:01.865564Z","iopub.status.idle":"2023-04-30T07:32:03.681269Z","shell.execute_reply.started":"2023-04-30T07:32:01.865528Z","shell.execute_reply":"2023-04-30T07:32:03.680235Z"},"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-04-30T07:32:26.060144Z","iopub.execute_input":"2023-04-30T07:32:26.060619Z","iopub.status.idle":"2023-04-30T07:32:26.066650Z","shell.execute_reply.started":"2023-04-30T07:32:26.060576Z","shell.execute_reply":"2023-04-30T07:32:26.065583Z"},"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-04-30T07:32:39.444008Z","iopub.execute_input":"2023-04-30T07:32:39.444840Z","iopub.status.idle":"2023-04-30T07:32:39.454937Z","shell.execute_reply.started":"2023-04-30T07:32:39.444796Z","shell.execute_reply":"2023-04-30T07:32:39.453359Z"},"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-04-30T07:32:49.561989Z","iopub.execute_input":"2023-04-30T07:32:49.562469Z","iopub.status.idle":"2023-04-30T07:33:29.428799Z","shell.execute_reply.started":"2023-04-30T07:32:49.562427Z","shell.execute_reply":"2023-04-30T07:33:29.427483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Display the first 5 examples\ndataset_df.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-04-30T07:33:41.063598Z","iopub.execute_input":"2023-04-30T07:33:41.064031Z","iopub.status.idle":"2023-04-30T07:33:41.096076Z","shell.execute_reply.started":"2023-04-30T07:33:41.063995Z","shell.execute_reply":"2023-04-30T07:33:41.095085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_df.describe()","metadata":{"execution":{"iopub.status.busy":"2023-04-30T07:33:49.956668Z","iopub.execute_input":"2023-04-30T07:33:49.957102Z","iopub.status.idle":"2023-04-30T07:33:50.118456Z","shell.execute_reply.started":"2023-04-30T07:33:49.957066Z","shell.execute_reply":"2023-04-30T07:33:50.117115Z"},"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-04-30T07:34:07.612802Z","iopub.execute_input":"2023-04-30T07:34:07.613211Z","iopub.status.idle":"2023-04-30T07:34:10.294273Z","shell.execute_reply.started":"2023-04-30T07:34:07.613175Z","shell.execute_reply":"2023-04-30T07:34:10.293105Z"},"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-04-30T07:34:26.419486Z","iopub.execute_input":"2023-04-30T07:34:26.419900Z","iopub.status.idle":"2023-04-30T07:34:26.861550Z","shell.execute_reply.started":"2023-04-30T07:34:26.419865Z","shell.execute_reply":"2023-04-30T07:34:26.860325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tfdf.keras.get_all_models()","metadata":{"execution":{"iopub.status.busy":"2023-04-30T07:34:37.340224Z","iopub.execute_input":"2023-04-30T07:34:37.340657Z","iopub.status.idle":"2023-04-30T07:34:37.349544Z","shell.execute_reply.started":"2023-04-30T07:34:37.340619Z","shell.execute_reply":"2023-04-30T07:34:37.348452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! head submission.csv","metadata":{"execution":{"iopub.status.busy":"2023-04-30T08:53:00.993676Z","iopub.execute_input":"2023-04-30T08:53:00.994116Z","iopub.status.idle":"2023-04-30T08:53:02.093408Z","shell.execute_reply.started":"2023-04-30T08:53:00.994063Z","shell.execute_reply":"2023-04-30T08:53:02.092039Z"},"trusted":true},"execution_count":null,"outputs":[]}]}