{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":38760,"databundleVersionId":4493939,"sourceType":"competition"}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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","trusted":true,"execution":{"iopub.status.busy":"2025-06-17T11:49:59.518167Z","iopub.execute_input":"2025-06-17T11:49:59.518467Z","iopub.status.idle":"2025-06-17T11:50:01.518941Z","shell.execute_reply.started":"2025-06-17T11:49:59.518444Z","shell.execute_reply":"2025-06-17T11:50:01.518057Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nfrom pathlib import Path\nimport os\nimport random\nimport numpy as np\nimport json\nfrom datetime import timedelta\nfrom collections import Counter\nfrom tqdm.notebook import tqdm\nfrom heapq import nlargest\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set_theme()\n\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T11:50:01.520404Z","iopub.execute_input":"2025-06-17T11:50:01.520847Z","iopub.status.idle":"2025-06-17T11:50:03.116686Z","shell.execute_reply.started":"2025-06-17T11:50:01.520823Z","shell.execute_reply":"2025-06-17T11:50:03.115546Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DATA_PATH = Path('../input/otto-recommender-system')\nTRAIN_PATH = DATA_PATH/'train.jsonl'\nTEST_PATH = DATA_PATH/'test.jsonl'\nSAMPLE_SUB_PATH = Path('../input/otto-recommender-system/sample_submission.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T11:50:03.117712Z","iopub.execute_input":"2025-06-17T11:50:03.118113Z","iopub.status.idle":"2025-06-17T11:50:03.123920Z","shell.execute_reply.started":"2025-06-17T11:50:03.118090Z","shell.execute_reply":"2025-06-17T11:50:03.122702Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission = pd.read_csv(SAMPLE_SUB_PATH)\nsample_submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T11:50:03.124801Z","iopub.execute_input":"2025-06-17T11:50:03.125132Z","iopub.status.idle":"2025-06-17T11:50:09.494166Z","shell.execute_reply.started":"2025-06-17T11:50:03.125106Z","shell.execute_reply":"2025-06-17T11:50:09.493390Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_size = 150000\n\nchunks = pd.read_json(TRAIN_PATH, lines=True, chunksize = sample_size)\n\nclicks_article_list = []\ncarts_article_list = []\norders_article_list = []\n\nfor e, c in enumerate(chunks):\n    \n    if e > 2:\n        break\n    \n    sample_train_df = c\n    \n    for i, row in c.iterrows():\n        actions = row['events']\n        for action in actions:\n            if action['type'] == 'clicks':\n                clicks_article_list.append(action['aid'])\n            elif action['type'] == 'carts':\n                carts_article_list.append(action['aid'])\n            else:\n                orders_article_list.append(action['aid'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T11:50:09.495968Z","iopub.execute_input":"2025-06-17T11:50:09.496221Z","iopub.status.idle":"2025-06-17T11:51:29.866686Z","shell.execute_reply.started":"2025-06-17T11:50:09.496201Z","shell.execute_reply":"2025-06-17T11:51:29.865800Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"article_click_freq = Counter(clicks_article_list)\narticle_carts_freq = Counter(carts_article_list)\narticle_order_freq = Counter(orders_article_list)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T11:51:29.867660Z","iopub.execute_input":"2025-06-17T11:51:29.868122Z","iopub.status.idle":"2025-06-17T11:51:36.584559Z","shell.execute_reply.started":"2025-06-17T11:51:29.868088Z","shell.execute_reply":"2025-06-17T11:51:36.583773Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"top_click_article = nlargest(20, article_click_freq, key = article_click_freq.get)\ntop_carts_article = nlargest(20, article_carts_freq, key = article_carts_freq.get)\ntop_order_article = nlargest(20, article_order_freq, key = article_order_freq.get) ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T11:51:36.585577Z","iopub.execute_input":"2025-06-17T11:51:36.585901Z","iopub.status.idle":"2025-06-17T11:51:37.020716Z","shell.execute_reply.started":"2025-06-17T11:51:36.585872Z","shell.execute_reply":"2025-06-17T11:51:37.019957Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"frequent_articles = {'clicks': top_click_article, 'carts':top_carts_article, 'order':top_order_article}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T11:51:37.021579Z","iopub.execute_input":"2025-06-17T11:51:37.021846Z","iopub.status.idle":"2025-06-17T11:51:37.026371Z","shell.execute_reply.started":"2025-06-17T11:51:37.021822Z","shell.execute_reply":"2025-06-17T11:51:37.025388Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data = pd.read_json(TEST_PATH, lines=True, chunksize=1000)\n\npreds = []\n\nfor chunk in tqdm(test_data, total=1671):\n    \n    for i, row in chunk.iterrows():\n        actions = row['events']\n        article_id_list = []\n        for action in actions:\n            article_id_list.append(action['aid'])\n            \n        article_freq = Counter(article_id_list)\n        top_articles = nlargest(20, article_freq, key = article_freq.get)\n        \n        padding_size = (20 - len(top_articles))\n        for action in ['clicks', 'carts', 'order']:\n            top_articles_added = top_articles + frequent_articles[action][:padding_size]\n            preds.append(\" \".join([str(id) for id in top_articles_added]))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T11:51:37.027406Z","iopub.execute_input":"2025-06-17T11:51:37.027817Z","iopub.status.idle":"2025-06-17T11:54:26.166689Z","shell.execute_reply.started":"2025-06-17T11:51:37.027762Z","shell.execute_reply":"2025-06-17T11:54:26.165674Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission['labels'] = preds","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T11:54:26.167631Z","iopub.execute_input":"2025-06-17T11:54:26.167921Z","iopub.status.idle":"2025-06-17T11:54:27.000069Z","shell.execute_reply.started":"2025-06-17T11:54:26.167902Z","shell.execute_reply":"2025-06-17T11:54:26.999199Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T11:54:27.000995Z","iopub.execute_input":"2025-06-17T11:54:27.001369Z","iopub.status.idle":"2025-06-17T11:54:55.031374Z","shell.execute_reply.started":"2025-06-17T11:54:27.001341Z","shell.execute_reply":"2025-06-17T11:54:55.030491Z"}},"outputs":[],"execution_count":null}]}