{"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":"## **<span style=\"color:#023e8a;font-size:200%\"><center> 🔥🔥EDA H&M🔥🔥</center></span>**\n## **<center><span style=\"color:#FEF1FE;background-color:#023e8a;border-radius: 5px;padding: 5px\">If you find this notebook useful or interesting, please, support with an upvote :)</span></center>**","metadata":{}},{"cell_type":"markdown","source":"## **<span style=\"color:#023e8a;font-size:1000%\"><center>EDA</center></span><span style=\"color:#023e8a;font-size:200%\"><center>Exploratory Data Analysis. H&M</center></span>**","metadata":{}},{"cell_type":"markdown","source":"# **<a id=\"Content\" style=\"color:#023e8a;\">Table of Content</a>**\n* [**<span style=\"color:#023e8a;\">1. First steps</span>**](#First)  \n* [**<span style=\"color:#023e8a;\">2. Articles</span>**](#Articles)  \n* [**<span style=\"color:#023e8a;\">3. Customers</span>**](#Customers)  \n* [**<span style=\"color:#023e8a;\">4. Transactions</span>**](#Transactions)  \n* [**<span style=\"color:#023e8a;\">5. Images with description and price</span>**](#Images)  \n","metadata":{}},{"cell_type":"markdown","source":"## **<span style=\"color:#023e8a;\">Intro</span>**\n\n**<span style=\"color:#023e8a;\">The competition is dedicated to the product recomendations (H&M)  </span>**\n\n**<span style=\"color:#023e8a;\">Here we have different kinds of data that help us to get good recomendations: </span>**\n\n📸 `images` - images of every article_id\n\n🙋 `articles`  - detailed metadata of every article_id\n\n👔 `customers`  - detailed metadata of every customer_id\n\n🧾 `transactions_train`  - purchases with details","metadata":{}},{"cell_type":"markdown","source":"## **<span id=\"First\" style=\"color:#023e8a;\">1. First steps</span>**","metadata":{}},{"cell_type":"markdown","source":"[**<span style=\"color:#FEF1FE;background-color:#023e8a;border-radius: 5px;padding: 2px\">Go to Table of Content</span>**](#Content)","metadata":{}},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> The first step as always: load the data :)</span>**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom matplotlib import pyplot as plt\nfrom tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:13:53.749831Z","iopub.execute_input":"2022-03-12T21:13:53.750239Z","iopub.status.idle":"2022-03-12T21:13:54.775407Z","shell.execute_reply.started":"2022-03-12T21:13:53.7502Z","shell.execute_reply":"2022-03-12T21:13:54.774691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/articles.csv\")\ncustomers = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/customers.csv\")\ntransactions = pd.read_csv(\"../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:13:54.777051Z","iopub.execute_input":"2022-03-12T21:13:54.777359Z","iopub.status.idle":"2022-03-12T21:15:02.706611Z","shell.execute_reply.started":"2022-03-12T21:13:54.777321Z","shell.execute_reply":"2022-03-12T21:15:02.705534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Let's look at the tables and try to get some outcomes about data inside.</span>**","metadata":{}},{"cell_type":"markdown","source":"## **<span id=\"Articles\" style=\"color:#023e8a;\">2. Articles</span>**","metadata":{}},{"cell_type":"markdown","source":"[**<span style=\"color:#FEF1FE;background-color:#023e8a;border-radius: 5px;padding: 2px\">Go to Table of Content</span>**](#Content)","metadata":{}},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> This table contains all h&m articles with details such as a type of product, a color, a product group and other features.</span>**  \n**<span style=\"color:#023e8a;\"> Article data description: </span>**\n\n> `article_id` **<span style=\"color:#023e8a;\">: A unique identifier of every article.</span>**  \n> `product_code`, `prod_name` **<span style=\"color:#023e8a;\">: A unique identifier of every product and its name (not the same).</span>**  \n> `product_type`, `product_type_name` **<span style=\"color:#023e8a;\">: The group of product_code and its name</span>**  \n> `graphical_appearance_no`, `graphical_appearance_name` **<span style=\"color:#023e8a;\">: The group of graphics and its name</span>**  \n> `colour_group_code`, `colour_group_name` **<span style=\"color:#023e8a;\">: The group of color and its name</span>**  \n> `perceived_colour_value_id`, `perceived_colour_value_name`, `perceived_colour_master_id`, `perceived_colour_master_name` **<span style=\"color:#023e8a;\">: The added color info</span>**  \n> `department_no`, `department_name`: **<span style=\"color:#023e8a;\">: A unique identifier of every dep and its name</span>**  \n> `index_code`, `index_name`: **<span style=\"color:#023e8a;\">: A unique identifier of every index and its name</span>**  \n> `index_group_no`, `index_group_name`: **<span style=\"color:#023e8a;\">: A group of indeces and its name</span>**  \n> `section_no`, `section_name`: **<span style=\"color:#023e8a;\">: A unique identifier of every section and its name</span>**  \n> `garment_group_no`, `garment_group_name`: **<span style=\"color:#023e8a;\">: A unique identifier of every garment and its name</span>**  \n> `detail_desc`: **<span style=\"color:#023e8a;\">: Details</span>**  ","metadata":{}},{"cell_type":"code","source":"articles.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:02.708332Z","iopub.execute_input":"2022-03-12T21:15:02.708657Z","iopub.status.idle":"2022-03-12T21:15:02.747678Z","shell.execute_reply.started":"2022-03-12T21:15:02.708616Z","shell.execute_reply":"2022-03-12T21:15:02.746823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\">Ladieswear accounts for a significant part of all dresses. Sportswear has the least portion.</span>**","metadata":{}},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(15, 7))\nax = sns.histplot(data=articles, y='index_name', color='orange')\nax.set_xlabel('count by index name')\nax.set_ylabel('index name')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:02.749391Z","iopub.execute_input":"2022-03-12T21:15:02.749645Z","iopub.status.idle":"2022-03-12T21:15:03.088267Z","shell.execute_reply.started":"2022-03-12T21:15:02.749614Z","shell.execute_reply":"2022-03-12T21:15:03.087227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> The garments grouped by index: Jersey fancy is the most frequent garment, especially for women and children. The next by number is accessories, many various accessories with low price.</span>**","metadata":{}},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(15, 7))\nax = sns.histplot(data=articles, y='garment_group_name', color='orange', hue='index_group_name', multiple='stack')\nax.set_xlabel('count by garment group')\nax.set_ylabel('garment group')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:03.08937Z","iopub.execute_input":"2022-03-12T21:15:03.089583Z","iopub.status.idle":"2022-03-12T21:15:03.82162Z","shell.execute_reply.started":"2022-03-12T21:15:03.089556Z","shell.execute_reply":"2022-03-12T21:15:03.820742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\">Now, pay attention to index group-index structure. Ladieswear and Children/Baby have subgroups.</span>**","metadata":{}},{"cell_type":"code","source":"articles.groupby(['index_group_name', 'index_name']).count()['article_id']","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:03.822729Z","iopub.execute_input":"2022-03-12T21:15:03.822958Z","iopub.status.idle":"2022-03-12T21:15:03.926803Z","shell.execute_reply.started":"2022-03-12T21:15:03.82293Z","shell.execute_reply":"2022-03-12T21:15:03.925788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> And look at the product group-product structure. Accessories are really various, the most numerious: bags, earrings and hats. However, trousers prevail.</span>**","metadata":{}},{"cell_type":"code","source":"pd.options.display.max_rows = None\narticles.groupby(['product_group_name', 'product_type_name']).count()['article_id']","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:03.928791Z","iopub.execute_input":"2022-03-12T21:15:03.929137Z","iopub.status.idle":"2022-03-12T21:15:04.034844Z","shell.execute_reply.started":"2022-03-12T21:15:03.929097Z","shell.execute_reply":"2022-03-12T21:15:04.033842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> And the table with number of unique values in columns:</span>**","metadata":{}},{"cell_type":"code","source":"for col in articles.columns:\n    if not 'no' in col and not 'code' in col and not 'id' in col:\n        un_n = articles[col].nunique()\n        print(f'n of unique {col}: {un_n}')","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:04.036541Z","iopub.execute_input":"2022-03-12T21:15:04.036953Z","iopub.status.idle":"2022-03-12T21:15:04.162359Z","shell.execute_reply.started":"2022-03-12T21:15:04.036908Z","shell.execute_reply":"2022-03-12T21:15:04.161363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span id=\"Customers\" style=\"color:#023e8a;\">3. Customers</span>**","metadata":{}},{"cell_type":"markdown","source":"[**<span style=\"color:#FEF1FE;background-color:#023e8a;border-radius: 5px;padding: 2px\">Go to Table of Content</span>**](#Content)","metadata":{}},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Customers data description: </span>**\n\n> `customer_id` **<span style=\"color:#023e8a;\">: A unique identifier of every customer</span>**  \n> `FN` **<span style=\"color:#023e8a;\">: 1 or missed </span>**  \n> `Active` **<span style=\"color:#023e8a;\">: 1 or missed</span>**  \n> `club_member_status` **<span style=\"color:#023e8a;\">: Status in club</span>**  \n> `fashion_news_frequency` **<span style=\"color:#023e8a;\">: How often H&M may send news to customer</span>**  \n> `age` **<span style=\"color:#023e8a;\">: The current age</span>**  \n> `postal_code` **<span style=\"color:#023e8a;\">: Postal code of customer</span>**  ","metadata":{}},{"cell_type":"code","source":"pd.options.display.max_rows = 50\ncustomers.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:04.163462Z","iopub.execute_input":"2022-03-12T21:15:04.164152Z","iopub.status.idle":"2022-03-12T21:15:04.180161Z","shell.execute_reply.started":"2022-03-12T21:15:04.164112Z","shell.execute_reply":"2022-03-12T21:15:04.179279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> There are no duplicates in </span>** `customers`","metadata":{}},{"cell_type":"code","source":"customers.shape[0] - customers['customer_id'].nunique()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:04.183095Z","iopub.execute_input":"2022-03-12T21:15:04.183688Z","iopub.status.idle":"2022-03-12T21:15:04.828211Z","shell.execute_reply.started":"2022-03-12T21:15:04.18364Z","shell.execute_reply":"2022-03-12T21:15:04.826977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Here we have abnormal number of customers by one postal code. One has 120303, it might be encoded nan adress or smth like a huge distribution center, or pickup.</span>**","metadata":{}},{"cell_type":"code","source":"data_postal = customers.groupby('postal_code', as_index=False).count().sort_values('customer_id', ascending=False)\ndata_postal.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:04.830036Z","iopub.execute_input":"2022-03-12T21:15:04.830367Z","iopub.status.idle":"2022-03-12T21:15:06.46822Z","shell.execute_reply.started":"2022-03-12T21:15:04.830322Z","shell.execute_reply":"2022-03-12T21:15:06.467577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Ages, club_member_status are different, like customer_ids.</span>**","metadata":{}},{"cell_type":"code","source":"customers[customers['postal_code']=='2c29ae653a9282cce4151bd87643c907644e09541abc28ae87dea0d1f6603b1c'].head(5)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:06.469263Z","iopub.execute_input":"2022-03-12T21:15:06.469584Z","iopub.status.idle":"2022-03-12T21:15:06.613774Z","shell.execute_reply.started":"2022-03-12T21:15:06.469556Z","shell.execute_reply":"2022-03-12T21:15:06.613158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> The most common age is about 21-23</span>**","metadata":{}},{"cell_type":"code","source":"sns.set_style('darkgrid')\nf, ax = plt.subplots(figsize=(10, 5))\nax = sns.histplot(data=customers, x='age', bins=50, color='orange')\nax.set_xlabel('Distribution of the customer age')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:06.614776Z","iopub.execute_input":"2022-03-12T21:15:06.615091Z","iopub.status.idle":"2022-03-12T21:15:07.130864Z","shell.execute_reply.started":"2022-03-12T21:15:06.615065Z","shell.execute_reply":"2022-03-12T21:15:07.129957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Status in H&M club. Almost every customer has an active club status, some of them begin to activate it (pre-create). A tiny part of customers abandoned the club.</span>**","metadata":{}},{"cell_type":"code","source":"sns.set_style(\"darkgrid\")\nf, ax = plt.subplots(figsize=(10,5))\nax = sns.histplot(data=customers, x='club_member_status', color='orange')\nax.set_xlabel('Distribution of club member status')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:07.132156Z","iopub.execute_input":"2022-03-12T21:15:07.132373Z","iopub.status.idle":"2022-03-12T21:15:08.230347Z","shell.execute_reply.started":"2022-03-12T21:15:07.132348Z","shell.execute_reply":"2022-03-12T21:15:08.229689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Here we have three types for NO DATA. Let's unite these values.</span>**","metadata":{}},{"cell_type":"code","source":"customers['fashion_news_frequency'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:08.231522Z","iopub.execute_input":"2022-03-12T21:15:08.232352Z","iopub.status.idle":"2022-03-12T21:15:08.317625Z","shell.execute_reply.started":"2022-03-12T21:15:08.232297Z","shell.execute_reply":"2022-03-12T21:15:08.316791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.loc[~customers['fashion_news_frequency'].isin(['Regularly', 'Monthly']), 'fashion_news_frequency'] = 'None'\ncustomers['fashion_news_frequency'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:08.319052Z","iopub.execute_input":"2022-03-12T21:15:08.319287Z","iopub.status.idle":"2022-03-12T21:15:08.494334Z","shell.execute_reply.started":"2022-03-12T21:15:08.31926Z","shell.execute_reply":"2022-03-12T21:15:08.493569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pie_data = customers[['customer_id', 'fashion_news_frequency']].groupby('fashion_news_frequency').count()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:08.495617Z","iopub.execute_input":"2022-03-12T21:15:08.495834Z","iopub.status.idle":"2022-03-12T21:15:08.741867Z","shell.execute_reply.started":"2022-03-12T21:15:08.495808Z","shell.execute_reply":"2022-03-12T21:15:08.741131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Customers prefer not to get any messages about the current news.</span>**","metadata":{}},{"cell_type":"code","source":"sns.set_style(\"darkgrid\")\nf, ax = plt.subplots(figsize=(10,5))\n# ax = sns.histplot(data=customers, x='fashion_news_frequency', color='orange')\n# ax = sns.pie(data=customers, x='fashion_news_frequency', color='orange')\ncolors = sns.color_palette('pastel')\nax.pie(pie_data.customer_id, labels=pie_data.index, colors = colors)\nax.set_facecolor('lightgrey')\nax.set_xlabel('Distribution of fashion news frequency')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:08.743136Z","iopub.execute_input":"2022-03-12T21:15:08.74335Z","iopub.status.idle":"2022-03-12T21:15:08.862144Z","shell.execute_reply.started":"2022-03-12T21:15:08.743326Z","shell.execute_reply":"2022-03-12T21:15:08.861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span id=\"Transactions\" style=\"color:#023e8a;\">4. Transactions</span>**","metadata":{}},{"cell_type":"markdown","source":"[**<span style=\"color:#FEF1FE;background-color:#023e8a;border-radius: 5px;padding: 2px\">Go to Table of Content</span>**](#Content)","metadata":{}},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Transactions data description: </span>**\n\n> `t_dat` **<span style=\"color:#023e8a;\">: A unique identifier of every customer</span>**  \n> `customer_id` **<span style=\"color:#023e8a;\">: A unique identifier of every customer </span>**  **<span style=\"color:#FF0000;\">(in </span>** `customers` **<span style=\"color:#FF0000;\"> table)</span>**  \n> `article_id` **<span style=\"color:#023e8a;\">: A unique identifier of every article</span>**  **<span style=\"color:#FF0000;\">(in </span>** `articles` **<span style=\"color:#FF0000;\"> table)</span>**  \n> `price` **<span style=\"color:#023e8a;\">: Price of purchase</span>**  \n> `sales_channel_id` **<span style=\"color:#023e8a;\">: 1 or 2</span>**  ","metadata":{}},{"cell_type":"code","source":"transactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:08.863693Z","iopub.execute_input":"2022-03-12T21:15:08.864037Z","iopub.status.idle":"2022-03-12T21:15:08.882226Z","shell.execute_reply.started":"2022-03-12T21:15:08.863982Z","shell.execute_reply":"2022-03-12T21:15:08.880994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Here we see outliers for price. </span>**","metadata":{}},{"cell_type":"code","source":"pd.set_option('display.float_format', '{:.4f}'.format)\ntransactions.describe()['price']","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:08.884035Z","iopub.execute_input":"2022-03-12T21:15:08.884461Z","iopub.status.idle":"2022-03-12T21:15:12.017867Z","shell.execute_reply.started":"2022-03-12T21:15:08.884421Z","shell.execute_reply":"2022-03-12T21:15:12.017046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:12.019041Z","iopub.execute_input":"2022-03-12T21:15:12.019357Z","iopub.status.idle":"2022-03-12T21:15:12.029985Z","shell.execute_reply.started":"2022-03-12T21:15:12.019325Z","shell.execute_reply":"2022-03-12T21:15:12.02914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.set_style(\"darkgrid\")\nf, ax = plt.subplots(figsize=(10,5))\nax = sns.boxplot(data=transactions, x='price', color='orange')\nax.set_xlabel('Price outliers')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:12.031418Z","iopub.execute_input":"2022-03-12T21:15:12.031951Z","iopub.status.idle":"2022-03-12T21:15:16.665672Z","shell.execute_reply.started":"2022-03-12T21:15:12.031918Z","shell.execute_reply":"2022-03-12T21:15:16.664713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Top 10 customers by num of transactions. </span>**","metadata":{}},{"cell_type":"code","source":"transactions_byid = transactions.groupby('customer_id').count()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:16.666917Z","iopub.execute_input":"2022-03-12T21:15:16.667205Z","iopub.status.idle":"2022-03-12T21:15:30.902681Z","shell.execute_reply.started":"2022-03-12T21:15:16.66717Z","shell.execute_reply":"2022-03-12T21:15:30.902029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_byid.sort_values(by='price', ascending=False)['price'][:10]","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:30.903719Z","iopub.execute_input":"2022-03-12T21:15:30.904354Z","iopub.status.idle":"2022-03-12T21:15:31.488112Z","shell.execute_reply.started":"2022-03-12T21:15:30.904321Z","shell.execute_reply":"2022-03-12T21:15:31.487519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> However, comparing prices inside groups is more accurate, because accessories and trousers prices may vary largerly. </span>**","metadata":{}},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Get subset from articles and merge it to transactions. </span>**","metadata":{}},{"cell_type":"code","source":"articles.columns","metadata":{"execution":{"iopub.status.busy":"2022-03-12T21:15:31.490526Z","iopub.execute_input":"2022-03-12T21:15:31.490948Z","iopub.status.idle":"2022-03-12T21:15:31.497023Z","shell.execute_reply.started":"2022-03-12T21:15:31.490917Z","shell.execute_reply":"2022-03-12T21:15:31.496463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles_for_merge = articles[['article_id', 'prod_name', 'product_type_name', 'product_group_name', 'index_name']]","metadata":{"execution":{"iopub.status.busy":"2022-03-07T23:54:26.256543Z","iopub.execute_input":"2022-03-07T23:54:26.256884Z","iopub.status.idle":"2022-03-07T23:54:26.26806Z","shell.execute_reply.started":"2022-03-07T23:54:26.256853Z","shell.execute_reply":"2022-03-07T23:54:26.26691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles_for_merge = transactions[['customer_id', 'article_id', 'price', 't_dat']].merge(articles_for_merge, on='article_id', how='left')","metadata":{"execution":{"iopub.status.busy":"2022-03-07T23:54:26.768021Z","iopub.execute_input":"2022-03-07T23:54:26.768282Z","iopub.status.idle":"2022-03-07T23:54:38.124031Z","shell.execute_reply.started":"2022-03-07T23:54:26.768254Z","shell.execute_reply":"2022-03-07T23:54:38.121237Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Here we see outliers for group name prices. Lower/Upper/Full body have a huge price variance. I guess it could be like some unique collections, relative to casual ones. Some high price articles even belong to accessories group.</span>**","metadata":{}},{"cell_type":"code","source":"sns.set_style(\"darkgrid\")\nf, ax = plt.subplots(figsize=(25,18))\nax = sns.boxplot(data=articles_for_merge, x='price', y='product_group_name')\nax.set_xlabel('Price outliers', fontsize=22)\nax.set_ylabel('Index names', fontsize=22)\nax.xaxis.set_tick_params(labelsize=22)\nax.yaxis.set_tick_params(labelsize=22)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-07T23:54:38.810673Z","iopub.execute_input":"2022-03-07T23:54:38.810973Z","iopub.status.idle":"2022-03-07T23:54:58.541131Z","shell.execute_reply.started":"2022-03-07T23:54:38.81094Z","shell.execute_reply":"2022-03-07T23:54:58.540501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Then look at boxplot prices according to accessories product group and find the reasons of high prices inside group.</span>**\n\n**<span style=\"color:#023e8a;\"> The largest outliers can be found among bags, which is logical enough. In addition, scarves and other accessories have articles with prices highly contrasting to the rest of garments.</span>**","metadata":{}},{"cell_type":"code","source":"sns.set_style(\"darkgrid\")\nf, ax = plt.subplots(figsize=(25,18))\n_ = articles_for_merge[articles_for_merge['product_group_name'] == 'Accessories']\nax = sns.boxplot(data=_, x='price', y='product_type_name')\nax.set_xlabel('Price outliers', fontsize=22)\nax.set_ylabel('Index names', fontsize=22)\nax.xaxis.set_tick_params(labelsize=22)\nax.yaxis.set_tick_params(labelsize=22)\ndel _\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-14T14:17:48.12284Z","iopub.execute_input":"2022-02-14T14:17:48.123137Z","iopub.status.idle":"2022-02-14T14:17:55.668177Z","shell.execute_reply.started":"2022-02-14T14:17:48.123108Z","shell.execute_reply":"2022-02-14T14:17:55.66752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> The index with the highest mean price is Ladieswear. With the lowest - children. </span>**","metadata":{}},{"cell_type":"code","source":"articles_index = articles_for_merge[['index_name', 'price']].groupby('index_name').mean()\nsns.set_style(\"darkgrid\")\nf, ax = plt.subplots(figsize=(10,5))\nax = sns.barplot(x=articles_index.price, y=articles_index.index, color='orange', alpha=0.8)\nax.set_xlabel('Price by index')\nax.set_ylabel('Index')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T16:37:31.106646Z","iopub.execute_input":"2022-02-10T16:37:31.10702Z","iopub.status.idle":"2022-02-10T16:37:31.401246Z","shell.execute_reply.started":"2022-02-10T16:37:31.10698Z","shell.execute_reply":"2022-02-10T16:37:31.400176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Stationery has the lowest mean price, the highest - shoes. </span>**","metadata":{}},{"cell_type":"code","source":"articles_index = articles_for_merge[['product_group_name', 'price']].groupby('product_group_name').mean()\nsns.set_style(\"darkgrid\")\nf, ax = plt.subplots(figsize=(10,5))\nax = sns.barplot(x=articles_index.price, y=articles_index.index, color='orange', alpha=0.8)\nax.set_xlabel('Price by product group')\nax.set_ylabel('Product group')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T16:42:47.950361Z","iopub.execute_input":"2022-02-10T16:42:47.950688Z","iopub.status.idle":"2022-02-10T16:42:48.314404Z","shell.execute_reply.started":"2022-02-10T16:42:47.950657Z","shell.execute_reply":"2022-02-10T16:42:48.313272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Now check the mean price change in time for top 5 product groups by mean price: </span>**\n>`Shoes`  \n>`Garment Full body`  \n>`Bags`  \n>`Garment Lower body`  \n>`Underwear/nightwear`  ","metadata":{}},{"cell_type":"code","source":"articles_for_merge['t_dat'] = pd.to_datetime(articles_for_merge['t_dat'])","metadata":{"execution":{"iopub.status.busy":"2022-02-10T17:00:32.686081Z","iopub.execute_input":"2022-02-10T17:00:32.687011Z","iopub.status.idle":"2022-02-10T17:00:32.748831Z","shell.execute_reply.started":"2022-02-10T17:00:32.686955Z","shell.execute_reply":"2022-02-10T17:00:32.74765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"product_list = ['Shoes', 'Garment Full body', 'Bags', 'Garment Lower body', 'Underwear/nightwear']\ncolors = ['cadetblue', 'orange', 'mediumspringgreen', 'tomato', 'lightseagreen']\nk = 0\nf, ax = plt.subplots(3, 2, figsize=(20, 15))\nfor i in range(3):\n    for j in range(2):\n        try:\n            product = product_list[k]\n            articles_for_merge_product = articles_for_merge[articles_for_merge.product_group_name == product_list[k]]\n            series_mean = articles_for_merge_product[['t_dat', 'price']].groupby(pd.Grouper(key=\"t_dat\", freq='M')).mean().fillna(0)\n            series_std = articles_for_merge_product[['t_dat', 'price']].groupby(pd.Grouper(key=\"t_dat\", freq='M')).std().fillna(0)\n            ax[i, j].plot(series_mean, linewidth=4, color=colors[k])\n            ax[i, j].fill_between(series_mean.index, (series_mean.values-2*series_std.values).ravel(), \n                             (series_mean.values+2*series_std.values).ravel(), color=colors[k], alpha=.1)\n            ax[i, j].set_title(f'Mean {product_list[k]} price in time')\n            ax[i, j].set_xlabel('month')\n            ax[i, j].set_xlabel(f'{product_list[k]}')\n            k += 1\n        except IndexError:\n            ax[i, j].set_visible(False)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-10T17:27:39.134702Z","iopub.execute_input":"2022-02-10T17:27:39.135074Z","iopub.status.idle":"2022-02-10T17:27:52.264713Z","shell.execute_reply.started":"2022-02-10T17:27:39.135036Z","shell.execute_reply":"2022-02-10T17:27:52.263967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<span id=\"Images\" style=\"color:#023e8a;\">5. Images with description and price</span>**","metadata":{}},{"cell_type":"markdown","source":"[**<span style=\"color:#FEF1FE;background-color:#023e8a;border-radius: 5px;padding: 2px\">Go to Table of Content</span>**](#Content)","metadata":{}},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Let's check the last purchases by max price and by min price </span>**","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_price_ids = transactions[transactions.t_dat==transactions.t_dat.max()].sort_values('price', ascending=False).iloc[:5][['article_id', 'price']]\nmin_price_ids = transactions[transactions.t_dat==transactions.t_dat.min()].sort_values('price', ascending=True).iloc[:5][['article_id', 'price']]","metadata":{"execution":{"iopub.status.busy":"2022-02-08T16:04:30.384146Z","iopub.execute_input":"2022-02-08T16:04:30.384472Z","iopub.status.idle":"2022-02-08T16:04:49.925803Z","shell.execute_reply.started":"2022-02-08T16:04:30.384434Z","shell.execute_reply":"2022-02-08T16:04:49.924705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Photos with description and price (top 5 max) </span>**","metadata":{}},{"cell_type":"code","source":"f, ax = plt.subplots(1, 5, figsize=(20,10))\ni = 0\nfor _, data in max_price_ids.iterrows():\n    desc = articles[articles['article_id'] == data['article_id']]['detail_desc'].iloc[0]\n    desc_list = desc.split(' ')\n    for j, elem in enumerate(desc_list):\n        if j > 0 and j % 5 == 0:\n            desc_list[j] = desc_list[j] + '\\n'\n    desc = ' '.join(desc_list)\n    img = mpimg.imread(f'../input/h-and-m-personalized-fashion-recommendations/images/0{str(data.article_id)[:2]}/0{int(data.article_id)}.jpg')\n    ax[i].imshow(img)\n    ax[i].set_title(f'price: {data.price:.2f}')\n    ax[i].set_xticks([], [])\n    ax[i].set_yticks([], [])\n    ax[i].grid(False)\n    ax[i].set_xlabel(desc, fontsize=10)\n    i += 1\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-08T16:35:34.168952Z","iopub.execute_input":"2022-02-08T16:35:34.16932Z","iopub.status.idle":"2022-02-08T16:35:35.786412Z","shell.execute_reply.started":"2022-02-08T16:35:34.169275Z","shell.execute_reply":"2022-02-08T16:35:35.785777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**<span style=\"color:#023e8a;\"> Photos with description and price (top 5 min) </span>**","metadata":{}},{"cell_type":"code","source":"f, ax = plt.subplots(1, 5, figsize=(20,10))\ni = 0\nfor _, data in min_price_ids.iterrows():\n    desc = articles[articles['article_id'] == data['article_id']]['detail_desc'].iloc[0]\n    desc_list = desc.split(' ')\n    for j, elem in enumerate(desc_list):\n        if j > 0 and j % 4 == 0:\n            desc_list[j] = desc_list[j] + '\\n'\n    desc = ' '.join(desc_list)\n    img = mpimg.imread(f'../input/h-and-m-personalized-fashion-recommendations/images/0{str(data.article_id)[:2]}/0{int(data.article_id)}.jpg')\n    ax[i].imshow(img)\n    ax[i].set_title(f'price: {data.price:.4f}')\n    ax[i].set_xlabel(desc, fontsize=10)\n    ax[i].set_xticks([], [])\n    ax[i].set_yticks([], [])\n    ax[i].grid(False)\n    i += 1\nplt.axis('off')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-08T16:35:45.763751Z","iopub.execute_input":"2022-02-08T16:35:45.764234Z","iopub.status.idle":"2022-02-08T16:35:47.468754Z","shell.execute_reply.started":"2022-02-08T16:35:45.764187Z","shell.execute_reply":"2022-02-08T16:35:47.467851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **<center><span style=\"color:#FEF1FE;background-color:#023e8a;border-radius: 5px;padding: 5px\">Thanks for reading! If you find this notebook useful or interesting, please, support with an upvote :)</span></center>**","metadata":{}}]}