{"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":"# 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\n\n\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","execution":{"iopub.status.busy":"2022-03-16T15:23:25.196822Z","iopub.execute_input":"2022-03-16T15:23:25.197059Z","iopub.status.idle":"2022-03-16T15:23:25.222235Z","shell.execute_reply.started":"2022-03-16T15:23:25.196996Z","shell.execute_reply":"2022-03-16T15:23:25.221630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cudf as cd\nimport cupy as cp\nimport cuml as cm\nimport pandas as pd\nimport sklearn\nimport numpy as np\ncustomers = cd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv')\ntransactions = cd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\narticles = cd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv')\nsample = cd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:25:29.089820Z","iopub.execute_input":"2022-03-16T15:25:29.090132Z","iopub.status.idle":"2022-03-16T15:26:22.208685Z","shell.execute_reply.started":"2022-03-16T15:25:29.090097Z","shell.execute_reply":"2022-03-16T15:26:22.207901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.astype({'article_id': 'str'}).dtypes\narticles.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:29:56.528140Z","iopub.execute_input":"2022-03-16T15:29:56.528409Z","iopub.status.idle":"2022-03-16T15:29:56.674071Z","shell.execute_reply.started":"2022-03-16T15:29:56.528379Z","shell.execute_reply":"2022-03-16T15:29:56.673413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = articles.drop(columns = ['product_type_name', 'graphical_appearance_name', 'colour_group_name', 'perceived_colour_value_name',\n                        'perceived_colour_master_name', 'index_name', 'index_group_name', 'section_name', \n                        'garment_group_name', 'prod_name', 'department_name', 'detail_desc'])\narticles.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:08:54.527000Z","iopub.execute_input":"2022-03-16T16:08:54.527256Z","iopub.status.idle":"2022-03-16T16:08:54.569076Z","shell.execute_reply.started":"2022-03-16T16:08:54.527228Z","shell.execute_reply":"2022-03-16T16:08:54.568436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = articles.rename(\n    columns={'article_id': 'item_id:token', 'product_code': 'product_code:token', 'product_type_no': 'product_type_no:float',\n             'product_group_name': 'product_group_name:token_seq', 'graphical_appearance_no': 'graphical_appearance_no:token', \n             'colour_group_code': 'colour_group_code:token', 'perceived_colour_value_id': 'perceived_colour_value_id:token', \n             'perceived_colour_master_id': 'perceived_colour_master_id:token', 'department_no': 'department_no:token', \n             'index_code': 'index_code:token', 'index_group_no': 'index_group_no:token', 'section_no': 'section_no:token', \n             'garment_group_no': 'garment_group_no:token'})\ntemp.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:08:56.811633Z","iopub.execute_input":"2022-03-16T16:08:56.812075Z","iopub.status.idle":"2022-03-16T16:08:56.852129Z","shell.execute_reply.started":"2022-03-16T16:08:56.812040Z","shell.execute_reply":"2022-03-16T16:08:56.851351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir /kaggle/working/recbox_data\ntemp.to_csv(r'/kaggle/working/recbox_data/recbox_data.item', index=False, sep='\\t')","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:09:23.665197Z","iopub.execute_input":"2022-03-16T16:09:23.665455Z","iopub.status.idle":"2022-03-16T16:09:24.462555Z","shell.execute_reply.started":"2022-03-16T16:09:23.665425Z","shell.execute_reply":"2022-03-16T16:09:24.461651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.astype({'article_id': 'str'}).dtypes\ntransactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:32:43.774360Z","iopub.execute_input":"2022-03-16T15:32:43.774646Z","iopub.status.idle":"2022-03-16T15:32:43.815217Z","shell.execute_reply.started":"2022-03-16T15:32:43.774609Z","shell.execute_reply":"2022-03-16T15:32:43.814481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['t_dat'] = cd.to_datetime(transactions['t_dat'], format=\"%Y-%m-%d\")","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:33:12.537541Z","iopub.execute_input":"2022-03-16T15:33:12.538085Z","iopub.status.idle":"2022-03-16T15:33:12.542863Z","shell.execute_reply.started":"2022-03-16T15:33:12.538049Z","shell.execute_reply":"2022-03-16T15:33:12.541463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:33:14.706029Z","iopub.execute_input":"2022-03-16T15:33:14.706623Z","iopub.status.idle":"2022-03-16T15:33:14.731162Z","shell.execute_reply.started":"2022-03-16T15:33:14.706570Z","shell.execute_reply":"2022-03-16T15:33:14.730468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions['timestamp'] = cd.to_numeric(transactions['t_dat'])\ntransactions['timestamp'] = transactions['timestamp']//10 ** 9\ntransactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:33:18.932377Z","iopub.execute_input":"2022-03-16T15:33:18.932681Z","iopub.status.idle":"2022-03-16T15:33:18.963249Z","shell.execute_reply.started":"2022-03-16T15:33:18.932647Z","shell.execute_reply":"2022-03-16T15:33:18.962515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions = transactions[transactions['timestamp']>1585620000]\ntransactions['customer_id'].to_string()\ntransactions.drop(columns=['t_dat','price','sales_channel_id'],inplace=True)\ntransactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:05:46.676436Z","iopub.execute_input":"2022-03-16T16:05:46.676978Z","iopub.status.idle":"2022-03-16T16:05:46.716376Z","shell.execute_reply.started":"2022-03-16T16:05:46.676941Z","shell.execute_reply":"2022-03-16T16:05:46.715369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = transactions.rename(columns={'customer_id': 'user_id:token', 'article_id': 'item_id:token', 'timestamp': 'timestamp:float'})\n\ntemp.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:05:54.807783Z","iopub.execute_input":"2022-03-16T16:05:54.808309Z","iopub.status.idle":"2022-03-16T16:05:54.846869Z","shell.execute_reply.started":"2022-03-16T16:05:54.808272Z","shell.execute_reply":"2022-03-16T16:05:54.845990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp.to_csv('/kaggle/working/recbox_data/recbox_data.inter', index=False, sep='\\t')","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:05:57.869029Z","iopub.execute_input":"2022-03-16T16:05:57.869277Z","iopub.status.idle":"2022-03-16T16:06:00.132713Z","shell.execute_reply.started":"2022-03-16T16:05:57.869250Z","shell.execute_reply":"2022-03-16T16:06:00.131668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.sequence import pad_sequences\nfrom tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout, Bidirectional\nfrom tensorflow.keras.preprocessing.text import Tokenizer\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras import regularizers\nfrom tensorflow.keras import losses\nfrom tensorflow.keras import metrics\nimport tensorflow.keras.utils as ku","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:06:05.656872Z","iopub.execute_input":"2022-03-16T16:06:05.657118Z","iopub.status.idle":"2022-03-16T16:06:05.662229Z","shell.execute_reply.started":"2022-03-16T16:06:05.657090Z","shell.execute_reply":"2022-03-16T16:06:05.661551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install recbole\nimport logging\nfrom logging import getLogger\nfrom recbole.config import Config\nfrom recbole.data import create_dataset, data_preparation\nfrom recbole.model.sequential_recommender import GRU4Rec\nfrom recbole.trainer import Trainer\nfrom recbole.utils import init_seed, init_logger","metadata":{"execution":{"iopub.status.busy":"2022-03-16T15:36:44.695920Z","iopub.execute_input":"2022-03-16T15:36:44.696223Z","iopub.status.idle":"2022-03-16T15:37:02.719521Z","shell.execute_reply.started":"2022-03-16T15:36:44.696193Z","shell.execute_reply":"2022-03-16T15:37:02.718754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"parameter_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': \"[40,inf)\",\n    'item_inter_num_interval': \"[40,inf)\",\n    'load_col': {'inter': ['user_id', 'item_id', 'timestamp'],\n                 'item': ['item_id', 'product_code', 'product_type_no', 'product_group_name', 'graphical_appearance_no',\n                      'colour_group_code', 'perceived_colour_value_id', 'perceived_colour_master_id',\n                      'department_no', 'index_code', 'index_group_no', 'section_no', 'garment_group_no']\n             },\n    'selected_features': ['product_code', 'product_type_no', 'product_group_name', 'graphical_appearance_no',\n                          'colour_group_code', 'perceived_colour_value_id', 'perceived_colour_master_id',\n                          'department_no', 'index_code', 'index_group_no', 'section_no', 'garment_group_no'],\n    'neg_sampling': None,\n    'epochs': 70,\n    'eval_args': {\n        'split': {'RS': [9, 0, 1]},\n        'group_by': 'user',\n        'order': 'TO',\n        'mode': 'full'}\n}\n\nconfig = Config(model='GRU4Rec', dataset='recbox_data', config_dict=parameter_dict)\n\n# init random seed\ninit_seed(config['seed'], config['reproducibility'])\ninit_logger(config)\nlogger = getLogger()\n# Create handlers\nc_handler = logging.StreamHandler()\nc_handler.setLevel(logging.INFO)\nlogger.addHandler(c_handler)\n\n# write config info into log\nlogger.info(config)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:06:10.380296Z","iopub.execute_input":"2022-03-16T16:06:10.380910Z","iopub.status.idle":"2022-03-16T16:06:11.278755Z","shell.execute_reply.started":"2022-03-16T16:06:10.380858Z","shell.execute_reply":"2022-03-16T16:06:11.277889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = create_dataset(config)\nlogger.info(dataset)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:10:59.136178Z","iopub.execute_input":"2022-03-16T16:10:59.136721Z","iopub.status.idle":"2022-03-16T16:10:59.268552Z","shell.execute_reply.started":"2022-03-16T16:10:59.136679Z","shell.execute_reply":"2022-03-16T16:10:59.268019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data, valid_data, test_data = data_preparation(config, dataset)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:11:24.042266Z","iopub.execute_input":"2022-03-16T16:11:24.042544Z","iopub.status.idle":"2022-03-16T16:11:39.742202Z","shell.execute_reply.started":"2022-03-16T16:11:24.042513Z","shell.execute_reply":"2022-03-16T16:11:39.741656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model loading and initialization\nmodel = GRU4Rec(config, train_data.dataset).to(config['device'])\nlogger.info(model)\n\n# trainer loading and initialization\ntrainer = Trainer(config, model)\n\n# model training\nbest_valid_score, best_valid_result = trainer.fit(train_data)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:30:27.567063Z","iopub.execute_input":"2022-03-16T16:30:27.567322Z","iopub.status.idle":"2022-03-16T16:42:40.763492Z","shell.execute_reply.started":"2022-03-16T16:30:27.567292Z","shell.execute_reply":"2022-03-16T16:42:40.762790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from recbole.utils.case_study import full_sort_topk\nexternal_user_ids = dataset.id2token(\n    dataset.uid_field, list(range(dataset.user_num)))[1:]","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:52:00.576448Z","iopub.execute_input":"2022-03-16T16:52:00.576770Z","iopub.status.idle":"2022-03-16T16:52:00.588915Z","shell.execute_reply.started":"2022-03-16T16:52:00.576729Z","shell.execute_reply":"2022-03-16T16:52:00.588085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"topk_items = []\nfor internal_user_id in 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":{"execution":{"iopub.status.busy":"2022-03-16T16:52:11.047330Z","iopub.execute_input":"2022-03-16T16:52:11.047650Z","iopub.status.idle":"2022-03-16T16:52:42.886025Z","shell.execute_reply.started":"2022-03-16T16:52:11.047612Z","shell.execute_reply":"2022-03-16T16:52:42.885322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"external_item_str = [' '.join(x) for x in topk_items]\nresult = pd.DataFrame(external_user_ids, columns=['customer_id'])\nresult['prediction'] = external_item_str\nresult.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:52:57.698166Z","iopub.execute_input":"2022-03-16T16:52:57.698444Z","iopub.status.idle":"2022-03-16T16:52:57.725410Z","shell.execute_reply.started":"2022-03-16T16:52:57.698413Z","shell.execute_reply":"2022-03-16T16:52:57.724566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.shape\n","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:54:16.356981Z","iopub.execute_input":"2022-03-16T16:54:16.357350Z","iopub.status.idle":"2022-03-16T16:54:16.365174Z","shell.execute_reply.started":"2022-03-16T16:54:16.357314Z","shell.execute_reply":"2022-03-16T16:54:16.364475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:57:17.054731Z","iopub.execute_input":"2022-03-16T16:57:17.055256Z","iopub.status.idle":"2022-03-16T16:57:17.076953Z","shell.execute_reply.started":"2022-03-16T16:57:17.055218Z","shell.execute_reply":"2022-03-16T16:57:17.076324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df = pd.merge(sample.to_pandas(), result, on='customer_id', how='outer')\nsubmit_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:58:56.769980Z","iopub.execute_input":"2022-03-16T16:58:56.770235Z","iopub.status.idle":"2022-03-16T16:58:58.282125Z","shell.execute_reply.started":"2022-03-16T16:58:56.770208Z","shell.execute_reply":"2022-03-16T16:58:58.281442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df = submit_df.fillna(-1)\nsubmit_df['prediction'] = submit_df.apply(\n    lambda x: x['prediction_y'] if x['prediction_y'] != -1 else x['prediction_x'], axis=1)\nsubmit_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:59:15.406134Z","iopub.execute_input":"2022-03-16T16:59:15.406416Z","iopub.status.idle":"2022-03-16T16:59:37.753274Z","shell.execute_reply.started":"2022-03-16T16:59:15.406383Z","shell.execute_reply":"2022-03-16T16:59:37.752462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df = submit_df.drop(columns=['prediction_y', 'prediction_x'])\nsubmit_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T16:59:48.267945Z","iopub.execute_input":"2022-03-16T16:59:48.268211Z","iopub.status.idle":"2022-03-16T16:59:48.522173Z","shell.execute_reply.started":"2022-03-16T16:59:48.268181Z","shell.execute_reply":"2022-03-16T16:59:48.521433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:00:10.464515Z","iopub.execute_input":"2022-03-16T17:00:10.464779Z","iopub.status.idle":"2022-03-16T17:00:10.469953Z","shell.execute_reply.started":"2022-03-16T17:00:10.464749Z","shell.execute_reply":"2022-03-16T17:00:10.469270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir /kaggle/submission\nsubmit_df.to_csv('/kaggle/submission/submit.csv')","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:33:11.293422Z","iopub.execute_input":"2022-03-16T17:33:11.293706Z","iopub.status.idle":"2022-03-16T17:33:24.476446Z","shell.execute_reply.started":"2022-03-16T17:33:11.293674Z","shell.execute_reply":"2022-03-16T17:33:24.475645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}