{"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":"from pathlib import Path\nfrom collections import defaultdict\nfrom collections import Counter\n\nimport numpy as np\nimport pandas as pd\n\nfrom tqdm.notebook import tqdm","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-02T15:54:03.188325Z","iopub.execute_input":"2022-11-02T15:54:03.189150Z","iopub.status.idle":"2022-11-02T15:54:03.336100Z","shell.execute_reply.started":"2022-11-02T15:54:03.189026Z","shell.execute_reply":"2022-11-02T15:54:03.334812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT = Path.cwd().parent\nINPUT = ROOT / \"input\"\nDATA = INPUT / \"otto-recommender-system/\"\n\nfor f in DATA.iterdir():\n    print(f.name)","metadata":{"execution":{"iopub.status.busy":"2022-11-02T15:54:03.337848Z","iopub.execute_input":"2022-11-02T15:54:03.338280Z","iopub.status.idle":"2022-11-02T15:54:03.346901Z","shell.execute_reply.started":"2022-11-02T15:54:03.338248Z","shell.execute_reply":"2022-11-02T15:54:03.345478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"last_aid_list = []\ntest_sessions = pd.read_json(DATA / 'test.jsonl', lines=True, chunksize=100000)\n\nfor chunk in tqdm(test_sessions):\n    \n    for session_id, events in chunk.values:\n        last_aid_list.append([session_id, events[-1][\"aid\"]])","metadata":{"execution":{"iopub.status.busy":"2022-11-02T15:56:33.323092Z","iopub.execute_input":"2022-11-02T15:56:33.323535Z","iopub.status.idle":"2022-11-02T15:57:02.079889Z","shell.execute_reply.started":"2022-11-02T15:56:33.323498Z","shell.execute_reply":"2022-11-02T15:57:02.078464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_list = []\nfor session_id, last_aid in last_aid_list:\n    last_aid_20_times = \" \".join([str(last_aid)] * 20)\n    data_list.append([f\"{session_id}_clicks\", last_aid_20_times])\n    data_list.append([f\"{session_id}_carts\", last_aid_20_times])\n    data_list.append([f\"{session_id}_orders\", last_aid_20_times])","metadata":{"execution":{"iopub.status.busy":"2022-11-02T15:58:05.415319Z","iopub.execute_input":"2022-11-02T15:58:05.416574Z","iopub.status.idle":"2022-11-02T15:58:14.707045Z","shell.execute_reply.started":"2022-11-02T15:58:05.416519Z","shell.execute_reply":"2022-11-02T15:58:14.705755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame(\n    data_list, columns=[\"session_type\", \"labels\"])\nsub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-02T15:58:17.664344Z","iopub.execute_input":"2022-11-02T15:58:17.664799Z","iopub.status.idle":"2022-11-02T15:58:41.006016Z","shell.execute_reply.started":"2022-11-02T15:58:17.664761Z","shell.execute_reply":"2022-11-02T15:58:41.004478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-02T15:58:41.009125Z","iopub.execute_input":"2022-11-02T15:58:41.009691Z","iopub.status.idle":"2022-11-02T15:58:41.029352Z","shell.execute_reply.started":"2022-11-02T15:58:41.009625Z","shell.execute_reply":"2022-11-02T15:58:41.027988Z"},"trusted":true},"execution_count":null,"outputs":[]}]}