{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-27T15:44:56.151482Z","iopub.execute_input":"2022-03-27T15:44:56.151820Z","iopub.status.idle":"2022-03-27T15:44:56.176688Z","shell.execute_reply.started":"2022-03-27T15:44:56.151733Z","shell.execute_reply":"2022-03-27T15:44:56.175996Z"},"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')","metadata":{"execution":{"iopub.status.busy":"2022-03-27T15:45:21.783741Z","iopub.execute_input":"2022-03-27T15:45:21.784005Z","iopub.status.idle":"2022-03-27T15:46:27.713934Z","shell.execute_reply.started":"2022-03-27T15:45:21.783978Z","shell.execute_reply":"2022-03-27T15:46:27.712944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train['t_dat_datetime']  = pd.to_datetime(transactions_train['t_dat'])\n\ntransactions_train['t_month'] = transactions_train['t_dat_datetime'].dt.month\n\ntransactions_train['t_year']  = transactions_train['t_dat_datetime'].dt.year\n\ntop_7_products = transactions_train[(transactions_train['t_year']==2020)&\n                                     (transactions_train['t_month'].isin([9]))\n                                    ].groupby('article_id').agg({'customer_id':'nunique'}).reset_index().\\\nsort_values('customer_id', ascending=False).head(7)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T17:01:23.653276Z","iopub.execute_input":"2022-03-26T17:01:23.653677Z","iopub.status.idle":"2022-03-26T17:01:36.124697Z","shell.execute_reply.started":"2022-03-26T17:01:23.653636Z","shell.execute_reply":"2022-03-26T17:01:36.123987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_7_products.head(7)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T17:01:36.125785Z","iopub.execute_input":"2022-03-26T17:01:36.126117Z","iopub.status.idle":"2022-03-26T17:01:36.138871Z","shell.execute_reply.started":"2022-03-26T17:01:36.126089Z","shell.execute_reply":"2022-03-26T17:01:36.137990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_7_products.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-26T17:01:36.140887Z","iopub.execute_input":"2022-03-26T17:01:36.141092Z","iopub.status.idle":"2022-03-26T17:01:36.146051Z","shell.execute_reply.started":"2022-03-26T17:01:36.141067Z","shell.execute_reply":"2022-03-26T17:01:36.145515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train['repeat_purchase'] = transactions_train.groupby(['customer_id','article_id'])['t_dat'].transform('nunique')","metadata":{"execution":{"iopub.status.busy":"2022-03-26T17:01:36.147082Z","iopub.execute_input":"2022-03-26T17:01:36.147484Z","iopub.status.idle":"2022-03-26T17:02:10.977035Z","shell.execute_reply.started":"2022-03-26T17:01:36.147457Z","shell.execute_reply":"2022-03-26T17:02:10.976248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filler_products = transactions_train[(transactions_train['repeat_purchase'] > 1) & (transactions_train['t_year'] == 2020)].\\\ngroupby('article_id').agg({'customer_id':'nunique'}).reset_index().\\\nsort_values('customer_id', ascending=False).head(5)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T17:02:10.978106Z","iopub.execute_input":"2022-03-26T17:02:10.978295Z","iopub.status.idle":"2022-03-26T17:02:12.741776Z","shell.execute_reply.started":"2022-03-26T17:02:10.978271Z","shell.execute_reply":"2022-03-26T17:02:12.741158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_12_products = pd.concat([top_7_products, filler_products], axis = 0)[['article_id']].drop_duplicates()","metadata":{"execution":{"iopub.status.busy":"2022-03-26T17:02:12.742780Z","iopub.execute_input":"2022-03-26T17:02:12.743085Z","iopub.status.idle":"2022-03-26T17:02:12.749379Z","shell.execute_reply.started":"2022-03-26T17:02:12.743059Z","shell.execute_reply":"2022-03-26T17:02:12.748814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_12_products['article_id'] = \" 0\"+top_12_products.article_id.astype('str')","metadata":{"execution":{"iopub.status.busy":"2022-03-26T17:02:12.750570Z","iopub.execute_input":"2022-03-26T17:02:12.750782Z","iopub.status.idle":"2022-03-26T17:02:12.761567Z","shell.execute_reply.started":"2022-03-26T17:02:12.750757Z","shell.execute_reply":"2022-03-26T17:02:12.760795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_12_products","metadata":{"execution":{"iopub.status.busy":"2022-03-26T17:02:12.762767Z","iopub.execute_input":"2022-03-26T17:02:12.763022Z","iopub.status.idle":"2022-03-26T17:02:12.777297Z","shell.execute_reply.started":"2022-03-26T17:02:12.762996Z","shell.execute_reply":"2022-03-26T17:02:12.776393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/sample_submission.csv')\n\nsubmission['prediction'] = top_12_products.article_id.sum()\n\nsubmission.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-03-26T17:02:12.779799Z","iopub.execute_input":"2022-03-26T17:02:12.780764Z","iopub.status.idle":"2022-03-26T17:02:30.200831Z","shell.execute_reply.started":"2022-03-26T17:02:12.780720Z","shell.execute_reply":"2022-03-26T17:02:30.199895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}