{"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":"## Competition goal \nTo predict what articles each customer will purchase in the 7-day period immediately after the training data ends.\n\n## Evaluation Metric\nSubmissions are evaluated according to the Mean Average Precision @ 12 (MAP@12):\n$$\nMAP@12 = \\frac{1}{U} \\sum_{u=1}^{U}  \\sum_{k=1}^{min(n,12)} P(k) \\times rel(k)\n$$\nwhere U is the number of customers, P(k) is the precision at cutoff k, n  is the number predictions per customer, and rel(k) is an indicator function equaling 1 if the item at rank k is a relevant (correct) label, zero otherwise.\n\n### Other important points\n* Some Articles (products) have corresponding images but not all.\n* Up to 12 articles to be predicted for each customer.\n* Predictions are to be made for all customer IDs given in sample_submission.csv irrespective of whether they appear in transactions training data or not.","metadata":{"execution":{"iopub.status.busy":"2022-02-10T04:33:39.625874Z","iopub.execute_input":"2022-02-10T04:33:39.626624Z","iopub.status.idle":"2022-02-10T04:33:39.633471Z","shell.execute_reply.started":"2022-02-10T04:33:39.626583Z","shell.execute_reply":"2022-02-10T04:33:39.63174Z"}}},{"cell_type":"markdown","source":"## Exploring Data files","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport seaborn as sns\nimport plotly.express as px\nimport os","metadata":{"execution":{"iopub.status.busy":"2022-02-12T07:01:02.505135Z","iopub.execute_input":"2022-02-12T07:01:02.505548Z","iopub.status.idle":"2022-02-12T07:01:04.700310Z","shell.execute_reply.started":"2022-02-12T07:01:02.505421Z","shell.execute_reply":"2022-02-12T07:01:04.699630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Exploring articles.csv","metadata":{}},{"cell_type":"code","source":"# articles.csv has all the information about all the articles(products) available that customers can buy\narticles = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/articles.csv',dtype={'article_id': str})\narticles.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-12T07:01:04.701952Z","iopub.execute_input":"2022-02-12T07:01:04.702189Z","iopub.status.idle":"2022-02-12T07:01:06.187137Z","shell.execute_reply.started":"2022-02-12T07:01:04.702159Z","shell.execute_reply":"2022-02-12T07:01:06.186199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-11T05:40:04.27887Z","iopub.execute_input":"2022-02-11T05:40:04.279187Z","iopub.status.idle":"2022-02-11T05:40:04.371581Z","shell.execute_reply.started":"2022-02-11T05:40:04.279138Z","shell.execute_reply":"2022-02-11T05:40:04.370353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Number of unique Articles : {articles.article_id.nunique()}')\nprint(f'Number of unique Product_code : {articles.product_code.nunique()}')\nprint(f'Number of unique Product_type_no : {articles.product_type_no.nunique()}')\nprint(f'Number of unique Product_group_name : {articles.product_group_name.nunique()}')","metadata":{"execution":{"iopub.status.busy":"2022-02-11T05:40:04.373395Z","iopub.execute_input":"2022-02-11T05:40:04.374129Z","iopub.status.idle":"2022-02-11T05:40:04.441045Z","shell.execute_reply.started":"2022-02-11T05:40:04.37405Z","shell.execute_reply":"2022-02-11T05:40:04.43858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let us see what the 19 groups of products are\narticles.product_group_name.unique()","metadata":{"execution":{"iopub.status.busy":"2022-02-11T05:40:04.445286Z","iopub.execute_input":"2022-02-11T05:40:04.445831Z","iopub.status.idle":"2022-02-11T05:40:04.459786Z","shell.execute_reply.started":"2022-02-11T05:40:04.445764Z","shell.execute_reply":"2022-02-11T05:40:04.458767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"product_group_counts = articles.groupby(['product_group_name'])['article_id'].count()\nproduct_group_counts.sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-11T05:40:04.460886Z","iopub.execute_input":"2022-02-11T05:40:04.461213Z","iopub.status.idle":"2022-02-11T05:40:04.48359Z","shell.execute_reply.started":"2022-02-11T05:40:04.461169Z","shell.execute_reply":"2022-02-11T05:40:04.48278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Exploring customers.csv","metadata":{}},{"cell_type":"code","source":"customers_df = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/customers.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-11T05:40:04.484715Z","iopub.execute_input":"2022-02-11T05:40:04.485002Z","iopub.status.idle":"2022-02-11T05:40:09.655871Z","shell.execute_reply.started":"2022-02-11T05:40:04.484972Z","shell.execute_reply":"2022-02-11T05:40:09.655022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-11T05:40:09.657139Z","iopub.execute_input":"2022-02-11T05:40:09.657453Z","iopub.status.idle":"2022-02-11T05:40:09.671835Z","shell.execute_reply.started":"2022-02-11T05:40:09.657414Z","shell.execute_reply":"2022-02-11T05:40:09.670879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-11T05:40:09.673357Z","iopub.execute_input":"2022-02-11T05:40:09.673931Z","iopub.status.idle":"2022-02-11T05:40:09.96728Z","shell.execute_reply.started":"2022-02-11T05:40:09.673887Z","shell.execute_reply":"2022-02-11T05:40:09.966405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* There are 1371980 unique customers\n* Out of which active are 464404","metadata":{}},{"cell_type":"code","source":"customers_df['age'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-02-11T05:40:09.968398Z","iopub.execute_input":"2022-02-11T05:40:09.968613Z","iopub.status.idle":"2022-02-11T05:40:10.031226Z","shell.execute_reply.started":"2022-02-11T05:40:09.968587Z","shell.execute_reply":"2022-02-11T05:40:10.030364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'The average age of customers is {customers_df[\"age\"].mean():.1f} and the median age is {customers_df[\"age\"].median()}')","metadata":{"execution":{"iopub.status.busy":"2022-02-11T05:40:10.032291Z","iopub.execute_input":"2022-02-11T05:40:10.032485Z","iopub.status.idle":"2022-02-11T05:40:10.063455Z","shell.execute_reply.started":"2022-02-11T05:40:10.032461Z","shell.execute_reply":"2022-02-11T05:40:10.062498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.displot(customers_df['age'])","metadata":{"execution":{"iopub.status.busy":"2022-02-11T05:40:10.064542Z","iopub.execute_input":"2022-02-11T05:40:10.064761Z","iopub.status.idle":"2022-02-11T05:40:11.554774Z","shell.execute_reply.started":"2022-02-11T05:40:10.064734Z","shell.execute_reply":"2022-02-11T05:40:11.553921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"## Transactions training data","metadata":{}},{"cell_type":"code","source":"transactions_train_df = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv',dtype={'customer_id': str}, parse_dates=['t_dat'])","metadata":{"execution":{"iopub.status.busy":"2022-02-12T07:06:36.977517Z","iopub.execute_input":"2022-02-12T07:06:36.979233Z","iopub.status.idle":"2022-02-12T07:07:49.168619Z","shell.execute_reply.started":"2022-02-12T07:06:36.979164Z","shell.execute_reply":"2022-02-12T07:07:49.167718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-12T07:08:37.769883Z","iopub.execute_input":"2022-02-12T07:08:37.770201Z","iopub.status.idle":"2022-02-12T07:08:37.785410Z","shell.execute_reply.started":"2022-02-12T07:08:37.770167Z","shell.execute_reply":"2022-02-12T07:08:37.784631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-11T05:41:18.107367Z","iopub.execute_input":"2022-02-11T05:41:18.107574Z","iopub.status.idle":"2022-02-11T05:41:18.130945Z","shell.execute_reply.started":"2022-02-11T05:41:18.107533Z","shell.execute_reply":"2022-02-11T05:41:18.127473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Grouping by sales_channel_id\ntransactions_train_df.groupby(['sales_channel_id']).agg(article_id_count=('article_id','count'),\n                                                        customer_id_count= ('customer_id','count'))","metadata":{"execution":{"iopub.status.busy":"2022-02-11T06:30:00.126484Z","iopub.execute_input":"2022-02-11T06:30:00.127203Z","iopub.status.idle":"2022-02-11T06:30:02.600939Z","shell.execute_reply.started":"2022-02-11T06:30:00.127154Z","shell.execute_reply":"2022-02-11T06:30:02.600142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(y=transactions_train_df['price'],color='yellow')","metadata":{"execution":{"iopub.status.busy":"2022-02-11T06:30:46.801753Z","iopub.execute_input":"2022-02-11T06:30:46.802554Z","iopub.status.idle":"2022-02-11T06:30:54.185511Z","shell.execute_reply.started":"2022-02-11T06:30:46.802515Z","shell.execute_reply":"2022-02-11T06:30:54.184955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"date_range = str(transactions_train_df['t_dat'].dt.date.min()) + ' to ' +str(transactions_train_df['t_dat'].dt.date.max())\nprint(date_range)","metadata":{"execution":{"iopub.status.busy":"2022-02-11T05:41:18.132841Z","iopub.execute_input":"2022-02-11T05:41:18.133981Z","iopub.status.idle":"2022-02-11T05:41:40.247462Z","shell.execute_reply.started":"2022-02-11T05:41:18.133929Z","shell.execute_reply":"2022-02-11T05:41:40.246476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = transactions_train_df.groupby(['t_dat',\"sales_channel_id\"])['price'].agg(['sum']).sort_values(by = 't_dat').reset_index()\nfig = px.bar( df, x='t_dat', y='sum', title='Daily Sales',color=\"sales_channel_id\", labels={'t_dat':'Transaction Date','sum':'Total Sales'})\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-12T07:12:04.105509Z","iopub.execute_input":"2022-02-12T07:12:04.108029Z","iopub.status.idle":"2022-02-12T07:12:06.460077Z","shell.execute_reply.started":"2022-02-12T07:12:04.107937Z","shell.execute_reply":"2022-02-12T07:12:06.458956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# List article ids in descending order of total customer counts and total amount of sales\ntransactions_train_df.groupby(['article_id']).agg({'customer_id':'count','price':'sum'}).sort_values(by=['customer_id','price'],ascending=[False,False])","metadata":{"execution":{"iopub.status.busy":"2022-02-11T05:57:16.76024Z","iopub.execute_input":"2022-02-11T05:57:16.761149Z","iopub.status.idle":"2022-02-11T05:57:19.313542Z","shell.execute_reply.started":"2022-02-11T05:57:16.761074Z","shell.execute_reply":"2022-02-11T05:57:19.312759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Transactions_train.csv is a big file with more than 3 million transaction records spanning almost 2 years from 2018-09-20 to 2020-09-22","metadata":{}},{"cell_type":"markdown","source":"## sample_submission file","metadata":{}},{"cell_type":"code","source":"sample_submission_df = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-11T05:41:40.248498Z","iopub.execute_input":"2022-02-11T05:41:40.248708Z","iopub.status.idle":"2022-02-11T05:41:44.995079Z","shell.execute_reply.started":"2022-02-11T05:41:40.248682Z","shell.execute_reply":"2022-02-11T05:41:44.994187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_df.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-11T05:41:44.996458Z","iopub.execute_input":"2022-02-11T05:41:44.996677Z","iopub.status.idle":"2022-02-11T05:41:45.133476Z","shell.execute_reply.started":"2022-02-11T05:41:44.996651Z","shell.execute_reply":"2022-02-11T05:41:45.132607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_df['customer_id'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-02-11T05:41:45.134914Z","iopub.execute_input":"2022-02-11T05:41:45.135983Z","iopub.status.idle":"2022-02-11T05:41:45.737012Z","shell.execute_reply.started":"2022-02-11T05:41:45.135945Z","shell.execute_reply":"2022-02-11T05:41:45.736173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Work in Progress!\n* Images\n* transactions deep dive ","metadata":{}}]}