{"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":"markdown","source":"# Simple aid Frequency Baseline","metadata":{}},{"cell_type":"code","source":"import json\nfrom 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\nimport lightgbm as lgb","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-01T23:40:03.644184Z","iopub.execute_input":"2022-11-01T23:40:03.644677Z","iopub.status.idle":"2022-11-01T23:40:04.547559Z","shell.execute_reply.started":"2022-11-01T23:40:03.644581Z","shell.execute_reply":"2022-11-01T23:40:04.546416Z"},"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-01T23:40:17.630443Z","iopub.execute_input":"2022-11-01T23:40:17.630860Z","iopub.status.idle":"2022-11-01T23:40:17.637322Z","shell.execute_reply.started":"2022-11-01T23:40:17.630830Z","shell.execute_reply":"2022-11-01T23:40:17.636324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### count aid frequency by session ","metadata":{}},{"cell_type":"code","source":"sorted_ids_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        aid_list = []\n        for action in events:\n            aid_list.append(action[\"aid\"])\n\n        cnt = Counter(aid_list)\n        sorted_aids = sorted(set(aid_list), key=lambda x: cnt[x], reverse=True)\n        sorted_ids_list.append([session_id, sorted_aids])","metadata":{"execution":{"iopub.status.busy":"2022-11-02T00:04:17.479072Z","iopub.execute_input":"2022-11-02T00:04:17.479468Z","iopub.status.idle":"2022-11-02T00:05:12.188430Z","shell.execute_reply.started":"2022-11-02T00:04:17.479440Z","shell.execute_reply":"2022-11-02T00:05:12.187259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### make submission\n\nIn this submission, I use the same `aid` sequence for `{session_id}_clicks`, `{session_id}_carts`, `{session_id}_orders`.","metadata":{}},{"cell_type":"code","source":"data_list = []\nfor session_id, sorted_aids in sorted_ids_list:\n    sorted_aids_20_str = \" \".join(map(str, sorted_aids[:20]))\n    data_list.append([f\"{session_id}_clicks\", sorted_aids_20_str])\n    data_list.append([f\"{session_id}_carts\", sorted_aids_20_str])\n    data_list.append([f\"{session_id}_orders\", sorted_aids_20_str])","metadata":{"execution":{"iopub.status.busy":"2022-11-02T00:08:20.227885Z","iopub.execute_input":"2022-11-02T00:08:20.228293Z","iopub.status.idle":"2022-11-02T00:08:34.816499Z","shell.execute_reply.started":"2022-11-02T00:08:20.228259Z","shell.execute_reply":"2022-11-02T00:08:34.814970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.DataFrame(\n    data_list, columns=[\"session_type\", \"labels\"])","metadata":{"execution":{"iopub.status.busy":"2022-11-02T00:08:42.006958Z","iopub.execute_input":"2022-11-02T00:08:42.007393Z","iopub.status.idle":"2022-11-02T00:08:43.072241Z","shell.execute_reply.started":"2022-11-02T00:08:42.007357Z","shell.execute_reply":"2022-11-02T00:08:43.070291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv(\"submission.csv\", index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head()","metadata":{},"execution_count":null,"outputs":[]}]}