{"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":"# Recommending Items Recently Bought","metadata":{}},{"cell_type":"code","source":"!pip install modin\nimport modin.pandas as pd\nimport ray\nray.init()\nimport numpy as np # linear algebra\n# import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\n","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-18T06:50:32.382761Z","iopub.execute_input":"2022-02-18T06:50:32.383288Z","iopub.status.idle":"2022-02-18T06:50:48.066491Z","shell.execute_reply.started":"2022-02-18T06:50:32.383242Z","shell.execute_reply":"2022-02-18T06:50:48.063732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:28:56.439587Z","iopub.execute_input":"2022-02-18T07:28:56.440246Z","iopub.status.idle":"2022-02-18T07:29:03.138989Z","shell.execute_reply.started":"2022-02-18T07:28:56.440202Z","shell.execute_reply":"2022-02-18T07:29:03.138061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-18T06:50:54.683738Z","iopub.execute_input":"2022-02-18T06:50:54.684056Z","iopub.status.idle":"2022-02-18T06:50:54.694470Z","shell.execute_reply.started":"2022-02-18T06:50:54.684022Z","shell.execute_reply":"2022-02-18T06:50:54.693464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.head(1)","metadata":{"execution":{"iopub.status.busy":"2022-02-18T06:51:08.462600Z","iopub.execute_input":"2022-02-18T06:51:08.462910Z","iopub.status.idle":"2022-02-18T06:51:08.648368Z","shell.execute_reply.started":"2022-02-18T06:51:08.462880Z","shell.execute_reply":"2022-02-18T06:51:08.647311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# customers = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/customers.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-18T06:51:09.020267Z","iopub.execute_input":"2022-02-18T06:51:09.020716Z","iopub.status.idle":"2022-02-18T06:51:09.025847Z","shell.execute_reply.started":"2022-02-18T06:51:09.020684Z","shell.execute_reply":"2022-02-18T06:51:09.023055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# customers.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-18T06:51:09.862909Z","iopub.execute_input":"2022-02-18T06:51:09.863569Z","iopub.status.idle":"2022-02-18T06:51:09.867978Z","shell.execute_reply.started":"2022-02-18T06:51:09.863522Z","shell.execute_reply":"2022-02-18T06:51:09.866924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# articles = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/articles.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-18T06:51:10.347314Z","iopub.execute_input":"2022-02-18T06:51:10.348181Z","iopub.status.idle":"2022-02-18T06:51:10.352642Z","shell.execute_reply.started":"2022-02-18T06:51:10.348136Z","shell.execute_reply":"2022-02-18T06:51:10.351698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# articles.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-02-18T06:51:11.065904Z","iopub.execute_input":"2022-02-18T06:51:11.066939Z","iopub.status.idle":"2022-02-18T06:51:11.071355Z","shell.execute_reply.started":"2022-02-18T06:51:11.066888Z","shell.execute_reply":"2022-02-18T06:51:11.070460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# articles[articles.columns].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-02-18T06:51:11.628821Z","iopub.execute_input":"2022-02-18T06:51:11.629724Z","iopub.status.idle":"2022-02-18T06:51:11.633613Z","shell.execute_reply.started":"2022-02-18T06:51:11.629676Z","shell.execute_reply":"2022-02-18T06:51:11.632676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv', dtype={'article_id': str})\n# set dtype or pandas will drop the leading '0' and convert to int\n\n# transactions_train = pd.read_csv('../input/recomen-split/train0.csv', dtype={'article_id': str})\n","metadata":{"execution":{"iopub.status.busy":"2022-02-18T06:51:12.918543Z","iopub.execute_input":"2022-02-18T06:51:12.919404Z","iopub.status.idle":"2022-02-18T06:52:07.826618Z","shell.execute_reply.started":"2022-02-18T06:51:12.919355Z","shell.execute_reply":"2022-02-18T06:52:07.824803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train['t_dat'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-02-18T06:52:07.830180Z","iopub.execute_input":"2022-02-18T06:52:07.830453Z","iopub.status.idle":"2022-02-18T06:52:24.779399Z","shell.execute_reply.started":"2022-02-18T06:52:07.830399Z","shell.execute_reply":"2022-02-18T06:52:24.778359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-18T06:52:24.781002Z","iopub.execute_input":"2022-02-18T06:52:24.781441Z","iopub.status.idle":"2022-02-18T06:52:24.787191Z","shell.execute_reply.started":"2022-02-18T06:52:24.781380Z","shell.execute_reply":"2022-02-18T06:52:24.786394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train['t_dat'] = pd.to_datetime(transactions_train['t_dat'])\npart = transactions_train[transactions_train['t_dat'] >= pd.to_datetime('2019-07-01')]","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:03:15.094973Z","iopub.execute_input":"2022-02-18T07:03:15.095752Z","iopub.status.idle":"2022-02-18T07:03:32.607361Z","shell.execute_reply.started":"2022-02-18T07:03:15.095696Z","shell.execute_reply":"2022-02-18T07:03:32.606411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_trainR = part[::-1]","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:03:37.659230Z","iopub.execute_input":"2022-02-18T07:03:37.659631Z","iopub.status.idle":"2022-02-18T07:04:33.394868Z","shell.execute_reply.started":"2022-02-18T07:03:37.659583Z","shell.execute_reply":"2022-02-18T07:04:33.393809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"part = transactions_train[transactions_train['t_dat'] < pd.to_datetime('2019-07-01')]\npartR = part[::-1]","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:05:11.932119Z","iopub.execute_input":"2022-02-18T07:05:11.932630Z","iopub.status.idle":"2022-02-18T07:06:00.333227Z","shell.execute_reply.started":"2022-02-18T07:05:11.932569Z","shell.execute_reply":"2022-02-18T07:06:00.331931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_trainR = pd.concat([transactions_trainR, partR])\ntransactions_trainR","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:09:52.537263Z","iopub.execute_input":"2022-02-18T07:09:52.537866Z","iopub.status.idle":"2022-02-18T07:09:57.021991Z","shell.execute_reply.started":"2022-02-18T07:09:52.537824Z","shell.execute_reply":"2022-02-18T07:09:57.021261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transactions_trainR.to_csv('transactions_trainR.csv', index = False)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:13:00.763844Z","iopub.execute_input":"2022-02-18T07:13:00.764358Z","iopub.status.idle":"2022-02-18T07:13:00.770029Z","shell.execute_reply.started":"2022-02-18T07:13:00.764304Z","shell.execute_reply":"2022-02-18T07:13:00.768905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_dat_unique = transactions_trainR['t_dat'].unique()\n# len(t_dat_unique) # 734\nt_dat_number = np.arange(len(t_dat_unique))","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:13:03.612634Z","iopub.execute_input":"2022-02-18T07:13:03.613144Z","iopub.status.idle":"2022-02-18T07:13:51.319251Z","shell.execute_reply.started":"2022-02-18T07:13:03.613094Z","shell.execute_reply":"2022-02-18T07:13:51.317861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(t_dat_unique)","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:13:51.321779Z","iopub.execute_input":"2022-02-18T07:13:51.322837Z","iopub.status.idle":"2022-02-18T07:13:51.330296Z","shell.execute_reply.started":"2022-02-18T07:13:51.322782Z","shell.execute_reply":"2022-02-18T07:13:51.329454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_dat_unique","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:13:51.332128Z","iopub.execute_input":"2022-02-18T07:13:51.332790Z","iopub.status.idle":"2022-02-18T07:13:51.371180Z","shell.execute_reply.started":"2022-02-18T07:13:51.332739Z","shell.execute_reply":"2022-02-18T07:13:51.370217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission = sample_submission.copy()\n# temp_submission.index = temp_submission['customer_id']\ntemp_submission.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:13:51.377768Z","iopub.execute_input":"2022-02-18T07:13:51.380465Z","iopub.status.idle":"2022-02-18T07:13:51.666734Z","shell.execute_reply.started":"2022-02-18T07:13:51.380386Z","shell.execute_reply":"2022-02-18T07:13:51.665738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_trainR['article_id'] = transactions_trainR['article_id'] + ' '\ntransactions_trainR","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:14:18.307083Z","iopub.execute_input":"2022-02-18T07:14:18.308012Z","iopub.status.idle":"2022-02-18T07:14:43.225227Z","shell.execute_reply.started":"2022-02-18T07:14:18.307962Z","shell.execute_reply":"2022-02-18T07:14:43.224489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g1 = transactions_trainR.groupby('customer_id')['article_id'].sum()#apply(lambda x: x.sum())\n\ndf1 = pd.DataFrame(\n    data=np.array(g1.index),\n#     index=g1.index,\n    columns=['customer_id']\n             )\n\ndf2 = pd.DataFrame(\n    data=np.array(g1),\n#     index=g1.index,\n    columns=['article_id']\n             )\ndf_add = df1.join(df2)","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-02-18T07:14:43.226667Z","iopub.execute_input":"2022-02-18T07:14:43.227020Z","iopub.status.idle":"2022-02-18T07:15:40.314496Z","shell.execute_reply.started":"2022-02-18T07:14:43.226990Z","shell.execute_reply":"2022-02-18T07:15:40.313496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_temp_submission = temp_submission.set_index('customer_id')\nmini_temp = df_add.set_index('customer_id')\nnew_temp_submission2 = new_temp_submission.join(mini_temp, how='left')\nnew_temp_submission2 = new_temp_submission2.reset_index()\n# new_temp_submission2# = new_temp_submission2.rename(columns={'index': 'customer_id'})\nnew_temp_submission2['new_prediction'] = new_temp_submission2['article_id'] + new_temp_submission2['prediction']\ntemp_submission['prediction'] = new_temp_submission2['new_prediction']\ntemp_submission","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:15:40.315912Z","iopub.execute_input":"2022-02-18T07:15:40.316162Z","iopub.status.idle":"2022-02-18T07:16:10.678832Z","shell.execute_reply.started":"2022-02-18T07:15:40.316133Z","shell.execute_reply":"2022-02-18T07:16:10.677708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission.isnull().any()","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:23:11.508263Z","iopub.execute_input":"2022-02-18T07:23:11.509142Z","iopub.status.idle":"2022-02-18T07:23:14.408466Z","shell.execute_reply.started":"2022-02-18T07:23:11.509093Z","shell.execute_reply":"2022-02-18T07:23:14.407501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission['prediction'][temp_submission['prediction'].isnull()] = sample_submission['prediction']","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:32:10.916144Z","iopub.execute_input":"2022-02-18T07:32:10.917296Z","iopub.status.idle":"2022-02-18T07:32:16.891903Z","shell.execute_reply.started":"2022-02-18T07:32:10.917231Z","shell.execute_reply":"2022-02-18T07:32:16.890733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp_submission.isnull().any()","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:32:16.894085Z","iopub.execute_input":"2022-02-18T07:32:16.898064Z","iopub.status.idle":"2022-02-18T07:32:20.474576Z","shell.execute_reply.started":"2022-02-18T07:32:16.898020Z","shell.execute_reply":"2022-02-18T07:32:20.473719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission['prediction'] = temp_submission['prediction'].str[:131]","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:32:27.373714Z","iopub.execute_input":"2022-02-18T07:32:27.374657Z","iopub.status.idle":"2022-02-18T07:32:27.403595Z","shell.execute_reply.started":"2022-02-18T07:32:27.374611Z","shell.execute_reply":"2022-02-18T07:32:27.401871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.isnull().any()","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:32:30.574967Z","iopub.execute_input":"2022-02-18T07:32:30.575793Z","iopub.status.idle":"2022-02-18T07:32:33.320496Z","shell.execute_reply.started":"2022-02-18T07:32:30.575735Z","shell.execute_reply":"2022-02-18T07:32:33.319469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:32:36.746739Z","iopub.execute_input":"2022-02-18T07:32:36.747478Z","iopub.status.idle":"2022-02-18T07:32:47.236837Z","shell.execute_reply.started":"2022-02-18T07:32:36.747407Z","shell.execute_reply":"2022-02-18T07:32:47.236038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission","metadata":{"execution":{"iopub.status.busy":"2022-02-18T07:32:47.240266Z","iopub.execute_input":"2022-02-18T07:32:47.240651Z","iopub.status.idle":"2022-02-18T07:32:50.507796Z","shell.execute_reply.started":"2022-02-18T07:32:47.240606Z","shell.execute_reply":"2022-02-18T07:32:50.506176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}