{"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 gc\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom tqdm import tqdm\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-06-10T16:39:40.300517Z","iopub.execute_input":"2023-06-10T16:39:40.301322Z","iopub.status.idle":"2023-06-10T16:39:40.319727Z","shell.execute_reply.started":"2023-06-10T16:39:40.301274Z","shell.execute_reply":"2023-06-10T16:39:40.318768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Study what is session_level\ntest = pd.read_csv(\"/kaggle/input/predict-student-performance-from-game-play/test.csv\")\nfor session_level in test.session_level.unique():\n    print(session_level, test[test.session_level == session_level].session_id.unique(), test[test.session_level == session_level].level_group.unique())","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:39:40.322238Z","iopub.execute_input":"2023-06-10T16:39:40.322867Z","iopub.status.idle":"2023-06-10T16:39:40.388098Z","shell.execute_reply.started":"2023-06-10T16:39:40.322832Z","shell.execute_reply":"2023-06-10T16:39:40.387007Z"},"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.int16,\n    'session_id': \"category\",\n    'level_group': \"category\",\n    \"index\": np.int32,\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}\n\ntrain = pd.read_csv(\n    '/kaggle/input/predict-student-performance-from-game-play/train.csv', \n    usecols=[\"session_id\", \"elapsed_time\", \"index\", \"level_group\", \"level\", \"event_name\"],\n    dtype=dtypes,\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:39:40.389421Z","iopub.execute_input":"2023-06-10T16:39:40.390077Z","iopub.status.idle":"2023-06-10T16:41:11.247452Z","shell.execute_reply.started":"2023-06-10T16:39:40.389945Z","shell.execute_reply":"2023-06-10T16:41:11.245960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:41:11.249288Z","iopub.execute_input":"2023-06-10T16:41:11.249664Z","iopub.status.idle":"2023-06-10T16:41:11.282279Z","shell.execute_reply.started":"2023-06-10T16:41:11.249619Z","shell.execute_reply":"2023-06-10T16:41:11.281077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def ordered_unique(arr):\n    seen = set()\n    ordered = []\n    for item in tqdm(arr):\n        if item in seen:\n            continue\n        seen.add(item)\n        ordered.append(item)\n    return ordered\n\nsession_ids = ordered_unique(train.session_id.values)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:41:11.287123Z","iopub.execute_input":"2023-06-10T16:41:11.287570Z","iopub.status.idle":"2023-06-10T16:41:22.684186Z","shell.execute_reply.started":"2023-06-10T16:41:11.287536Z","shell.execute_reply":"2023-06-10T16:41:22.682246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:41:22.687361Z","iopub.execute_input":"2023-06-10T16:41:22.687785Z","iopub.status.idle":"2023-06-10T16:41:22.870791Z","shell.execute_reply.started":"2023-06-10T16:41:22.687753Z","shell.execute_reply":"2023-06-10T16:41:22.869720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"limits = {'0-4':(1,4), '5-12':(4,14), '13-22':(14,19)}\n\ncounter = 0\nsession_id_level_map = {}\nsession_id_q_session_level_map = {}\nsession_levels = []\nfor session_id, level_group in tqdm(train[[\"session_id\", \"level_group\"]].values):\n    # Maintain the label encoding of (session_id and level group)\n    if (session_id, level_group) not in session_id_level_map:\n        session_id_level_map[(session_id, level_group)] = counter\n        counter += 1\n    \n    session_level = session_id_level_map[(session_id, level_group)]\n    session_levels.append(session_level)\n    \n    for q in range(limits[level_group][0], limits[level_group][1]):\n        session_id_q_session_level_map[(session_id, q)] = session_level","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:41:22.872094Z","iopub.execute_input":"2023-06-10T16:41:22.872441Z","iopub.status.idle":"2023-06-10T16:43:47.214376Z","shell.execute_reply.started":"2023-06-10T16:41:22.872412Z","shell.execute_reply":"2023-06-10T16:43:47.212703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"session_level\"] = session_levels\ntrain[\"session_level\"] = train[\"session_level\"].astype(np.int32)\ntrain.session_level.min(), train.session_level.max()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:43:47.216115Z","iopub.execute_input":"2023-06-10T16:43:47.216623Z","iopub.status.idle":"2023-06-10T16:44:00.541975Z","shell.execute_reply.started":"2023-06-10T16:43:47.216582Z","shell.execute_reply":"2023-06-10T16:44:00.540732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save as the test dataframe\ntrain.to_parquet(\"./session_level_encoded_train.pq\")","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:44:00.543702Z","iopub.execute_input":"2023-06-10T16:44:00.544294Z","iopub.status.idle":"2023-06-10T16:44:03.623720Z","shell.execute_reply.started":"2023-06-10T16:44:00.544218Z","shell.execute_reply":"2023-06-10T16:44:03.622765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the maps\nimport pickle\n\nwith open('maps.pkl', 'wb') as handle:\n    pickle.dump({\n        \"salltrueession_levels\": session_levels,\n        \"session_id_level_map\": session_id_level_map,\n        \"session_id_q_session_level_map\": session_id_q_session_level_map,\n    }, handle)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:44:03.626808Z","iopub.execute_input":"2023-06-10T16:44:03.629438Z","iopub.status.idle":"2023-06-10T16:44:05.106036Z","shell.execute_reply.started":"2023-06-10T16:44:03.629393Z","shell.execute_reply":"2023-06-10T16:44:05.104404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del train\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:44:05.111701Z","iopub.execute_input":"2023-06-10T16:44:05.112110Z","iopub.status.idle":"2023-06-10T16:44:05.401691Z","shell.execute_reply.started":"2023-06-10T16:44:05.112081Z","shell.execute_reply":"2023-06-10T16:44:05.400499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv(\"/kaggle/input/predict-student-performance-from-game-play/sample_submission.csv\").head(10)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:44:05.403376Z","iopub.execute_input":"2023-06-10T16:44:05.403799Z","iopub.status.idle":"2023-06-10T16:44:05.427245Z","shell.execute_reply.started":"2023-06-10T16:44:05.403748Z","shell.execute_reply":"2023-06-10T16:44:05.426345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a sample submission file for train data\nsample_rows = []\n\nfor q in range(1, 19):    \n    for session_id in session_ids:\n        session_level = session_id_q_session_level_map[(session_id, q)]\n        sample_rows.append({\n            \"session_id\": f\"{session_id}_q{q}\",\n            \"correct\": 0,\n            \"session_level\": session_level\n        })","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:44:05.428618Z","iopub.execute_input":"2023-06-10T16:44:05.429455Z","iopub.status.idle":"2023-06-10T16:44:06.213930Z","shell.execute_reply.started":"2023-06-10T16:44:05.429417Z","shell.execute_reply":"2023-06-10T16:44:06.212474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = pd.DataFrame(sample_rows)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:44:06.219045Z","iopub.execute_input":"2023-06-10T16:44:06.219517Z","iopub.status.idle":"2023-06-10T16:44:06.991599Z","shell.execute_reply.started":"2023-06-10T16:44:06.219484Z","shell.execute_reply":"2023-06-10T16:44:06.990336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub.to_csv(\"train_sample_submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:44:06.994239Z","iopub.execute_input":"2023-06-10T16:44:06.994697Z","iopub.status.idle":"2023-06-10T16:44:08.477314Z","shell.execute_reply.started":"2023-06-10T16:44:06.994661Z","shell.execute_reply":"2023-06-10T16:44:08.476220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:44:08.479419Z","iopub.execute_input":"2023-06-10T16:44:08.480298Z","iopub.status.idle":"2023-06-10T16:44:08.492614Z","shell.execute_reply.started":"2023-06-10T16:44:08.480262Z","shell.execute_reply":"2023-06-10T16:44:08.491139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(session_ids)","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:44:08.494888Z","iopub.execute_input":"2023-06-10T16:44:08.495378Z","iopub.status.idle":"2023-06-10T16:44:08.508034Z","shell.execute_reply.started":"2023-06-10T16:44:08.495340Z","shell.execute_reply":"2023-06-10T16:44:08.506731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-10T16:44:08.509950Z","iopub.execute_input":"2023-06-10T16:44:08.510345Z","iopub.status.idle":"2023-06-10T16:44:08.520341Z","shell.execute_reply.started":"2023-06-10T16:44:08.510313Z","shell.execute_reply":"2023-06-10T16:44:08.519268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}