{"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":"## Resources:\n\"\"\" \nhttps://www.kaggle.com/code/vslaykovsky/co-visitation-matrix\nhttps://www.kaggle.com/code/radek1/co-visitation-matrix-simplified-imprvd-logic\nhttps://medium.com/nvidia-merlin/transformers4rec-4523cc7d8fa8\n\"\"\"","metadata":{}},{"cell_type":"code","source":"## Resources:\n\"\"\" \nhttps://www.kaggle.com/code/vslaykovsky/co-visitation-matrix\nhttps://www.kaggle.com/code/radek1/co-visitation-matrix-simplified-imprvd-logic\nhttps://medium.com/nvidia-merlin/transformers4rec-4523cc7d8fa8\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-11-07T14:53:59.444687Z","iopub.execute_input":"2022-11-07T14:53:59.445166Z","iopub.status.idle":"2022-11-07T14:53:59.475113Z","shell.execute_reply.started":"2022-11-07T14:53:59.445075Z","shell.execute_reply":"2022-11-07T14:53:59.474100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-07T14:53:59.476793Z","iopub.execute_input":"2022-11-07T14:53:59.477586Z","iopub.status.idle":"2022-11-07T14:54:01.748434Z","shell.execute_reply.started":"2022-11-07T14:53:59.477551Z","shell.execute_reply":"2022-11-07T14:54:01.747207Z"},"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-07T14:54:01.750467Z","iopub.execute_input":"2022-11-07T14:54:01.751002Z","iopub.status.idle":"2022-11-07T14:54:01.759239Z","shell.execute_reply.started":"2022-11-07T14:54:01.750932Z","shell.execute_reply":"2022-11-07T14:54:01.757673Z"},"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-07T14:54:01.760885Z","iopub.execute_input":"2022-11-07T14:54:01.761461Z","iopub.status.idle":"2022-11-07T14:54:48.416290Z","shell.execute_reply.started":"2022-11-07T14:54:01.761423Z","shell.execute_reply":"2022-11-07T14:54:48.414867Z"},"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-07T14:54:48.419258Z","iopub.execute_input":"2022-11-07T14:54:48.419618Z","iopub.status.idle":"2022-11-07T14:55:00.254698Z","shell.execute_reply.started":"2022-11-07T14:54:48.419587Z","shell.execute_reply":"2022-11-07T14:55:00.253810Z"},"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-07T14:55:00.256364Z","iopub.execute_input":"2022-11-07T14:55:00.256714Z","iopub.status.idle":"2022-11-07T14:55:01.358416Z","shell.execute_reply.started":"2022-11-07T14:55:00.256683Z","shell.execute_reply":"2022-11-07T14:55:01.357136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-07T14:55:01.359816Z","iopub.execute_input":"2022-11-07T14:55:01.360272Z","iopub.status.idle":"2022-11-07T14:55:09.693188Z","shell.execute_reply.started":"2022-11-07T14:55:01.360238Z","shell.execute_reply":"2022-11-07T14:55:09.691952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-07T14:55:09.694943Z","iopub.execute_input":"2022-11-07T14:55:09.695334Z","iopub.status.idle":"2022-11-07T14:55:09.710181Z","shell.execute_reply.started":"2022-11-07T14:55:09.695304Z","shell.execute_reply":"2022-11-07T14:55:09.708962Z"},"trusted":true},"execution_count":null,"outputs":[]}]}