{"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":"!pip install recbole -q","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","id":"qTLWGJQQSTSI","outputId":"bef54cbf-2239-4656-a04d-0a2b25eceba2","execution":{"iopub.status.busy":"2022-04-11T01:37:57.823359Z","iopub.execute_input":"2022-04-11T01:37:57.824234Z","iopub.status.idle":"2022-04-11T01:38:08.456425Z","shell.execute_reply.started":"2022-04-11T01:37:57.824187Z","shell.execute_reply":"2022-04-11T01:38:08.455561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nfrom path import Path\nimport gc\nfrom tqdm import tqdm","metadata":{"id":"kd_XhqwfSTSM","execution":{"iopub.status.busy":"2022-04-11T01:38:08.460051Z","iopub.execute_input":"2022-04-11T01:38:08.460287Z","iopub.status.idle":"2022-04-11T01:38:08.465472Z","shell.execute_reply.started":"2022-04-11T01:38:08.460258Z","shell.execute_reply":"2022-04-11T01:38:08.46437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path(\"../input/h-and-m-personalized-fashion-recommendations\")\noutput_path = Path(\"/kaggle/working/hm\")","metadata":{"id":"nVt27qeQWwX4","execution":{"iopub.status.busy":"2022-04-11T01:38:08.466844Z","iopub.execute_input":"2022-04-11T01:38:08.467553Z","iopub.status.idle":"2022-04-11T01:38:08.476088Z","shell.execute_reply.started":"2022-04-11T01:38:08.467516Z","shell.execute_reply":"2022-04-11T01:38:08.475189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir {str(output_path)}","metadata":{"execution":{"iopub.status.busy":"2022-04-11T01:38:08.4784Z","iopub.execute_input":"2022-04-11T01:38:08.479202Z","iopub.status.idle":"2022-04-11T01:38:09.400021Z","shell.execute_reply.started":"2022-04-11T01:38:08.479142Z","shell.execute_reply":"2022-04-11T01:38:09.399171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## creating \"hm.inter\" from \"transactions_train.csv\"","metadata":{"id":"904Vkcd-STSN"}},{"cell_type":"code","source":"%%time\ndf_inter = pd.read_csv(\n    path/\"transactions_train.csv\", \n    dtype={\"article_id\": str}\n)\ndf_inter.head()","metadata":{"id":"nthFQxKISTSO","outputId":"1274f812-728d-4721-926b-4e1e449e70da","execution":{"iopub.status.busy":"2022-04-11T01:38:09.402697Z","iopub.execute_input":"2022-04-11T01:38:09.403307Z","iopub.status.idle":"2022-04-11T01:38:11.537901Z","shell.execute_reply.started":"2022-04-11T01:38:09.403274Z","shell.execute_reply":"2022-04-11T01:38:11.537187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_inter.shape","metadata":{"id":"2KufSkyZSTSP","outputId":"cc8547e0-f9e7-4564-dacc-26c19c614de4","execution":{"iopub.status.busy":"2022-04-11T01:38:11.539326Z","iopub.execute_input":"2022-04-11T01:38:11.539597Z","iopub.status.idle":"2022-04-11T01:38:11.56528Z","shell.execute_reply.started":"2022-04-11T01:38:11.53956Z","shell.execute_reply":"2022-04-11T01:38:11.557601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_inter.dtypes","metadata":{"id":"JskuZf5cSTSP","outputId":"e30f95fe-0a9e-4808-f06c-fb18eab6f034","execution":{"iopub.status.busy":"2022-04-11T01:38:11.56645Z","iopub.status.idle":"2022-04-11T01:38:11.56725Z","shell.execute_reply.started":"2022-04-11T01:38:11.567007Z","shell.execute_reply":"2022-04-11T01:38:11.567032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# popular items between August and September\npopular_items = df_inter[df_inter.t_dat.str[5:7].isin([\"08\", \"09\"])].article_id.value_counts()[:12].index.tolist()\npopular_items","metadata":{"id":"kfa6v5o_STSQ","outputId":"822c17b9-29af-4c7f-86ee-1c23fd03f0b8","execution":{"iopub.status.busy":"2022-04-11T01:38:11.568655Z","iopub.status.idle":"2022-04-11T01:38:11.569077Z","shell.execute_reply.started":"2022-04-11T01:38:11.568843Z","shell.execute_reply":"2022-04-11T01:38:11.568866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_inter = df_inter.tail(4_000_000)\ndf_inter.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-11T01:38:11.570412Z","iopub.status.idle":"2022-04-11T01:38:11.570841Z","shell.execute_reply.started":"2022-04-11T01:38:11.570615Z","shell.execute_reply":"2022-04-11T01:38:11.570638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n# t_dat's type is object(=str), but what I want is timestamp\n# so convert str like \"2018-09-20\" to datetime64[s]\n# and then, convert it to timestamp like 1537401600\ndf_inter[\"timestamp\"] = df_inter.t_dat.values.astype('datetime64[s]').astype(np.int64)\ndf_inter[\"timestamp\"].head()","metadata":{"id":"zYI03mHWSTSR","outputId":"7d1a5afa-c340-40e1-fc9f-6cec38843c30","execution":{"iopub.status.busy":"2022-04-11T01:38:11.572193Z","iopub.status.idle":"2022-04-11T01:38:11.572617Z","shell.execute_reply.started":"2022-04-11T01:38:11.572379Z","shell.execute_reply":"2022-04-11T01:38:11.572401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_inter = df_inter[[\"customer_id\", \"article_id\", \"timestamp\"]]","metadata":{"id":"13TreMnnSTSS","execution":{"iopub.status.busy":"2022-04-11T01:38:11.573947Z","iopub.status.idle":"2022-04-11T01:38:11.574351Z","shell.execute_reply.started":"2022-04-11T01:38:11.574131Z","shell.execute_reply":"2022-04-11T01:38:11.574153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# rename columns\n# RecBole can handle implicit feedback recommendation!\n# so no column for \"rating\"\ndf_inter = df_inter.rename(columns={\n    \"customer_id\": \"user_id:token\",\n    \"article_id\": \"item_id:token\",\n    \"timestamp\": \"timestamp:float\"\n})\ndf_inter.head()","metadata":{"id":"rRz5fgdkSTSS","outputId":"56105383-cbc3-4ea6-a54d-83cabafa8554","execution":{"iopub.status.busy":"2022-04-11T01:38:11.575696Z","iopub.status.idle":"2022-04-11T01:38:11.576114Z","shell.execute_reply.started":"2022-04-11T01:38:11.575881Z","shell.execute_reply":"2022-04-11T01:38:11.575912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_inter.to_csv(output_path/\"hm.inter\", index=None, sep=\"\\t\")","metadata":{"id":"wQCLSfkjSTST","execution":{"iopub.status.busy":"2022-04-11T01:38:11.577587Z","iopub.status.idle":"2022-04-11T01:38:11.578008Z","shell.execute_reply.started":"2022-04-11T01:38:11.577773Z","shell.execute_reply":"2022-04-11T01:38:11.577796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head {output_path/\"hm.inter\"}","metadata":{"id":"eSDLqyhQGy3j","outputId":"8bbaba4e-1170-4b10-a69f-d4441a8154f8","execution":{"iopub.status.busy":"2022-04-11T01:38:11.579315Z","iopub.status.idle":"2022-04-11T01:38:11.579738Z","shell.execute_reply.started":"2022-04-11T01:38:11.579514Z","shell.execute_reply":"2022-04-11T01:38:11.579537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## creating \"hm.user\" from \"customers.csv\"","metadata":{"id":"VPsOeZXZSTSU"}},{"cell_type":"code","source":"%%time\ndf_user = pd.read_csv(path/\"customers.csv\")\ndf_user.head()","metadata":{"id":"IskFSkP0STSU","outputId":"9dda0ecd-5668-4af7-bfe4-31e4cbc5e3ec","execution":{"iopub.status.busy":"2022-04-11T01:38:11.581067Z","iopub.status.idle":"2022-04-11T01:38:11.581471Z","shell.execute_reply.started":"2022-04-11T01:38:11.581251Z","shell.execute_reply":"2022-04-11T01:38:11.581273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_user.shape","metadata":{"id":"SJttHOIkSTSU","outputId":"fc810c50-983a-4c41-91de-de86ff2ae4a6","execution":{"iopub.status.busy":"2022-04-11T01:38:11.582989Z","iopub.status.idle":"2022-04-11T01:38:11.583398Z","shell.execute_reply.started":"2022-04-11T01:38:11.583176Z","shell.execute_reply":"2022-04-11T01:38:11.583199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_user.dtypes","metadata":{"id":"GpTup4OkSTSV","outputId":"be007420-89a4-44e8-cc63-d4e0b83a4772","execution":{"iopub.status.busy":"2022-04-11T01:38:11.584722Z","iopub.status.idle":"2022-04-11T01:38:11.585141Z","shell.execute_reply.started":"2022-04-11T01:38:11.584915Z","shell.execute_reply":"2022-04-11T01:38:11.584939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_user.postal_code.nunique()","metadata":{"id":"h157v25OSTSV","outputId":"eee8ea59-b54a-4ad3-d3b9-6ce371e2af99","execution":{"iopub.status.busy":"2022-04-11T01:38:11.586601Z","iopub.status.idle":"2022-04-11T01:38:11.587046Z","shell.execute_reply.started":"2022-04-11T01:38:11.586795Z","shell.execute_reply":"2022-04-11T01:38:11.586818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_user = df_user[[\"customer_id\", \"age\", \"postal_code\"]]","metadata":{"id":"vOZ7keK5STSV","execution":{"iopub.status.busy":"2022-04-11T01:38:11.58833Z","iopub.status.idle":"2022-04-11T01:38:11.588751Z","shell.execute_reply.started":"2022-04-11T01:38:11.588526Z","shell.execute_reply":"2022-04-11T01:38:11.588549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_user = df_user.rename(columns={\n    \"customer_id\": \"user_id:token\",\n    \"age\": \"age:float\",\n    \"postal_code\": \"postal_code:token\"\n})\ndf_user.head()","metadata":{"id":"CN52uHNDSTSW","outputId":"7622fd11-379e-4ded-b093-b6b4dff8e16d","execution":{"iopub.status.busy":"2022-04-11T01:38:11.590216Z","iopub.status.idle":"2022-04-11T01:38:11.59064Z","shell.execute_reply.started":"2022-04-11T01:38:11.590402Z","shell.execute_reply":"2022-04-11T01:38:11.590424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_user.to_csv(output_path/\"hm.user\", index=None, sep=\"\\t\")","metadata":{"id":"J06U99VcSTSW","execution":{"iopub.status.busy":"2022-04-11T01:38:11.592082Z","iopub.status.idle":"2022-04-11T01:38:11.592487Z","shell.execute_reply.started":"2022-04-11T01:38:11.592265Z","shell.execute_reply":"2022-04-11T01:38:11.592289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head {output_path/\"hm.user\"}","metadata":{"id":"m1PSVPpMGrss","outputId":"1423c92f-7a5b-40e5-e708-cb5bc5142858","execution":{"iopub.status.busy":"2022-04-11T01:38:11.593793Z","iopub.status.idle":"2022-04-11T01:38:11.594208Z","shell.execute_reply.started":"2022-04-11T01:38:11.593986Z","shell.execute_reply":"2022-04-11T01:38:11.594009Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## creating \"hm.item\" from \"articles.csv\"","metadata":{"id":"f8Ysh3-dSTSW"}},{"cell_type":"code","source":"%%time\ndf_item = pd.read_csv(path/\"articles.csv\", dtype={\"article_id\": str})\ndf_item.head()","metadata":{"id":"tryDFn_CSTSX","outputId":"ec85105a-6b9e-4ba0-f0ab-d1b3c1a87a8e","execution":{"iopub.status.busy":"2022-04-11T01:38:11.595553Z","iopub.status.idle":"2022-04-11T01:38:11.595969Z","shell.execute_reply.started":"2022-04-11T01:38:11.595736Z","shell.execute_reply":"2022-04-11T01:38:11.595758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_item.iloc[0]","metadata":{"execution":{"iopub.status.busy":"2022-04-11T01:38:11.597296Z","iopub.status.idle":"2022-04-11T01:38:11.597719Z","shell.execute_reply.started":"2022-04-11T01:38:11.597482Z","shell.execute_reply":"2022-04-11T01:38:11.59752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_item.shape","metadata":{"id":"dtFqW0oKSTSX","outputId":"796c627d-c7d7-4f3d-ed83-efe08ffa0da8","execution":{"iopub.status.busy":"2022-04-11T01:38:11.599031Z","iopub.status.idle":"2022-04-11T01:38:11.599436Z","shell.execute_reply.started":"2022-04-11T01:38:11.599214Z","shell.execute_reply":"2022-04-11T01:38:11.599238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_item.dtypes","metadata":{"id":"uabymgg6STSX","outputId":"24f2b029-4bd0-40c0-ee9b-09c49967bb20","execution":{"iopub.status.busy":"2022-04-11T01:38:11.60091Z","iopub.status.idle":"2022-04-11T01:38:11.601322Z","shell.execute_reply.started":"2022-04-11T01:38:11.601098Z","shell.execute_reply":"2022-04-11T01:38:11.601121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_item = df_item[[\n    \"article_id\",\n    \"product_type_no\",\n    \"product_group_name\",\n    \"colour_group_code\",\n    \"perceived_colour_value_id\",\n    \"index_group_no\",\n]]","metadata":{"id":"JSDYQjSASTSX","execution":{"iopub.status.busy":"2022-04-11T01:38:11.60274Z","iopub.status.idle":"2022-04-11T01:38:11.603153Z","shell.execute_reply.started":"2022-04-11T01:38:11.602932Z","shell.execute_reply":"2022-04-11T01:38:11.602954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_item = df_item.rename(columns={\n    \"article_id\": \"item_id:token\",\n    \"product_type_no\": \"product_type_no:float\",\n    \"product_group_name\": \"product_group_name:token_seq\",\n    \"colour_group_code\": \"colour_group_code:token\",\n    \"perceived_colour_value_id\": \"perceived_colour_value_id:token\",\n    \"index_group_no\": \"index_group_no:token\",\n})\ndf_item.head()","metadata":{"id":"HJm4N-BISTSY","outputId":"a7d91736-903f-46b0-bd36-d195b8d5e241","execution":{"iopub.status.busy":"2022-04-11T01:38:11.60526Z","iopub.status.idle":"2022-04-11T01:38:11.606196Z","shell.execute_reply.started":"2022-04-11T01:38:11.605965Z","shell.execute_reply":"2022-04-11T01:38:11.605988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_item.to_csv(output_path/\"hm.item\", index=None, sep=\"\\t\")","metadata":{"id":"3GqyJMJASTSY","execution":{"iopub.status.busy":"2022-04-11T01:38:11.607345Z","iopub.status.idle":"2022-04-11T01:38:11.608384Z","shell.execute_reply.started":"2022-04-11T01:38:11.608152Z","shell.execute_reply":"2022-04-11T01:38:11.608176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!head {output_path/\"hm.item\"}","metadata":{"id":"_BV12sIaAXZi","outputId":"2a62640b-b338-4555-c253-37bc343d90c0","execution":{"iopub.status.busy":"2022-04-11T01:38:11.609489Z","iopub.status.idle":"2022-04-11T01:38:11.610215Z","shell.execute_reply.started":"2022-04-11T01:38:11.609971Z","shell.execute_reply":"2022-04-11T01:38:11.609996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## delete dataframes","metadata":{"id":"itdAX-n2STSY"}},{"cell_type":"code","source":"del df_inter\ndel df_user\ndel df_item\n\ngc.collect()","metadata":{"id":"tpZQKfgRSTSZ","outputId":"2b84983f-3b11-40e1-8b84-04b9e5d492f1","execution":{"iopub.status.busy":"2022-04-11T01:38:11.611615Z","iopub.status.idle":"2022-04-11T01:38:11.612545Z","shell.execute_reply.started":"2022-04-11T01:38:11.612279Z","shell.execute_reply":"2022-04-11T01:38:11.612303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## using RecBole","metadata":{"id":"lDvWr7TMSTSZ"}},{"cell_type":"code","source":"import logging\nfrom logging import getLogger\n\nfrom recbole.config import Config\nfrom recbole.data import create_dataset, data_preparation\nfrom recbole.model.sequential_recommender import SASRecF\nfrom recbole.trainer import Trainer\nfrom recbole.utils import init_seed, init_logger\nfrom recbole.utils.case_study import full_sort_topk","metadata":{"id":"6MjGqycYSTSZ","execution":{"iopub.status.busy":"2022-04-11T01:38:11.613648Z","iopub.status.idle":"2022-04-11T01:38:11.614537Z","shell.execute_reply.started":"2022-04-11T01:38:11.61427Z","shell.execute_reply":"2022-04-11T01:38:11.614295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config_dict = {\n    \"data_path\": \"/kaggle/working\",\n    \"USER_ID_FIELD\": \"user_id\",\n    \"ITEM_ID_FIELD\": \"item_id\",\n    \"TIME_FIELD\": \"timestamp\",\n    \"user_inter_num_interval\": \"[2, inf)\",\n    \"item_inter_num_interval\": \"[50, inf)\",\n    \"load_col\": {\n        \"inter\": [\"user_id\", \"item_id\", \"timestamp\"],\n        \"user\": [\"user_id\", \"age\", \"postal_code\"],\n        \"item\": [\n            \"item_id\",\n            \"product_type_no\", \n            \"product_group_name\", \n            \"colour_group_code\",\n            \"perceived_colour_value_id\",\n            \"index_group_no\"\n        ]\n    }, \n    \"selected_features\": [\n        \"product_type_no\", \n        \"product_group_name\", \n        \"colour_group_code\", \n        \"perceived_colour_value_id\",\n        \"index_group_no\"\n    ],\n    \"neg_sampling\": None,\n    \"eval_args\": {\n        \"split\": {\"RS\": [0.95, 0.03, 0.02]},\n        \"group_by\": \"user\",\n        \"order\": \"TO\",\n        \"mode\": \"full\"\n    },\n    \"metrics\": [\"MRR\", \"Hit\", \"MAP\"],\n    \"topk\": 12,\n    \"epochs\": 30,\n    \"show_progress\": True,\n    \"valid_metric\": \"MAP@12\",\n    \"learning_rate\": 0.003,\n}","metadata":{"id":"av0cOecZSTSZ","execution":{"iopub.status.busy":"2022-04-11T01:38:11.615681Z","iopub.status.idle":"2022-04-11T01:38:11.616582Z","shell.execute_reply.started":"2022-04-11T01:38:11.616315Z","shell.execute_reply":"2022-04-11T01:38:11.616339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_name = \"SASRecF\"","metadata":{"execution":{"iopub.status.busy":"2022-04-11T01:38:11.617665Z","iopub.status.idle":"2022-04-11T01:38:11.618579Z","shell.execute_reply.started":"2022-04-11T01:38:11.618311Z","shell.execute_reply":"2022-04-11T01:38:11.618335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nprint(f\"training {model_name}\")\n\nconfig = Config(model=model_name, dataset='hm', config_dict=config_dict)\ninit_seed(config['seed'], config['reproducibility'])\n\ndataset = create_dataset(config)\ntrain_data, valid_data, test_data = data_preparation(config, dataset)\n\nmodel = eval(model_name)(config, train_data.dataset).to(config['device'])\ntrainer = Trainer(config, model)\n_, best_valid_result = trainer.fit(\n    train_data, \n    valid_data, \n    callback_fn=lambda epoch_idx, valid_score: print(f\"{config['valid_metric']} at epoch {epoch_idx + 1}: {valid_score}\")\n)\n\nprint(f\"result: {best_valid_result}\")","metadata":{"id":"efFsqjm9STSa","outputId":"a005041f-bd0c-46a3-e383-81c69602a68e","execution":{"iopub.status.busy":"2022-04-11T01:38:11.619676Z","iopub.status.idle":"2022-04-11T01:38:11.620598Z","shell.execute_reply.started":"2022-04-11T01:38:11.620313Z","shell.execute_reply":"2022-04-11T01:38:11.620337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainer.evaluate(test_data)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T01:38:11.621677Z","iopub.status.idle":"2022-04-11T01:38:11.622581Z","shell.execute_reply.started":"2022-04-11T01:38:11.622312Z","shell.execute_reply":"2022-04-11T01:38:11.622336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del dataset\ndel train_data\ndel valid_data\ndel test_data\n\ngc.collect()","metadata":{"id":"qr4UcGbiSTSa","execution":{"iopub.status.busy":"2022-04-11T01:38:11.623677Z","iopub.status.idle":"2022-04-11T01:38:11.624583Z","shell.execute_reply.started":"2022-04-11T01:38:11.624316Z","shell.execute_reply":"2022-04-11T01:38:11.62434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# making recommendations\nhttps://recbole.io/docs/user_guide/usage/case_study.html  \n\"Get the top ranked item for each user\" looks helpful","metadata":{"execution":{"iopub.status.busy":"2022-04-08T13:51:31.96169Z","iopub.execute_input":"2022-04-08T13:51:31.962018Z","iopub.status.idle":"2022-04-08T13:51:31.975012Z","shell.execute_reply.started":"2022-04-08T13:51:31.961975Z","shell.execute_reply":"2022-04-08T13:51:31.973711Z"},"id":"iKBv0ApTSTSb"}},{"cell_type":"code","source":"submission_df = pd.read_csv(path/\"sample_submission.csv\")\nsubmission_df.head()","metadata":{"id":"2252J9hNSTSb","outputId":"6ff75422-5c92-4f9f-807a-f1a39f3a0f21","execution":{"iopub.status.busy":"2022-04-11T01:38:11.625668Z","iopub.status.idle":"2022-04-11T01:38:11.62656Z","shell.execute_reply.started":"2022-04-11T01:38:11.626297Z","shell.execute_reply":"2022-04-11T01:38:11.626321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = submission_df.drop(\"prediction\", axis=1)\nsubmission_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-11T01:38:11.627656Z","iopub.status.idle":"2022-04-11T01:38:11.628548Z","shell.execute_reply.started":"2022-04-11T01:38:11.628274Z","shell.execute_reply":"2022-04-11T01:38:11.628298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config_dict[\"eval_args\"] = {\n    \"split\": {\"RS\": [0.0, 0.0, 1.0]},\n    \"group_by\": \"user\",\n    \"order\": \"TO\",\n    \"mode\": \"full\"\n}\nconfig = Config(model=model_name, dataset='hm', config_dict=config_dict)\ndataset = create_dataset(config)\ntrain_data, valid_data, test_data = data_preparation(config, dataset)\ntest_data.dataset","metadata":{"execution":{"iopub.status.busy":"2022-04-11T01:38:11.631609Z","iopub.status.idle":"2022-04-11T01:38:11.632462Z","shell.execute_reply.started":"2022-04-11T01:38:11.632215Z","shell.execute_reply":"2022-04-11T01:38:11.632239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"external_user_ids = dataset.id2token(\n    dataset.uid_field, list(range(dataset.user_num))\n)[1:]","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"topk_items = []\nfor internal_user_id in tqdm(list(range(dataset.user_num))[1:]):\n    _, topk_iid_list = full_sort_topk([internal_user_id], model, test_data, k=12, device=config['device'])\n    last_topk_iid_list = topk_iid_list[-1]\n    external_item_list = dataset.id2token(dataset.iid_field, last_topk_iid_list.cpu()).tolist()\n    topk_items.append(external_item_list)\nprint(len(topk_items))","metadata":{"id":"5A6oN69yM3fJ","execution":{"iopub.status.busy":"2022-04-11T01:38:11.633572Z","iopub.status.idle":"2022-04-11T01:38:11.634462Z","shell.execute_reply.started":"2022-04-11T01:38:11.634213Z","shell.execute_reply":"2022-04-11T01:38:11.634237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_recommendation = pd.DataFrame({\n    \"customer_id\": external_user_ids,\n    \"prediction\": [\" \".join(topk) for topk in topk_items]\n})\ndel topk_items\ndf_recommendation.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-11T01:38:38.39015Z","iopub.execute_input":"2022-04-11T01:38:38.390623Z","iopub.status.idle":"2022-04-11T01:38:38.776016Z","shell.execute_reply.started":"2022-04-11T01:38:38.390588Z","shell.execute_reply":"2022-04-11T01:38:38.775288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_recommendation.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-11T01:38:41.655264Z","iopub.execute_input":"2022-04-11T01:38:41.655841Z","iopub.status.idle":"2022-04-11T01:38:41.662978Z","shell.execute_reply.started":"2022-04-11T01:38:41.655801Z","shell.execute_reply":"2022-04-11T01:38:41.662187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = submission_df.merge(df_recommendation, how=\"left\", on=\"customer_id\")\ndel df_recommendation\n\nsubmission_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-11T01:38:11.64195Z","iopub.status.idle":"2022-04-11T01:38:11.642904Z","shell.execute_reply.started":"2022-04-11T01:38:11.642659Z","shell.execute_reply":"2022-04-11T01:38:11.642683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-11T01:38:11.644057Z","iopub.status.idle":"2022-04-11T01:38:11.64483Z","shell.execute_reply.started":"2022-04-11T01:38:11.644571Z","shell.execute_reply":"2022-04-11T01:38:11.644597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-04-11T01:38:11.64626Z","iopub.status.idle":"2022-04-11T01:38:11.647192Z","shell.execute_reply.started":"2022-04-11T01:38:11.646946Z","shell.execute_reply":"2022-04-11T01:38:11.646972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fill NA with most popular items in August and September\nsubmission_df.prediction = submission_df.prediction.fillna(\" \".join(popular_items))\nsubmission_df.isna().sum()","metadata":{"execution":{"iopub.status.busy":"2022-04-11T01:38:11.648349Z","iopub.status.idle":"2022-04-11T01:38:11.649283Z","shell.execute_reply.started":"2022-04-11T01:38:11.649039Z","shell.execute_reply":"2022-04-11T01:38:11.649065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-11T01:38:11.650447Z","iopub.status.idle":"2022-04-11T01:38:11.65135Z","shell.execute_reply.started":"2022-04-11T01:38:11.651108Z","shell.execute_reply":"2022-04-11T01:38:11.651133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv(\"submission.csv\", index=None)","metadata":{"execution":{"iopub.status.busy":"2022-04-11T01:38:11.652525Z","iopub.status.idle":"2022-04-11T01:38:11.653467Z","shell.execute_reply.started":"2022-04-11T01:38:11.653202Z","shell.execute_reply":"2022-04-11T01:38:11.653227Z"},"trusted":true},"execution_count":null,"outputs":[]}]}