{"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":"[](https://www.qwant.com/?client=brz-brave&t=images&q=H%26M&o=0%3AD0088AD8AB7DF001697104F51E1D4EDFCA0CCA1B)","metadata":{}},{"cell_type":"markdown","source":"<img src=\"https://images.unsplash.com/photo-1578983662508-41895226ebfb?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1211&q=80\" width=600></img>\n\n\nData: There are four csv files:\n\n* articles.csv: A dictionary of each of every product sold by H&M with its characteristics.\n* transactions_train.csv: Our main data file, which showcases all of training relevant data.\n* customers.csv: A dictionary related to each customer. (like articles, but with customer related info)\n* submission.csv: A sample on how to make a submission format.\n","metadata":{}},{"cell_type":"code","source":"#Import packages\nimport os\nimport pandas as pd\nimport numpy as np\nimport plotly\nfrom pathlib import Path\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport umap\nimport sklearn.cluster as cluster\nfrom sklearn.metrics import adjusted_rand_score, adjusted_mutual_info_score","metadata":{"execution":{"iopub.status.busy":"2022-02-17T19:11:59.655751Z","iopub.execute_input":"2022-02-17T19:11:59.656167Z","iopub.status.idle":"2022-02-17T19:11:59.662303Z","shell.execute_reply.started":"2022-02-17T19:11:59.656130Z","shell.execute_reply":"2022-02-17T19:11:59.661371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('/kaggle/input/h-and-m-personalized-fashion-recommendations/')\narticles = pd.read_csv(path / 'articles.csv')\ncustomers = pd.read_csv(path / 'customers.csv')\ntransaction = pd.read_csv(path / 'transactions_train.csv')\n","metadata":{"execution":{"iopub.status.busy":"2022-02-17T19:11:59.664101Z","iopub.execute_input":"2022-02-17T19:11:59.664712Z","iopub.status.idle":"2022-02-17T19:12:46.468290Z","shell.execute_reply.started":"2022-02-17T19:11:59.664673Z","shell.execute_reply":"2022-02-17T19:12:46.465797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 1.Data head and structure","metadata":{}},{"cell_type":"code","source":"print(articles.info())\nprint(\"#\"*30)\nprint(\"missing data:\",articles.isnull().sum().sort_values(ascending = False))\narticles.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-02-17T19:12:46.471418Z","iopub.execute_input":"2022-02-17T19:12:46.472153Z","iopub.status.idle":"2022-02-17T19:12:47.002703Z","shell.execute_reply.started":"2022-02-17T19:12:46.472031Z","shell.execute_reply":"2022-02-17T19:12:47.000357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* For the articles we have 416 data missing for the detail description otherwise there is no others missing data.\n* We have a lot of different kind of articles, we could aggregate them by class.","metadata":{}},{"cell_type":"code","source":"print(customers.info())\nprint(\"#\"*30)\nprint(\"missing data:\",customers.isnull().sum().sort_values(ascending = False))\ncustomers.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-02-17T19:12:47.022548Z","iopub.execute_input":"2022-02-17T19:12:47.024699Z","iopub.status.idle":"2022-02-17T19:12:48.601590Z","shell.execute_reply.started":"2022-02-17T19:12:47.024353Z","shell.execute_reply":"2022-02-17T19:12:48.600557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* the customers file has many missing data, all most of columns contain missing data.\n","metadata":{}},{"cell_type":"code","source":"transaction['t_dat'] = pd.to_datetime(transaction['t_dat'])\ntransaction.dtypes\nprint(transaction.info())\nprint(\"#\"*30)\nprint(\"missing data:\",transaction.isnull().sum().sort_values(ascending = False))\ntransaction.head(2)","metadata":{"execution":{"iopub.status.busy":"2022-02-17T19:12:48.604667Z","iopub.execute_input":"2022-02-17T19:12:48.604974Z","iopub.status.idle":"2022-02-17T19:12:59.803657Z","shell.execute_reply.started":"2022-02-17T19:12:48.604935Z","shell.execute_reply":"2022-02-17T19:12:59.802708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* The transaction file has no missing data\n* We can like the transaction file with the others by:\n                                                    - the customer_id => customers\n                                                    - article_id => articles","metadata":{}},{"cell_type":"markdown","source":"# 2.Visualisations:","metadata":{}},{"cell_type":"markdown","source":"## 2.1Transaction data\n\n## What is the best sales channel?","metadata":{}},{"cell_type":"code","source":"cust_ch = transaction.groupby(['sales_channel_id'])['customer_id'].count()\nplt.figure(figsize = (8,6))\ng1 = sns.barplot(x = cust_ch.index,  y= cust_ch.values)\nplt.title(f'Number of customers by channel')\nlocs,labels = plt.xticks()\ncust_ch","metadata":{"execution":{"iopub.status.busy":"2022-02-17T19:12:59.804844Z","iopub.execute_input":"2022-02-17T19:12:59.805047Z","iopub.status.idle":"2022-02-17T19:13:04.001372Z","shell.execute_reply.started":"2022-02-17T19:12:59.805021Z","shell.execute_reply":"2022-02-17T19:13:04.000366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* The channel 2 is the best channel of we have a lot of clients.\n","metadata":{}},{"cell_type":"markdown","source":"## What are the 100 best buyers?","metadata":{}},{"cell_type":"code","source":"top1 = transaction.groupby(['sales_channel_id','customer_id'])['price'].sum()\ntop10 = top1.reset_index()\ntop100 = top10.sort_values(by =['price'],ascending=False)[:100]\ntop100\n\nplt.figure(figsize = (20,8))\ng2 = sns.barplot(x = top100.customer_id,  y= top100.price, hue = top100.sales_channel_id)\ng2.set_xticklabels(g2.get_xticklabels(),rotation=90)\nplt.title(f'100 Best Buyers')\nlocs,labels = plt.xticks()","metadata":{"execution":{"iopub.status.busy":"2022-02-17T19:13:04.002982Z","iopub.execute_input":"2022-02-17T19:13:04.003326Z","iopub.status.idle":"2022-02-17T19:13:26.833429Z","shell.execute_reply.started":"2022-02-17T19:13:04.003280Z","shell.execute_reply":"2022-02-17T19:13:26.832284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## What are the 50 best days of sales?","metadata":{}},{"cell_type":"code","source":"date = transaction.groupby(['t_dat', 'sales_channel_id'])['price'].sum()\ndate = date.reset_index()\ndate_50 = date.sort_values(by =['price'],ascending=False)[:50]\n\nplt.figure(figsize = (18,6))\ng3 = sns.barplot(x = date_50.t_dat,  y= date_50.price, hue = date_50.sales_channel_id)\ng3.set_xticklabels(g3.get_xticklabels(),rotation=90)\nlocs,labels = plt.xticks()\nplt.title(f'The 50 top best days of sales')","metadata":{"execution":{"iopub.status.busy":"2022-02-17T19:13:26.834988Z","iopub.execute_input":"2022-02-17T19:13:26.835568Z","iopub.status.idle":"2022-02-17T19:13:30.271951Z","shell.execute_reply.started":"2022-02-17T19:13:26.835533Z","shell.execute_reply":"2022-02-17T19:13:30.270854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* The best days and the best clients are both on the sales channel 2.","metadata":{}},{"cell_type":"markdown","source":"## 2.2Customers Data\n\n### What is the age distribution of the clients?","metadata":{}},{"cell_type":"code","source":"age1 = customers.groupby(['age'])['customer_id'].count()\nage = age1.sort_values(ascending = False)\nplt.figure(figsize = (20,8))\ng4 = sns.barplot(x = age.index,  y= age.values)\ng4.set_xticklabels(g4.get_xticklabels(),rotation=90)\nplt.title(f'Number of customers per age')","metadata":{"execution":{"iopub.status.busy":"2022-02-17T19:13:30.273527Z","iopub.execute_input":"2022-02-17T19:13:30.273797Z","iopub.status.idle":"2022-02-17T19:13:32.237879Z","shell.execute_reply.started":"2022-02-17T19:13:30.273757Z","shell.execute_reply":"2022-02-17T19:13:32.237045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* most of the clients are young.","metadata":{}},{"cell_type":"markdown","source":"### What is the distrubition of each fashion News Frequency by customers?","metadata":{}},{"cell_type":"code","source":"freq = customers.groupby([\"fashion_news_frequency\"])[\"customer_id\"].count().sort_values(ascending = False)\nplt.figure(figsize = (8,8))\ng3 = sns.barplot(x = freq.index,  y= freq.values)\ng3.set_xticklabels(g3.get_xticklabels(),rotation=90)\nplt.title(f'Number of customers per each Fashion News Frequency')","metadata":{"execution":{"iopub.status.busy":"2022-02-17T19:13:32.239273Z","iopub.execute_input":"2022-02-17T19:13:32.239588Z","iopub.status.idle":"2022-02-17T19:13:32.770573Z","shell.execute_reply.started":"2022-02-17T19:13:32.239546Z","shell.execute_reply":"2022-02-17T19:13:32.769529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fashion = customers.loc[customers.fashion_news_frequency == 'NONE']\nfash = fashion.groupby(['age'])['customer_id'].count()\nplt.figure(figsize = (16,8))\ng4 = sns.barplot(x = fash.index,  y= fash.values)\ng4.set_xticklabels(g4.get_xticklabels(),rotation=90)\nplt.title(f'Number of None customers by age')\n\nfashion1 = customers.loc[customers.fashion_news_frequency == 'Regularly']\nfash1 = fashion1.groupby(['age'])['customer_id'].count()\nplt.figure(figsize = (16,8))\ng5 = sns.barplot(x = fash1.index,  y= fash1.values)\ng5.set_xticklabels(g5.get_xticklabels(),rotation=90)\nplt.title(f'Number of customers regularly by age')","metadata":{"execution":{"iopub.status.busy":"2022-02-17T19:13:32.771759Z","iopub.execute_input":"2022-02-17T19:13:32.771978Z","iopub.status.idle":"2022-02-17T19:13:36.148598Z","shell.execute_reply.started":"2022-02-17T19:13:32.771941Z","shell.execute_reply":"2022-02-17T19:13:36.147802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* The fashion news frequency has no significant value, there is  the same age distribution for the clients between None and regularly.","metadata":{}},{"cell_type":"markdown","source":"## 2.3Articles Data","metadata":{}},{"cell_type":"markdown","source":"### Distribution of the articles by product type name","metadata":{}},{"cell_type":"code","source":"product = articles.groupby(['product_type_name'])['article_id'].count().sort_values(ascending=False)\nplt.figure(figsize = (22,6))\ng6 = sns.barplot(x = product.index,  y= product.values)\ng6.set_xticklabels(g6.get_xticklabels(),rotation=90)\nplt.title(f'Number of articles by product type')","metadata":{"execution":{"iopub.status.busy":"2022-02-17T19:13:36.150068Z","iopub.execute_input":"2022-02-17T19:13:36.151900Z","iopub.status.idle":"2022-02-17T19:13:38.498152Z","shell.execute_reply.started":"2022-02-17T19:13:36.151853Z","shell.execute_reply":"2022-02-17T19:13:38.497317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Distribution of the articles by garment group?","metadata":{}},{"cell_type":"code","source":"garment = articles.groupby(['garment_group_name'])['article_id'].count().sort_values(ascending=False)\nplt.figure(figsize = (16,8))\ng7 = sns.barplot(x = garment.index,  y= garment.values)\ng7.set_xticklabels(g7.get_xticklabels(),rotation=90)\nplt.title(f'Number of articles by Garment group name')","metadata":{"execution":{"iopub.status.busy":"2022-02-17T19:13:38.499834Z","iopub.execute_input":"2022-02-17T19:13:38.500143Z","iopub.status.idle":"2022-02-17T19:13:38.901154Z","shell.execute_reply.started":"2022-02-17T19:13:38.500100Z","shell.execute_reply":"2022-02-17T19:13:38.899760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Distribution of the articles by section name","metadata":{}},{"cell_type":"code","source":"section = articles.groupby(['section_name'])['article_id'].count().sort_values(ascending=False)\nplt.figure(figsize = (22,6))\ng8 = sns.barplot(x = section.index,  y= section.values)\ng8.set_xticklabels(g8.get_xticklabels(),rotation=90)\nplt.title(f'Number of articles by section name')","metadata":{"execution":{"iopub.status.busy":"2022-02-17T19:13:38.903508Z","iopub.execute_input":"2022-02-17T19:13:38.903743Z","iopub.status.idle":"2022-02-17T19:13:40.186341Z","shell.execute_reply.started":"2022-02-17T19:13:38.903714Z","shell.execute_reply":"2022-02-17T19:13:40.185487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Distribution of the articles by index group name","metadata":{}},{"cell_type":"code","source":"product = articles.groupby(['index_group_name'])['article_id'].nunique().sort_values(ascending=False)\nplt.figure(figsize = (22,6))\ng8 = sns.barplot(x = product.index,  y= product.values)\nplt.title(f'Number of articles by index group name')","metadata":{"execution":{"iopub.status.busy":"2022-02-17T19:13:40.187454Z","iopub.execute_input":"2022-02-17T19:13:40.187686Z","iopub.status.idle":"2022-02-17T19:13:40.443306Z","shell.execute_reply.started":"2022-02-17T19:13:40.187658Z","shell.execute_reply":"2022-02-17T19:13:40.442699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Index group name is a good column to filter by a subcategory of articles.\n'Ladieswear', 'Baby/Children', 'Menswear', 'Sport', 'Divided'\n\n","metadata":{}},{"cell_type":"markdown","source":"# 3.Merge the transaction and article table\n### what is the total revenue by index group name","metadata":{}},{"cell_type":"code","source":"df = transaction[['customer_id', 'article_id','price']]\nartic1 = articles[['article_id','product_type_no','product_type_name','index_group_name','section_name']]\nres = artic1.merge(df,left_on='article_id', right_on='article_id', how='left')\nbest_article = res.groupby(['index_group_name'])['price'].sum().sort_values(ascending=False)\nplt.figure(figsize = (18,8))\ng9 = sns.barplot(x = best_article.index,  y= best_article.values)\nplt.title(f'Revenue by index group name')","metadata":{"execution":{"iopub.status.busy":"2022-02-17T19:13:40.444237Z","iopub.execute_input":"2022-02-17T19:13:40.444777Z","iopub.status.idle":"2022-02-17T19:14:03.517098Z","shell.execute_reply.started":"2022-02-17T19:13:40.444740Z","shell.execute_reply":"2022-02-17T19:14:03.516309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"* Ladieswear is the most profitable","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}