{"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)\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-22T16:59:05.725008Z","iopub.execute_input":"2022-03-22T16:59:05.725321Z","iopub.status.idle":"2022-03-22T16:59:06.667423Z","shell.execute_reply.started":"2022-03-22T16:59:05.725281Z","shell.execute_reply":"2022-03-22T16:59:06.666540Z"},"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-22T16:59:06.669040Z","iopub.execute_input":"2022-03-22T16:59:06.669287Z","iopub.status.idle":"2022-03-22T17:00:09.075686Z","shell.execute_reply.started":"2022-03-22T16:59:06.669256Z","shell.execute_reply":"2022-03-22T17:00:09.073796Z"},"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","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:00:09.077607Z","iopub.execute_input":"2022-03-22T17:00:09.078689Z","iopub.status.idle":"2022-03-22T17:00:19.508405Z","shell.execute_reply.started":"2022-03-22T17:00:09.078646Z","shell.execute_reply":"2022-03-22T17:00:19.505931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train['max_purc_date'] = transactions_train.groupby('customer_id')['t_dat_datetime'].transform('max')\n\ntransactions_train['min_purc_date'] = transactions_train.groupby('customer_id')['t_dat_datetime'].transform('min')\n\ntransactions_train['total_visits'] = transactions_train.groupby('customer_id')['t_dat'].transform('nunique')\n\ntransactions_train['total_item_bought'] = transactions_train.groupby('customer_id')['article_id'].transform('nunique')\n\ntransactions_train['max_year'] = transactions_train.groupby('customer_id')['t_year'].transform('max')","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:00:19.514093Z","iopub.execute_input":"2022-03-22T17:00:19.514570Z","iopub.status.idle":"2022-03-22T17:01:46.473342Z","shell.execute_reply.started":"2022-03-22T17:00:19.514526Z","shell.execute_reply":"2022-03-22T17:01:46.471767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# transactions_train_filt = transactions_train[transactions_train['max_year'] == 2020]\n\n# transactions_train_filt['product_reach'] = transactions_train_filt.groupby('article_id')['customer_id'].transform('nunique')\n\n# transactions_train_filt = transactions_train_filt[transactions_train_filt['product_reach'] > 8]\n\ntransactions_train['repeat_purchase'] = transactions_train.groupby(['customer_id','article_id'])['t_dat'].transform('nunique')\n\ntransactions_train_filt = transactions_train[transactions_train['repeat_purchase'] > 1]\n\n# transactions_train_filt = transactions_train_filt[transactions_train_filt['total_visits'] >= 2]\n\n# transactions_train_filt = transactions_train_filt[transactions_train_filt['total_item_bought'] >= 12]","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:01:46.475322Z","iopub.execute_input":"2022-03-22T17:01:46.476099Z","iopub.status.idle":"2022-03-22T17:02:26.658327Z","shell.execute_reply.started":"2022-03-22T17:01:46.476051Z","shell.execute_reply":"2022-03-22T17:02:26.657375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# article_cust = transactions_train_filt[['article_id','customer_id']].drop_duplicates()\n\n# article_cust_full = pd.merge(article_cust, article_cust.rename(columns={'article_id':'article_id_1',\n#                                                    }),\n#          left_on='customer_id',\n#          right_on='customer_id'\n#         )\n\n# article_cust_full = article_cust_full[article_cust_full['article_id'] != article_cust_full['article_id_1']]\n\n# common_cust = article_cust_full.groupby(['article_id', 'article_id_1']).\\\n# agg(common_cust=('customer_id','nunique')).reset_index()\n\n# common_cust.shape\n\n# common_cust = pd.merge(common_cust,\n#                        article_cust_full.groupby('article_id').\\\n#                        agg(article_id_cus=('customer_id','nunique')).\\\n#                        reset_index(),\n#                        left_on = 'article_id',\n#                        right_on = 'article_id'\n#                       )\n# common_cust.head()\n\n# common_cust.shape\n\n# common_cust = pd.merge(common_cust,\n#                        article_cust_full.groupby('article_id_1').\\\n#                        agg(article_id_cus=('customer_id','nunique')).\\\n#                        reset_index(),\n#                        left_on = 'article_id_1',\n#                        right_on = 'article_id_1'\n#                       )\n# common_cust.head()\n\n# common_cust['lift'] = common_cust['common_cust']/ (common_cust['article_id_cus_x']*common_cust['article_id_cus_y'])\n\n# common_cust = common_cust[common_cust['common_cust']>10].sort_values(['article_id_cus_y','lift'], ascending=[False,False])\n\n# len(common_cust['article_id'].unique())\n\n# common_cust = pd.merge(common_cust,\n#          transactions_train_filt.groupby(['article_id']).\\\n#          agg(article_id_max_dat=('t_dat_datetime','max')).\\\n#          reset_index(),\n#          left_on='article_id',\n#          right_on='article_id'\n#         )\n\n# common_cust = pd.merge(common_cust,\n#          transactions_train_filt.rename(columns={}).groupby(['article_id']).\\\n#          agg(article_id_1_max_dat=('t_dat_datetime','max')).\\\n#          reset_index(),\n#          left_on='article_id_1',\n#          right_on='article_id'\n#         )\n\n# common_cust.drop(['article_id_y'], axis = 1, inplace=True)\n# common_cust.head()\n\n# len(transactions_train_filt[(transactions_train_filt['t_month'].isin([8,9,10]))&\n#                             (transactions_train_filt['t_year']!=2020)\n                            \n#                            ]['customer_id'].unique())\n\n","metadata":{"execution":{"iopub.status.busy":"2022-03-21T18:01:41.426772Z","iopub.execute_input":"2022-03-21T18:01:41.427101Z","iopub.status.idle":"2022-03-21T18:01:47.037688Z","shell.execute_reply.started":"2022-03-21T18:01:41.427068Z","shell.execute_reply":"2022-03-21T18:01:47.03669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"product_purchase_cycle = transactions_train_filt[['customer_id','article_id','t_dat_datetime']].\\\nsort_values(['customer_id','article_id','t_dat_datetime']).drop_duplicates()\n\nproduct_purchase_cycle['t_dat_datetime_lag'] = product_purchase_cycle.\\\ngroupby(['customer_id','article_id'])['t_dat_datetime'].shift(1)\n\nproduct_purchase_cycle['purchase_cycle'] = product_purchase_cycle['t_dat_datetime'] - product_purchase_cycle['t_dat_datetime_lag']\n\nAverage_Purchase_Cycle = product_purchase_cycle[~(product_purchase_cycle['purchase_cycle'].isnull())].\\\ngroupby(['customer_id','article_id']).agg({\"purchase_cycle\":\"mean\"}).reset_index()\n\ntransactions_train_filt = pd.merge(transactions_train_filt,\n                                   Average_Purchase_Cycle,\n                                   left_on = ['customer_id','article_id'],\n                                   right_on = ['customer_id','article_id'],\n                                   how='inner'\n                                  )\n\ntransactions_train_filt['product_last_purchase_date'] = transactions_train_filt.\\\ngroupby(['customer_id','article_id'])['t_dat_datetime'].transform(\"max\")","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:03:18.723709Z","iopub.execute_input":"2022-03-22T17:03:18.724421Z","iopub.status.idle":"2022-03-22T17:03:33.324013Z","shell.execute_reply.started":"2022-03-22T17:03:18.724378Z","shell.execute_reply":"2022-03-22T17:03:33.323151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datetime import datetime\nfrom datetime import timedelta\n\ntransactions_train_filt['purchase_cycle_days'] = transactions_train_filt['purchase_cycle'].astype('str').str.split(' ', expand=True)[0]\n\ntransactions_train_filt['next_purchase_date'] = transactions_train_filt['product_last_purchase_date'] + transactions_train_filt['purchase_cycle_days'].astype(int).map(timedelta)","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:03:33.325941Z","iopub.execute_input":"2022-03-22T17:03:33.326422Z","iopub.status.idle":"2022-03-22T17:04:14.555737Z","shell.execute_reply.started":"2022-03-22T17:03:33.326375Z","shell.execute_reply":"2022-03-22T17:04:14.554562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start_date = transactions_train_filt['t_dat_datetime'].max() + timedelta(-90)\nend_date = transactions_train_filt['t_dat_datetime'].max() + timedelta(90)","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:05:22.828952Z","iopub.execute_input":"2022-03-22T17:05:22.829374Z","iopub.status.idle":"2022-03-22T17:05:22.855051Z","shell.execute_reply.started":"2022-03-22T17:05:22.829339Z","shell.execute_reply":"2022-03-22T17:05:22.853826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datetime.date(transactions_train_filt['t_dat_datetime'].max()) ","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:05:25.403033Z","iopub.execute_input":"2022-03-22T17:05:25.403349Z","iopub.status.idle":"2022-03-22T17:05:25.418938Z","shell.execute_reply.started":"2022-03-22T17:05:25.403315Z","shell.execute_reply":"2022-03-22T17:05:25.418258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start_date, end_date","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:05:26.035901Z","iopub.execute_input":"2022-03-22T17:05:26.036626Z","iopub.status.idle":"2022-03-22T17:05:26.042818Z","shell.execute_reply.started":"2022-03-22T17:05:26.036588Z","shell.execute_reply":"2022-03-22T17:05:26.041946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(transactions_train_filt['customer_id'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:05:26.624289Z","iopub.execute_input":"2022-03-22T17:05:26.625124Z","iopub.status.idle":"2022-03-22T17:05:27.717887Z","shell.execute_reply.started":"2022-03-22T17:05:26.625061Z","shell.execute_reply":"2022-03-22T17:05:27.717268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer_next_purchase = transactions_train_filt[transactions_train_filt['next_purchase_date'].\\\n                        between(start_date, end_date, inclusive=\"both\")]\\\n[['customer_id','t_dat','article_id']].drop_duplicates()\n\ncustomer_next_purchase['repeat_purchase'] = customer_next_purchase.groupby(['customer_id'])['article_id'].transform('count')\n\ncustomer_next_purchase['product_imp_rank'] = customer_next_purchase.groupby(['customer_id'])['repeat_purchase'].rank(method=\"first\",ascending=True)\n\ncustomer_next_purchase['max_rank'] = customer_next_purchase[customer_next_purchase['product_imp_rank'] <= 12].\\\ngroupby('customer_id')['product_imp_rank'].transform('max')\n\ncustomer_next_purchase = customer_next_purchase[['customer_id','article_id','max_rank']]\n\ncustomer_next_purchase = customer_next_purchase.sort_values(['customer_id','article_id'])","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:05:27.719124Z","iopub.execute_input":"2022-03-22T17:05:27.719460Z","iopub.status.idle":"2022-03-22T17:05:30.026508Z","shell.execute_reply.started":"2022-03-22T17:05:27.719430Z","shell.execute_reply":"2022-03-22T17:05:30.025897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(customer_next_purchase['customer_id'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:05:30.027594Z","iopub.execute_input":"2022-03-22T17:05:30.027931Z","iopub.status.idle":"2022-03-22T17:05:30.200004Z","shell.execute_reply.started":"2022-03-22T17:05:30.027895Z","shell.execute_reply":"2022-03-22T17:05:30.199171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Filling Products \n\ntop_12_products = customer_next_purchase.groupby(['article_id']).agg(cust_count=('customer_id','nunique')).reset_index()\n\ntop_12_products = top_12_products.sort_values('cust_count', ascending = False).head(12)[['article_id']]","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:05:30.201767Z","iopub.execute_input":"2022-03-22T17:05:30.201999Z","iopub.status.idle":"2022-03-22T17:05:30.446470Z","shell.execute_reply.started":"2022-03-22T17:05:30.201970Z","shell.execute_reply":"2022-03-22T17:05:30.445663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filler_cust_prod_comb= pd.merge(customer_next_purchase['customer_id'].drop_duplicates(), \ntop_12_products['article_id'], how = 'cross')","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:05:30.447785Z","iopub.execute_input":"2022-03-22T17:05:30.448038Z","iopub.status.idle":"2022-03-22T17:05:30.696576Z","shell.execute_reply.started":"2022-03-22T17:05:30.448003Z","shell.execute_reply":"2022-03-22T17:05:30.695857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer_next_purchase= pd.concat([customer_next_purchase[['customer_id', 'article_id']],\n          filler_cust_prod_comb[['customer_id', 'article_id']]],\n          axis = 0\n         ).drop_duplicates()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:05:30.698421Z","iopub.execute_input":"2022-03-22T17:05:30.698934Z","iopub.status.idle":"2022-03-22T17:05:31.423353Z","shell.execute_reply.started":"2022-03-22T17:05:30.698888Z","shell.execute_reply":"2022-03-22T17:05:31.422531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer_next_purchase['row_num'] = customer_next_purchase.groupby(['customer_id']).cumcount()\n\ncustomer_next_purchase = customer_next_purchase[customer_next_purchase['row_num'] <= 11]\n\ncustomer_next_purchase['article_id'] = ' 0'+customer_next_purchase['article_id'].astype('str')\n\ncustomer_next_purchase = customer_next_purchase.groupby(['customer_id'])['article_id'].sum().reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:05:31.425211Z","iopub.execute_input":"2022-03-22T17:05:31.425813Z","iopub.status.idle":"2022-03-22T17:05:33.556049Z","shell.execute_reply.started":"2022-03-22T17:05:31.425766Z","shell.execute_reply":"2022-03-22T17:05:33.555021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_12_products['article_id'] = ' 0'+top_12_products['article_id'].astype('str')\n\ntop_12_products_for_missin_cust = top_12_products['article_id'].sum()\ntop_12_products_for_missin_cust","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:05:33.683180Z","iopub.execute_input":"2022-03-22T17:05:33.683461Z","iopub.status.idle":"2022-03-22T17:05:33.691269Z","shell.execute_reply.started":"2022-03-22T17:05:33.683430Z","shell.execute_reply":"2022-03-22T17:05:33.690388Z"},"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')\\\n[['customer_id']]\n\nsample_submission = pd.merge(sample_submission,\n                             customer_next_purchase,\n                             left_on = ['customer_id'],\n                             right_on = ['customer_id'],\n                             how = 'left'\n                            ).fillna(top_12_products_for_missin_cust)","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:05:36.318882Z","iopub.execute_input":"2022-03-22T17:05:36.319154Z","iopub.status.idle":"2022-03-22T17:05:42.177413Z","shell.execute_reply.started":"2022-03-22T17:05:36.319127Z","shell.execute_reply":"2022-03-22T17:05:42.176413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# no_products_cust['article_id'].fillna( \"0706016001 0706016002 0372860001 0399223001 0464297007 0160442007 0610776002 0156231001 0399256001 0372860002 0706016003 0759871002\", inplace=True)\n# no_products_cust.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:05:42.181185Z","iopub.execute_input":"2022-03-22T17:05:42.181938Z","iopub.status.idle":"2022-03-22T17:05:42.186937Z","shell.execute_reply.started":"2022-03-22T17:05:42.181888Z","shell.execute_reply":"2022-03-22T17:05:42.185252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.rename(columns={'article_id':'prediction'}, inplace=True)\nsample_submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:05:42.188379Z","iopub.execute_input":"2022-03-22T17:05:42.188636Z","iopub.status.idle":"2022-03-22T17:05:42.220677Z","shell.execute_reply.started":"2022-03-22T17:05:42.188595Z","shell.execute_reply":"2022-03-22T17:05:42.219579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission.to_csv('submission.csv', index =False)","metadata":{"execution":{"iopub.status.busy":"2022-03-22T17:05:42.222341Z","iopub.execute_input":"2022-03-22T17:05:42.222630Z","iopub.status.idle":"2022-03-22T17:05:48.672709Z","shell.execute_reply.started":"2022-03-22T17:05:42.222587Z","shell.execute_reply":"2022-03-22T17:05:48.671780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}