{"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":"# <u>**Getting started with H&M EDA**</u>","metadata":{}},{"cell_type":"markdown","source":"### In this notebook we have analysed each of the transactions, customers and articles datafrfame separately:\n* Performed basic operations to understand the data (column types, missing data and so on)\n* Visualized the trend of sales over the period of time\n* Found out which days are most favourable for shoppers\n* Comparison of online vs offline sales\n* Extract list of top articles and customers\n* Feature Engineering on few of the customer features\n* Top Products of H&M","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nimport plotly.express as px\nimport dask.dataframe as dd\n\nwarnings.simplefilter('ignore')\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-02-23T05:57:57.157394Z","iopub.execute_input":"2022-02-23T05:57:57.157700Z","iopub.status.idle":"2022-02-23T05:57:57.164822Z","shell.execute_reply.started":"2022-02-23T05:57:57.157672Z","shell.execute_reply":"2022-02-23T05:57:57.164158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_path = '../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv'\ncustomers_path = '../input/h-and-m-personalized-fashion-recommendations/customers.csv'\narticles_path = '../input/h-and-m-personalized-fashion-recommendations/articles.csv'\n\n# converting the column datatypes to save memory\nc = 'category'\ntran_dict = {'customer_id': c, 't_dat': c}\nart_dict = {'prod_name': c, 'prod_type_name': c, 'product_group_name': c, 'graphical_appearance_name': c, 'colour_group_name': c, 'perceived_colour_value_name': c, \n            'department_name': c, 'index_code': c, 'index_name': c, 'index_group_name': c, 'section_name': c, 'garment_group_name': c, 'detail_desc': c}\n\ntransactions = pd.read_csv(transactions_path, dtype=tran_dict)\narticles = pd.read_csv(articles_path, dtype=art_dict)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T05:58:00.031529Z","iopub.execute_input":"2022-02-23T05:58:00.031953Z","iopub.status.idle":"2022-02-23T05:59:41.714670Z","shell.execute_reply.started":"2022-02-23T05:58:00.031923Z","shell.execute_reply":"2022-02-23T05:59:41.713671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def drop(df, *feature):\n    '''\n    Function to drop features from a dataframe\n    Takes the dataframe and single/multiple features\n    '''\n    for feat in feature:\n        df.drop(feat, axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T05:59:49.541899Z","iopub.execute_input":"2022-02-23T05:59:49.542592Z","iopub.status.idle":"2022-02-23T05:59:49.547507Z","shell.execute_reply.started":"2022-02-23T05:59:49.542536Z","shell.execute_reply":"2022-02-23T05:59:49.546793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analyzing the transaction data","metadata":{}},{"cell_type":"code","source":"transactions.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T05:59:50.980567Z","iopub.execute_input":"2022-02-23T05:59:50.980867Z","iopub.status.idle":"2022-02-23T05:59:50.999807Z","shell.execute_reply.started":"2022-02-23T05:59:50.980837Z","shell.execute_reply":"2022-02-23T05:59:50.998921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-23T05:59:51.253313Z","iopub.execute_input":"2022-02-23T05:59:51.253641Z","iopub.status.idle":"2022-02-23T05:59:52.167864Z","shell.execute_reply.started":"2022-02-23T05:59:51.253607Z","shell.execute_reply":"2022-02-23T05:59:52.166960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions.isnull().sum()        # no missing values in transaction","metadata":{"execution":{"iopub.status.busy":"2022-02-23T05:59:52.170013Z","iopub.execute_input":"2022-02-23T05:59:52.170347Z","iopub.status.idle":"2022-02-23T05:59:52.425605Z","shell.execute_reply.started":"2022-02-23T05:59:52.170302Z","shell.execute_reply":"2022-02-23T05:59:52.424836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Extracting date info from t_dat feature**","metadata":{}},{"cell_type":"code","source":"transactions['year'] = pd.DatetimeIndex(transactions['t_dat']).year\ntransactions['month'] = pd.DatetimeIndex(transactions['t_dat']).month\ntransactions['dayofweek'] = pd.DatetimeIndex(transactions['t_dat']).dayofweek","metadata":{"execution":{"iopub.status.busy":"2022-02-23T05:59:52.426700Z","iopub.execute_input":"2022-02-23T05:59:52.426939Z","iopub.status.idle":"2022-02-23T06:00:16.983072Z","shell.execute_reply.started":"2022-02-23T05:59:52.426905Z","shell.execute_reply":"2022-02-23T06:00:16.982097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"month_wise_sales = transactions.groupby(['year', 'month']).size().reset_index().rename(columns={0:'Count'})\nmonth_wise_sales['month'] = month_wise_sales['month'].map({1:'Jan',2:'Feb',3:'Mar',4:'Apr',5:'May',6:'Jun',7:'Jul',8:'Aug',9:'Sep',10:'Oct',11:'Nov',12:'Dec'})\nmatplotlib.rcParams['figure.figsize'] = (12,6)\nsns.barplot(x='month', y='Count', data=month_wise_sales, hue='year', palette=['blue', 'red', 'green'])\nplt.title('Month wise Number of Items Sold')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:16.985027Z","iopub.execute_input":"2022-02-23T06:00:16.985449Z","iopub.status.idle":"2022-02-23T06:00:19.468107Z","shell.execute_reply.started":"2022-02-23T06:00:16.985400Z","shell.execute_reply":"2022-02-23T06:00:19.467161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"day_wise_sales = transactions.groupby(['dayofweek']).size().reset_index().rename(columns={0:'Count'})\nday_wise_sales['dayofweek'] = day_wise_sales['dayofweek'].map({0:'Monday', 1:'Tuesday', 2:'Wednesday', 3:'Thursday', 4:'Friday', 5:'Saturday', 6:'Sunday'})\nmatplotlib.rcParams['figure.figsize'] = (12,6)\nsns.barplot(x='dayofweek', y='Count', data=day_wise_sales)\nplt.title('WeekDay wise Number of Items Sold')\nplt.xticks(rotation=45)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:21.548535Z","iopub.execute_input":"2022-02-23T06:00:21.549041Z","iopub.status.idle":"2022-02-23T06:00:22.432536Z","shell.execute_reply.started":"2022-02-23T06:00:21.548981Z","shell.execute_reply":"2022-02-23T06:00:22.431569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales = transactions[['year', 'month', 'price']].groupby(['year', 'month']).sum().reset_index().rename(columns={0:'Sales'})\nsales['price'] = sales['price'].astype(int)\nsns.lineplot(data=sales, x=\"month\", y=\"price\", hue='year', palette=['red', 'green', 'blue'])","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:22.434010Z","iopub.execute_input":"2022-02-23T06:00:22.434274Z","iopub.status.idle":"2022-02-23T06:00:28.263007Z","shell.execute_reply.started":"2022-02-23T06:00:22.434239Z","shell.execute_reply":"2022-02-23T06:00:28.262118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Some vital conclusions can be drawn from the above line chart:\n* Total sales was quite less in the first month (September 2018)\n* Sales doubled in the very next month and remained consistent in the range of 25000 to 40000\n* Breakthrough month - Jun 2019\n* In 2020 the sales improved gradually till June after which it started declining","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(4,6))\nsns.countplot(transactions['sales_channel_id'], color = 'crimson')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:28.264339Z","iopub.execute_input":"2022-02-23T06:00:28.264795Z","iopub.status.idle":"2022-02-23T06:00:31.278262Z","shell.execute_reply.started":"2022-02-23T06:00:28.264751Z","shell.execute_reply":"2022-02-23T06:00:31.277281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_subset = transactions[['year', 'month', 'price']].groupby(['year', 'month']).sum().reset_index()\nsns.catplot(x = 'year', y = 'price', data=df_subset, hue = 'month')","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:31.279630Z","iopub.execute_input":"2022-02-23T06:00:31.280240Z","iopub.status.idle":"2022-02-23T06:00:34.870705Z","shell.execute_reply.started":"2022-02-23T06:00:31.280208Z","shell.execute_reply":"2022-02-23T06:00:34.868444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Top Articles and Customers**","metadata":{}},{"cell_type":"code","source":"top_ten_articles = transactions['article_id'].value_counts().index[:10]               # 10 most sold articles\ntop_twenty_customers = transactions['customer_id'].value_counts().index[:20]             # 20 customers who purchased max number of times","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:34.872511Z","iopub.execute_input":"2022-02-23T06:00:34.872839Z","iopub.status.idle":"2022-02-23T06:00:37.462119Z","shell.execute_reply.started":"2022-02-23T06:00:34.872794Z","shell.execute_reply":"2022-02-23T06:00:37.461328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop(transactions, ['t_dat'])                             # extracted useful date info, t_dat no longer required","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:37.463652Z","iopub.execute_input":"2022-02-23T06:00:37.464358Z","iopub.status.idle":"2022-02-23T06:00:38.063080Z","shell.execute_reply.started":"2022-02-23T06:00:37.464321Z","shell.execute_reply":"2022-02-23T06:00:38.062054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Summary of the findings: \n1. We don't have any missing values in transaction dataframe\n2. Transaction details are available from September 2018 to September 2020\n3. Barring September for all the other months, 2018 saw more number of purchases than 2019 which is in turn did better than 2020 (no. of purchases is declining yearly)\n4. Customers make most purchases on Saturdays and least on Sundays though the difference is not too high\n5. We found the min, max and avg price values of the transactions\n6. We extracted the top articles sold over the period and also the top 10 customers who bought the most number of times.\n7. 2/3rd of the sales happened via channel ID 2 and the rest via channel ID 1\n","metadata":{}},{"cell_type":"markdown","source":"# Analyzing the customer data","metadata":{}},{"cell_type":"code","source":"customers = pd.read_csv(customers_path)\ncustomers.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:38.064287Z","iopub.execute_input":"2022-02-23T06:00:38.064522Z","iopub.status.idle":"2022-02-23T06:00:43.039953Z","shell.execute_reply.started":"2022-02-23T06:00:38.064492Z","shell.execute_reply":"2022-02-23T06:00:43.039103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:43.042278Z","iopub.execute_input":"2022-02-23T06:00:43.043215Z","iopub.status.idle":"2022-02-23T06:00:43.416861Z","shell.execute_reply.started":"2022-02-23T06:00:43.043166Z","shell.execute_reply":"2022-02-23T06:00:43.415955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def value_counts(feature_list):\n    '''\n    To print the value counts of the categories within a feature. Takes a list of features as an argument.\n    '''\n    for i in feature_list:\n        print(i.upper())\n        print(customers[i].value_counts())\n        print('Missing values: ', customers[i].isnull().sum(), '\\n')","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:43.420051Z","iopub.execute_input":"2022-02-23T06:00:43.420565Z","iopub.status.idle":"2022-02-23T06:00:43.426445Z","shell.execute_reply.started":"2022-02-23T06:00:43.420516Z","shell.execute_reply":"2022-02-23T06:00:43.425260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"check_features = ['FN', 'Active', 'club_member_status', 'fashion_news_frequency']\nvalue_counts(check_features)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:43.427738Z","iopub.execute_input":"2022-02-23T06:00:43.427996Z","iopub.status.idle":"2022-02-23T06:00:43.766424Z","shell.execute_reply.started":"2022-02-23T06:00:43.427959Z","shell.execute_reply":"2022-02-23T06:00:43.765266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Handling the missing values:\n* **FN**: Filling the missing values with 0s as the non-null values are 1s\n* **Active**: Same as FN\n* **club_member_status**: Filling with the mode (ACTIVE)\n* **fashion_news_frequency**: Replacing 2 `None` values with `NONE` and filling the missing values with the mode NONE for the time being","metadata":{}},{"cell_type":"code","source":"customers['fashion_news_frequency'].replace(to_replace='None', value='NONE', inplace=True)\nvalues = {\"FN\": 0, \"Active\": 0, \"club_member_status\": 'ACTIVE', \"fashion_news_frequency\": 'NONE'}\ncustomers.fillna(value=values, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:43.767935Z","iopub.execute_input":"2022-02-23T06:00:43.768250Z","iopub.status.idle":"2022-02-23T06:00:43.953759Z","shell.execute_reply.started":"2022-02-23T06:00:43.768215Z","shell.execute_reply":"2022-02-23T06:00:43.952724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"value_counts(check_features)           # missing values handled","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:43.955137Z","iopub.execute_input":"2022-02-23T06:00:43.956035Z","iopub.status.idle":"2022-02-23T06:00:44.298423Z","shell.execute_reply.started":"2022-02-23T06:00:43.955988Z","shell.execute_reply":"2022-02-23T06:00:44.297457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Let's see the age feature now**","metadata":{}},{"cell_type":"code","source":"customers['age'].isnull().sum()           # total number of missing values in age feature","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:44.299698Z","iopub.execute_input":"2022-02-23T06:00:44.299931Z","iopub.status.idle":"2022-02-23T06:00:44.309103Z","shell.execute_reply.started":"2022-02-23T06:00:44.299902Z","shell.execute_reply":"2022-02-23T06:00:44.308175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Minimum Age: ', customers['age'].value_counts().index.min())\nprint('Maximum Age: ', customers['age'].value_counts().index.max())","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:44.310299Z","iopub.execute_input":"2022-02-23T06:00:44.310546Z","iopub.status.idle":"2022-02-23T06:00:44.351818Z","shell.execute_reply.started":"2022-02-23T06:00:44.310508Z","shell.execute_reply":"2022-02-23T06:00:44.350869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers['age'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:44.353151Z","iopub.execute_input":"2022-02-23T06:00:44.353416Z","iopub.status.idle":"2022-02-23T06:00:44.416598Z","shell.execute_reply.started":"2022-02-23T06:00:44.353385Z","shell.execute_reply":"2022-02-23T06:00:44.415746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.boxplot(customers['age'], color='purple')","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:44.417863Z","iopub.execute_input":"2022-02-23T06:00:44.418132Z","iopub.status.idle":"2022-02-23T06:00:44.643211Z","shell.execute_reply.started":"2022-02-23T06:00:44.418103Z","shell.execute_reply":"2022-02-23T06:00:44.642118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Missing value imputation with median as we have outliers\ncustomers['age'].fillna(customers['age'].median(), inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:44.644404Z","iopub.execute_input":"2022-02-23T06:00:44.644617Z","iopub.status.idle":"2022-02-23T06:00:44.675006Z","shell.execute_reply.started":"2022-02-23T06:00:44.644591Z","shell.execute_reply":"2022-02-23T06:00:44.673975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customers[customers['age']>65].shape","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:44.676307Z","iopub.execute_input":"2022-02-23T06:00:44.676549Z","iopub.status.idle":"2022-02-23T06:00:44.703120Z","shell.execute_reply.started":"2022-02-23T06:00:44.676520Z","shell.execute_reply":"2022-02-23T06:00:44.702483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**We have customers ranging from 16 years to 99 years of age with the average age being 36 years**\n\nBinning - grouping the age into categories:\n1. Below 26\n2. 26-35\n3. 36-45\n4. 46-55\n5. 56-65\n6. Above 66\n\nWe are considering buckets of 10, later we can try different approaches of binning or perhaps frequency encoding.","metadata":{}},{"cell_type":"code","source":"age_bins = [15,26,36,46,56,66,100]\ncustomers['age'] = pd.cut(customers['age'], bins=age_bins, labels=['Below 26','26-35','36-45','46-55', '56-65', 'Above 65'])","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:44.704411Z","iopub.execute_input":"2022-02-23T06:00:44.704641Z","iopub.status.idle":"2022-02-23T06:00:44.759639Z","shell.execute_reply.started":"2022-02-23T06:00:44.704615Z","shell.execute_reply":"2022-02-23T06:00:44.758661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,6))\ncustomers.groupby('age').size().plot(kind='pie', autopct='%1.2d%%')\nplt.ylabel('Age Distribution', size=20)\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:44.760670Z","iopub.execute_input":"2022-02-23T06:00:44.761478Z","iopub.status.idle":"2022-02-23T06:00:44.989382Z","shell.execute_reply.started":"2022-02-23T06:00:44.761412Z","shell.execute_reply":"2022-02-23T06:00:44.988427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"drop(customers, ['postal_code'])             ","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:44.990925Z","iopub.execute_input":"2022-02-23T06:00:44.992088Z","iopub.status.idle":"2022-02-23T06:00:45.063897Z","shell.execute_reply.started":"2022-02-23T06:00:44.992027Z","shell.execute_reply":"2022-02-23T06:00:45.062708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Insights of customers dataset\n* Outliers detected in age feature\n* Folks between age range of 16-35 dominates customers data\n* FN and Active features had 1s or empty values ","metadata":{}},{"cell_type":"markdown","source":"# Analyzing the articles data","metadata":{}},{"cell_type":"code","source":"articles.info()                      # only description has few missing values","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:45.065163Z","iopub.execute_input":"2022-02-23T06:00:45.065460Z","iopub.status.idle":"2022-02-23T06:00:45.153853Z","shell.execute_reply.started":"2022-02-23T06:00:45.065426Z","shell.execute_reply":"2022-02-23T06:00:45.152956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles[['product_code','prod_name']].value_counts().head(5)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:45.155123Z","iopub.execute_input":"2022-02-23T06:00:45.155393Z","iopub.status.idle":"2022-02-23T06:00:45.185500Z","shell.execute_reply.started":"2022-02-23T06:00:45.155356Z","shell.execute_reply":"2022-02-23T06:00:45.184662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**As we see that features are in pairs of unique codes or IDs and their corressponding names, we can drop either of the two to avoid data redundancy**","metadata":{}},{"cell_type":"code","source":"feature_list = [i for i in articles.columns[1:] if str(articles[i].dtypes)[:3] == 'int']\ndrop(articles, feature_list)\narticles.head(3)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:45.188309Z","iopub.execute_input":"2022-02-23T06:00:45.188654Z","iopub.status.idle":"2022-02-23T06:00:45.213053Z","shell.execute_reply.started":"2022-02-23T06:00:45.188622Z","shell.execute_reply":"2022-02-23T06:00:45.212132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"px.sunburst(articles, path=['perceived_colour_value_name', 'perceived_colour_master_name'], title='Color Categories')","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:45.214331Z","iopub.execute_input":"2022-02-23T06:00:45.215022Z","iopub.status.idle":"2022-02-23T06:00:47.410986Z","shell.execute_reply.started":"2022-02-23T06:00:45.214987Z","shell.execute_reply":"2022-02-23T06:00:47.410114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Perceived_colour_master_name doesn't add much information**","metadata":{}},{"cell_type":"code","source":"drop(articles, 'perceived_colour_master_name')","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:47.412813Z","iopub.execute_input":"2022-02-23T06:00:47.413071Z","iopub.status.idle":"2022-02-23T06:00:47.420749Z","shell.execute_reply.started":"2022-02-23T06:00:47.413040Z","shell.execute_reply":"2022-02-23T06:00:47.419506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Dominating Products in H&M Stock**","metadata":{}},{"cell_type":"code","source":"def top_fives(feature, col):\n    products = articles.groupby(feature).size().reset_index().rename(columns={0: 'Total'}).sort_values('Total', ascending=False).head()\n    fig = px.pie(products, values='Total', names=feature, color_discrete_sequence=col, title='Top 5 {}'.format(feature))\n    fig.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:47.741221Z","iopub.execute_input":"2022-02-23T06:00:47.741860Z","iopub.status.idle":"2022-02-23T06:00:47.747388Z","shell.execute_reply.started":"2022-02-23T06:00:47.741820Z","shell.execute_reply":"2022-02-23T06:00:47.746612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"to_get_tops = {'product_group_name': px.colors.sequential.RdBu, \n               'prod_name': px.colors.sequential.RdBu_r, \n               'product_type_name': px.colors.sequential.BuGn_r, \n               'graphical_appearance_name': px.colors.sequential.OrRd_r}\n\nfor i, j in to_get_tops.items():\n    top_fives(i, j)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:48.218925Z","iopub.execute_input":"2022-02-23T06:00:48.219687Z","iopub.status.idle":"2022-02-23T06:00:48.418467Z","shell.execute_reply.started":"2022-02-23T06:00:48.219646Z","shell.execute_reply":"2022-02-23T06:00:48.417523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Detailed Description Feature**","metadata":{}},{"cell_type":"code","source":"from wordcloud import WordCloud, STOPWORDS\nimport gensim\nfrom gensim.utils import simple_preprocess\nfrom gensim.parsing.preprocessing import STOPWORDS\nfrom nltk.corpus import stopwords\n\nstop_words = stopwords.words('english')\nstop_words.extend(['the', 'with', 'at', 'zip'])                        # adding in the list","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:49.103582Z","iopub.execute_input":"2022-02-23T06:00:49.103852Z","iopub.status.idle":"2022-02-23T06:00:50.268806Z","shell.execute_reply.started":"2022-02-23T06:00:49.103824Z","shell.execute_reply":"2022-02-23T06:00:50.267794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles['detail_desc'] = articles['detail_desc'].str.replace('[#,@,&,.]','')        # removing special charaters from description\n\n# Remove stop words and remove words with 2 or less characters\ndef preprocess(text):\n    ''' keeping only the words which are not in stop_words list '''\n    result = []\n    for token in gensim.utils.simple_preprocess(text) :\n        if token not in gensim.parsing.preprocessing.STOPWORDS and len(token) > 2 and token not in stop_words:\n            result.append(token)\n            \n    return ' '.join(result)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:50.270443Z","iopub.execute_input":"2022-02-23T06:00:50.270694Z","iopub.status.idle":"2022-02-23T06:00:50.405779Z","shell.execute_reply.started":"2022-02-23T06:00:50.270663Z","shell.execute_reply":"2022-02-23T06:00:50.405057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles['detail_desc'].fillna('None', inplace=True)\narticles['clean_desc'] = articles['detail_desc'].apply(preprocess)\ndrop(articles, 'detail_desc')","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:50.706101Z","iopub.execute_input":"2022-02-23T06:00:50.706559Z","iopub.status.idle":"2022-02-23T06:00:57.368724Z","shell.execute_reply.started":"2022-02-23T06:00:50.706526Z","shell.execute_reply":"2022-02-23T06:00:57.367754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def word_show(group):\n    ''' to display the word cloud on the basis of index_group_name '''\n    plt.figure(figsize=(15,10))\n    wc = WordCloud(max_words=2000, width=1600, height=800, stopwords=stop_words).generate(str(articles[articles['index_group_name']==group].clean_desc))\n    plt.title('Dominating words in description of {} section'.format(group), fontsize=25)\n    plt.imshow(wc)","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:57.370824Z","iopub.execute_input":"2022-02-23T06:00:57.371253Z","iopub.status.idle":"2022-02-23T06:00:57.377214Z","shell.execute_reply.started":"2022-02-23T06:00:57.371205Z","shell.execute_reply":"2022-02-23T06:00:57.376273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"word_show('Ladieswear')","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:57.378507Z","iopub.execute_input":"2022-02-23T06:00:57.378936Z","iopub.status.idle":"2022-02-23T06:00:59.111149Z","shell.execute_reply.started":"2022-02-23T06:00:57.378891Z","shell.execute_reply":"2022-02-23T06:00:59.110371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"word_show('Baby/Children')","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:00:59.112756Z","iopub.execute_input":"2022-02-23T06:00:59.113208Z","iopub.status.idle":"2022-02-23T06:01:00.762458Z","shell.execute_reply.started":"2022-02-23T06:00:59.113157Z","shell.execute_reply":"2022-02-23T06:01:00.761551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Top 10**","metadata":{}},{"cell_type":"code","source":"top_articles = articles.loc[articles['article_id'].isin(list(top_ten_articles))]\npx.sunburst(top_articles, path=['index_group_name', 'index_name', 'section_name'], title='Top Selling Products ')","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:01:00.764044Z","iopub.execute_input":"2022-02-23T06:01:00.764512Z","iopub.status.idle":"2022-02-23T06:01:00.926846Z","shell.execute_reply.started":"2022-02-23T06:01:00.764469Z","shell.execute_reply":"2022-02-23T06:01:00.925790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top_customers = customers.loc[customers['customer_id'].isin(list(top_twenty_customers))]\ntop_customers['fashion_news_frequency'].replace('NONE', 'Irregular', inplace=True)\npx.sunburst(top_customers, path=['age', 'fashion_news_frequency', 'club_member_status'], title=\"Top Customers' behaviour\")","metadata":{"execution":{"iopub.status.busy":"2022-02-23T06:01:07.132356Z","iopub.execute_input":"2022-02-23T06:01:07.132654Z","iopub.status.idle":"2022-02-23T06:01:07.444561Z","shell.execute_reply.started":"2022-02-23T06:01:07.132625Z","shell.execute_reply":"2022-02-23T06:01:07.443550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **Summary of the insights:**\n* Only detailed_desc has few missing values\n* Every informative feature comes in pair of unique ID/code and its corressponding name\n* Ladieswear is the most popular section followed by Kids/Baby section whereas Sports is a very small section in H&M\n* There are 3 color features, the superset perceived_colour_value_name, doen't add any extra information hence, it has been dropped\n* Top 5 product features have been identified with the below results:\n    1. Upper body garments product categories is available in abundance followed by lower body garments\n    2. Dragonfly Dress is the top SKU\n    3. Trousers and Dress product types dominates the articles dataset\n    4. H&M tend to prefer solid colors over patterns for its products\n* Extracted the commonly occurring description words in the most popular categories - Ladies and Children\n* Top 20 customers are all active club members and most of them follow fashion news regularly\n* Top purchasing customers are in the age group of 25-55\n* Divided and Ladies are top selling product categories","metadata":{}},{"cell_type":"markdown","source":"## **Next steps**\n* Merge the datasets\n* Ensure missing values are handled\n* Categorical encoding\n* Scaling features\n* Feature Correlation\n* Feature Selection\n* Model building\n* Performance Evaluation","metadata":{}},{"cell_type":"markdown","source":"**Suggestions/feedbacks are welcomed!**","metadata":{}}]}