{"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)\nimport os\n\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-18T03:44:05.469192Z","iopub.execute_input":"2022-04-18T03:44:05.469859Z","iopub.status.idle":"2022-04-18T03:44:06.729044Z","shell.execute_reply.started":"2022-04-18T03:44:05.469741Z","shell.execute_reply":"2022-04-18T03:44:06.728150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# image names to imgpaths","metadata":{}},{"cell_type":"code","source":"img2path = {}\nfor img_folder in os.listdir('../input/h-and-m-personalized-fashion-recommendations/images'):\n    subfolder = os.path.join('../input/h-and-m-personalized-fashion-recommendations/images', img_folder)\n    for imgname in os.listdir(subfolder):\n        img2path[imgname.replace('.jpg', '')] = os.path.join(subfolder, imgname)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T03:44:06.730987Z","iopub.execute_input":"2022-04-18T03:44:06.731690Z","iopub.status.idle":"2022-04-18T03:44:12.318437Z","shell.execute_reply.started":"2022-04-18T03:44:06.731648Z","shell.execute_reply":"2022-04-18T03:44:12.317656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"article_df = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/articles.csv', \n                        dtype={'article_id': str})\narticle_df = article_df[[\n    'article_id', 'prod_name', 'product_type_name',\n    'product_group_name', 'department_name', \n    'index_name', 'index_group_name', 'section_name',\n    'garment_group_name', 'detail_desc'\n]].copy()\n\narticle_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T03:44:12.319759Z","iopub.execute_input":"2022-04-18T03:44:12.320014Z","iopub.status.idle":"2022-04-18T03:44:13.443741Z","shell.execute_reply.started":"2022-04-18T03:44:12.319984Z","shell.execute_reply":"2022-04-18T03:44:13.442901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ntransaction_df = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\", \n                             usecols=['t_dat', 'customer_id', 'article_id'],\n                             dtype={'article_id': str})\ntransaction_df['t_dat'] = pd.to_datetime(transaction_df['t_dat'])\ntransaction_df = transaction_df.merge(article_df)\n\n\ntransaction_df = transaction_df[transaction_df['t_dat'] > '2019-09-01']\nmin_date = transaction_df.t_dat.min()\ntransaction_df['week'] = ((transaction_df.t_dat - min_date).dt.days)//7\n\ntransaction_df = transaction_df.sort_values('t_dat')\ntransaction_df = transaction_df.groupby('customer_id', as_index=False)[['week', 'article_id']].agg(list)\n\ntransaction_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T03:44:13.446021Z","iopub.execute_input":"2022-04-18T03:44:13.446343Z","iopub.status.idle":"2022-04-18T03:47:12.289784Z","shell.execute_reply.started":"2022-04-18T03:44:13.446300Z","shell.execute_reply":"2022-04-18T03:47:12.288909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_df['num_purchases'] = transaction_df['article_id'].apply(len)\ntransaction_df['num_unique_purchases'] = transaction_df['article_id'].apply(lambda x: len(set(x)))\ntransaction_df['num_weeks'] = transaction_df.week.apply(lambda x:len(set(x)))\ntransaction_df['avg_purchases_per_week'] = transaction_df['num_purchases'].div(transaction_df['num_weeks'])\n\ntransaction_df = transaction_df[(transaction_df.num_purchases!=1) & \n                                (transaction_df.num_weeks<25) &\n                                (transaction_df.num_purchases<100)\n                               ]\n\ntransaction_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:19:03.826859Z","iopub.execute_input":"2022-04-18T04:19:03.827541Z","iopub.status.idle":"2022-04-18T04:19:08.600670Z","shell.execute_reply.started":"2022-04-18T04:19:03.827494Z","shell.execute_reply":"2022-04-18T04:19:08.599890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 4))\nplt.title(\"distributions of Number of Active Transaction weeks per customer\")\nsns.countplot(data=transaction_df, x='num_weeks')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:19:44.536661Z","iopub.execute_input":"2022-04-18T04:19:44.537192Z","iopub.status.idle":"2022-04-18T04:19:44.890838Z","shell.execute_reply.started":"2022-04-18T04:19:44.537139Z","shell.execute_reply":"2022-04-18T04:19:44.890136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_df.avg_purchases_per_week.describe()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:19:51.214378Z","iopub.execute_input":"2022-04-18T04:19:51.214893Z","iopub.status.idle":"2022-04-18T04:19:51.261669Z","shell.execute_reply.started":"2022-04-18T04:19:51.214823Z","shell.execute_reply":"2022-04-18T04:19:51.260816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_df.avg_purchases_per_week.quantile(0.99)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:21:31.437297Z","iopub.execute_input":"2022-04-18T04:21:31.437590Z","iopub.status.idle":"2022-04-18T04:21:31.460736Z","shell.execute_reply.started":"2022-04-18T04:21:31.437557Z","shell.execute_reply":"2022-04-18T04:21:31.459803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 4))\nplt.title(\"distributions of Avg number of purchases per weeks per customer\")\nsns.boxplot(data=transaction_df, x='avg_purchases_per_week')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:19:34.730580Z","iopub.execute_input":"2022-04-18T04:19:34.730897Z","iopub.status.idle":"2022-04-18T04:19:34.975222Z","shell.execute_reply.started":"2022-04-18T04:19:34.730846Z","shell.execute_reply":"2022-04-18T04:19:34.974339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 4))\nplt.title(\"distributions of Avg number of purchases per weeks per customer\")\nsns.histplot(data=transaction_df[transaction_df.avg_purchases_per_week<15], x='avg_purchases_per_week')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:21:38.682801Z","iopub.execute_input":"2022-04-18T04:21:38.683138Z","iopub.status.idle":"2022-04-18T04:21:39.542451Z","shell.execute_reply.started":"2022-04-18T04:21:38.683102Z","shell.execute_reply":"2022-04-18T04:21:39.541376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:22:29.310044Z","iopub.execute_input":"2022-04-18T04:22:29.310336Z","iopub.status.idle":"2022-04-18T04:22:29.327843Z","shell.execute_reply.started":"2022-04-18T04:22:29.310307Z","shell.execute_reply":"2022-04-18T04:22:29.326932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"number of customers:\",len(transaction_df))","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:29:12.700163Z","iopub.execute_input":"2022-04-18T04:29:12.700513Z","iopub.status.idle":"2022-04-18T04:29:12.705838Z","shell.execute_reply.started":"2022-04-18T04:29:12.700477Z","shell.execute_reply":"2022-04-18T04:29:12.705140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def avg_gap_betweek_weeks(weeks):\n    weeks = np.diff(weeks)\n    return np.mean(np.unique(weeks))","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:30:33.066152Z","iopub.execute_input":"2022-04-18T04:30:33.066674Z","iopub.status.idle":"2022-04-18T04:30:33.071545Z","shell.execute_reply.started":"2022-04-18T04:30:33.066641Z","shell.execute_reply":"2022-04-18T04:30:33.070574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_df['avg_week_gap'] = transaction_df['week'].apply(avg_gap_betweek_weeks)\ntransaction_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:30:58.670581Z","iopub.execute_input":"2022-04-18T04:30:58.670924Z","iopub.status.idle":"2022-04-18T04:31:30.358512Z","shell.execute_reply.started":"2022-04-18T04:30:58.670844Z","shell.execute_reply":"2022-04-18T04:31:30.357630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_df[transaction_df.avg_week_gap!=0].avg_week_gap.describe()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:32:40.609053Z","iopub.execute_input":"2022-04-18T04:32:40.609436Z","iopub.status.idle":"2022-04-18T04:32:40.738757Z","shell.execute_reply.started":"2022-04-18T04:32:40.609389Z","shell.execute_reply":"2022-04-18T04:32:40.737922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_df.avg_week_gap.quantile(0.99)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:32:58.368054Z","iopub.execute_input":"2022-04-18T04:32:58.368605Z","iopub.status.idle":"2022-04-18T04:32:58.389900Z","shell.execute_reply.started":"2022-04-18T04:32:58.368568Z","shell.execute_reply":"2022-04-18T04:32:58.389094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\nplt.xticks(np.arange(0, 50, 3))\nplt.title(\"distribution of average gap between weeks.\")\nsns.histplot(data=transaction_df[transaction_df.avg_week_gap!=0], x='avg_week_gap')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:35:02.052999Z","iopub.execute_input":"2022-04-18T04:35:02.053299Z","iopub.status.idle":"2022-04-18T04:35:03.331086Z","shell.execute_reply.started":"2022-04-18T04:35:02.053264Z","shell.execute_reply":"2022-04-18T04:35:03.330192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\nplt.title(\"scatter plot of Number Of Weeks (vs) Average Week Gap\")\nplt.xticks(np.arange(0, 25, 3))\nsns.scatterplot(data=transaction_df, x='num_weeks', y='avg_week_gap')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:51:56.032957Z","iopub.execute_input":"2022-04-18T04:51:56.033268Z","iopub.status.idle":"2022-04-18T04:51:57.784888Z","shell.execute_reply.started":"2022-04-18T04:51:56.033236Z","shell.execute_reply":"2022-04-18T04:51:57.784093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_df.groupby('num_weeks')['avg_week_gap'].std()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:52:58.458513Z","iopub.execute_input":"2022-04-18T04:52:58.458789Z","iopub.status.idle":"2022-04-18T04:52:58.488793Z","shell.execute_reply.started":"2022-04-18T04:52:58.458759Z","shell.execute_reply":"2022-04-18T04:52:58.487949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"number of customers with avg. week gap > 30:\", len(transaction_df[ (transaction_df.num_weeks>1) & \n                                                                         (transaction_df.avg_week_gap>30)]))","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:56:39.824061Z","iopub.execute_input":"2022-04-18T04:56:39.824361Z","iopub.status.idle":"2022-04-18T04:56:39.836005Z","shell.execute_reply.started":"2022-04-18T04:56:39.824328Z","shell.execute_reply":"2022-04-18T04:56:39.834974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transaction_df[ (transaction_df.num_weeks>1) & \n               (transaction_df.avg_week_gap>30)].num_weeks.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:57:13.996944Z","iopub.execute_input":"2022-04-18T04:57:13.997681Z","iopub.status.idle":"2022-04-18T04:57:14.010417Z","shell.execute_reply.started":"2022-04-18T04:57:13.997638Z","shell.execute_reply":"2022-04-18T04:57:14.009587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Observations:\n\n1. Number of Weeks the purchase happened is long-tailed , i.e number of weeks customers coming back to purchases items are getting reduced.\n\n2. If the customer comes to purchase items in a week, if we calculate the average number of items purchases by the person in a week that 90th quantile of 15 items. 75th quantile <5 items\n\n3. If we consider the average gap between the weeks in which purchase happens, most users are coming back with-in 9weeks.We can also observe that few customers have avg. gap > 40 --> these are the old customers, by which difficult to esitmate, as their preference could have changed.\n\n4. As can be seek, the Variation in the Average week gap is reduced as the number of weeks purchased increases.\n\n5. There are like 2273 customers with week gap >=30 --> these are the customers that are coming back to platform after a period of atlease 30 weeks. Inference is difficult due to change in the intereset of the user.","metadata":{}},{"cell_type":"code","source":"transaction_df.avg_week_gap.quantile(0.8)","metadata":{"execution":{"iopub.status.busy":"2022-04-18T04:36:05.118712Z","iopub.execute_input":"2022-04-18T04:36:05.119250Z","iopub.status.idle":"2022-04-18T04:36:05.139011Z","shell.execute_reply.started":"2022-04-18T04:36:05.119213Z","shell.execute_reply":"2022-04-18T04:36:05.138032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ","metadata":{}},{"cell_type":"code","source":"transaction_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-18T06:01:19.245430Z","iopub.execute_input":"2022-04-18T06:01:19.245757Z","iopub.status.idle":"2022-04-18T06:01:19.263765Z","shell.execute_reply.started":"2022-04-18T06:01:19.245722Z","shell.execute_reply":"2022-04-18T06:01:19.263187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize images","metadata":{}},{"cell_type":"code","source":"def read_image(imgpath):\n    img = cv2.imread(imgpath)\n    return img","metadata":{"execution":{"iopub.status.busy":"2022-04-18T03:53:34.902238Z","iopub.execute_input":"2022-04-18T03:53:34.902680Z","iopub.status.idle":"2022-04-18T03:53:34.907027Z","shell.execute_reply.started":"2022-04-18T03:53:34.902645Z","shell.execute_reply":"2022-04-18T03:53:34.906306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"row = transaction_df.iloc[3]\nproducts = row.article_id\nweek = row.week\n\nfor i, imgname in enumerate(products):\n    print(\"week:\", week[i])\n    print(\"product:\", imgname)\n    \n    img = read_image(img2path[imgname])\n    plt.imshow(img)\n    plt.show()\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2022-04-18T06:01:26.151368Z","iopub.execute_input":"2022-04-18T06:01:26.152095Z","iopub.status.idle":"2022-04-18T06:01:29.596962Z","shell.execute_reply.started":"2022-04-18T06:01:26.152044Z","shell.execute_reply":"2022-04-18T06:01:29.596339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Looking at some of the images closely associated products are being bought by the user.","metadata":{}},{"cell_type":"code","source":"article_df[article_df.article_id.isin(products)]","metadata":{"execution":{"iopub.status.busy":"2022-04-18T05:47:43.903272Z","iopub.execute_input":"2022-04-18T05:47:43.904078Z","iopub.status.idle":"2022-04-18T05:47:43.940169Z","shell.execute_reply.started":"2022-04-18T05:47:43.904036Z","shell.execute_reply":"2022-04-18T05:47:43.939236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}