{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#Importing libraries\nimport numpy as np\nimport pandas as pd\n\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom PIL import Image\n\nimport gc","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2022-02-16T06:42:46.495572Z","iopub.execute_input":"2022-02-16T06:42:46.496122Z","iopub.status.idle":"2022-02-16T06:42:47.492047Z","shell.execute_reply.started":"2022-02-16T06:42:46.495992Z","shell.execute_reply":"2022-02-16T06:42:47.491265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Uploading data\narticles = 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_train = pd.read_csv('../input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\n\n# colour_group_name\ntransactions_train['t_dat'] = pd.to_datetime(transactions_train['t_dat'])\ntransactions_train['year'] = transactions_train['t_dat'].dt.year\ntransactions_train['mon'] = transactions_train['t_dat'].dt.month\ntransactions_train['day'] = transactions_train['t_dat'].dt.day","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2022-02-16T06:42:47.493633Z","iopub.execute_input":"2022-02-16T06:42:47.493851Z","iopub.status.idle":"2022-02-16T06:44:12.812191Z","shell.execute_reply.started":"2022-02-16T06:42:47.493825Z","shell.execute_reply":"2022-02-16T06:44:12.811423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Total Vs Transacting Customers","metadata":{}},{"cell_type":"code","source":"# Total Customers\ncustomers['age_bucket'] = pd.cut(customers['age'], bins = [15, 18, 25, 30, 40, 50, 100])\n\n# Transacting Customers in each bucket\na = transactions_train[['customer_id']]\nb = customers[['customer_id', 'age_bucket']]\nc = pd.merge(a, b, how = 'inner', on = 'customer_id')\nc = c.groupby(by = 'age_bucket').agg({'customer_id':'nunique'}).reset_index()\n\nplt.figure(figsize=(15, 6))\nplt.subplot(1, 2, 1)\nsns.countplot(data = customers, x='age_bucket', palette='crest')\nplt.title('Total Customers')\nplt.xlabel('Age Bucket')\nplt.ylabel('Count of Customers')\n\nplt.subplot(1, 2, 2)\nsns.barplot(data =c, x='age_bucket', y = 'customer_id', palette='crest')\nplt.title('Transacting Customers')\nplt.xlabel('Age Bucket')\nplt.ylabel('Count of Transacting Customers')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-16T06:44:12.813511Z","iopub.execute_input":"2022-02-16T06:44:12.813718Z","iopub.status.idle":"2022-02-16T06:44:40.548938Z","shell.execute_reply.started":"2022-02-16T06:44:12.813693Z","shell.execute_reply":"2022-02-16T06:44:40.548291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h4 style=\"color:black;\">Count of Total Customers and Transacting Customers is almost same.</h4>","metadata":{}},{"cell_type":"markdown","source":"# Article Group - Stock vs Yearly Sales Percentage","metadata":{}},{"cell_type":"code","source":"transactions_train['t_dat'] = pd.to_datetime(transactions_train['t_dat'], infer_datetime_format=True)\ntransactions_train['year'] = transactions_train['t_dat'].dt.year\ntransactions_train['mon'] = transactions_train['t_dat'].dt.month\ntransactions_train['day'] = transactions_train['t_dat'].dt.day\na = transactions_train[['article_id', 'year']]\nb = articles[['article_id', 'index_group_name']]\nc = pd.merge(a, b, how = 'inner')\nd = c.pivot_table(index='year', columns='index_group_name', values = 'article_id', aggfunc='count')\nd['total'] = d.sum(axis=1)\nd.iloc[:, 0] = np.round((d.iloc[:, 0]/d['total'])*100, 2)\nd.iloc[:, 1] = np.round((d.iloc[:, 1]/d['total'])*100, 2)\nd.iloc[:, 2] = np.round((d.iloc[:, 2]/d['total'])*100, 2)\nd.iloc[:, 3] = np.round((d.iloc[:, 3]/d['total'])*100, 2)\nd.iloc[:, 4] = np.round((d.iloc[:, 4]/d['total'])*100, 2)\nd.drop(['total'], axis = 1, inplace=True)\n\ne = pd.DataFrame(articles[['index_group_name']].value_counts())\ne.columns = ['cnt']\ne['pct'] = np.round((e['cnt']/e['cnt'].sum())*100, 2)\n\nplt.figure(figsize=(25, 6))\nplt.subplot(1, 3, 1)\nsns.heatmap(e[['cnt']], cmap='Blues', annot=True, fmt='d')\nplt.xlabel('Stock Count')\nplt.ylabel('Article group')\n\nplt.subplot(1, 3, 2)\nsns.heatmap(e[['pct']], cmap='Blues', annot=True, fmt='g')\nplt.xlabel('Stock in Precentage')\nplt.ylabel('Article group')\n\nplt.subplot(1, 3, 3)\nsns.heatmap(d, annot=True, cmap='Blues', fmt='g')\nplt.title(\"Year wise Article Group Sales Percentage\")\nplt.xlabel('Article group')\nplt.ylabel('Year')\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-16T06:44:40.550758Z","iopub.execute_input":"2022-02-16T06:44:40.551162Z","iopub.status.idle":"2022-02-16T06:45:00.023627Z","shell.execute_reply.started":"2022-02-16T06:44:40.551116Z","shell.execute_reply":"2022-02-16T06:45:00.022731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h4 style=\"color:black;\">LadiesWear are top in both stock and Sales Percentage.</h4>\n<h4 style=\"color:black;\">In Stock Baby/Children group is almost equal to LadiesWear, however for Baby/Children group Sales is low as compared to items in stock.</h4>","metadata":{}},{"cell_type":"markdown","source":"# Sales Channel wise Yearly Sales","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nsns.countplot(data = transactions_train, x='year', palette='Blues', hue='sales_channel_id')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-16T06:45:00.025079Z","iopub.execute_input":"2022-02-16T06:45:00.025432Z","iopub.status.idle":"2022-02-16T06:45:05.964759Z","shell.execute_reply.started":"2022-02-16T06:45:00.025387Z","shell.execute_reply":"2022-02-16T06:45:05.963898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h4 style=\"color:black;\">Distribution is almost same each year for Sales Channel Id 1 and 2.</h4>\n","metadata":{"execution":{"iopub.status.busy":"2022-02-15T19:01:20.751803Z","iopub.execute_input":"2022-02-15T19:01:20.752146Z","iopub.status.idle":"2022-02-15T19:01:20.759853Z","shell.execute_reply.started":"2022-02-15T19:01:20.752113Z","shell.execute_reply":"2022-02-15T19:01:20.758514Z"}}},{"cell_type":"markdown","source":"# Top Selling Items and Avg Sales Price - Overall","metadata":{"execution":{"iopub.status.busy":"2022-02-12T05:52:28.139537Z","iopub.execute_input":"2022-02-12T05:52:28.139955Z","iopub.status.idle":"2022-02-12T05:52:28.145159Z","shell.execute_reply.started":"2022-02-12T05:52:28.139914Z","shell.execute_reply":"2022-02-12T05:52:28.144114Z"}}},{"cell_type":"code","source":"a = transactions_train.groupby(by = ['article_id']).agg({'article_id':'count', 'price':'mean'})\na.columns = ['Count', 'Avg Sales Price']\na = a.reset_index()\na = a.sort_values(by = ['Count'], ascending=[False])\na = a.head(12)\n\nplt.figure(figsize=(28, 10))\ni = 1\nfor j,x in enumerate(a['article_id'].to_list()):\n    try:\n        image = Image.open(\"../input/h-and-m-personalized-fashion-recommendations/images/0\"+str(x)[:2]+\"/0\"+str(x)+\".jpg\")\n        plt.subplot(2, 5, i)\n        plt.imshow(image)\n        #plt.axis('off')\n        plt.title(x)\n        plt.xlabel(\"Sell Count = \"+str(a['Count'].to_list()[j]))\n        plt.ylabel(\"Average Sell Price= \"+str(np.round(a['Avg Sales Price'], 3).to_list()[j]))\n        i+=1\n    except:\n        pass\n    ","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-16T06:45:05.966298Z","iopub.execute_input":"2022-02-16T06:45:05.966566Z","iopub.status.idle":"2022-02-16T06:45:12.548322Z","shell.execute_reply.started":"2022-02-16T06:45:05.966534Z","shell.execute_reply":"2022-02-16T06:45:12.547367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Note: Products are ignored for which image is not avaialble","metadata":{}},{"cell_type":"markdown","source":"# Checking Monthly Seasonality","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20, 5))\na= transactions_train.groupby(by = ['year', 'mon']).agg({'customer_id':'count'}).reset_index()\nax = sns.lineplot(data = a, x = a['mon'], y = a['customer_id'], hue = 'year', marker=\"o\", palette='gist_rainbow_r', linewidth = 4)\nax.set(xticks=a['mon'].values)\nplt.xlabel(\"Month\")\nplt.ylabel(\"Transaction Count\")\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-16T06:45:12.549691Z","iopub.execute_input":"2022-02-16T06:45:12.550702Z","iopub.status.idle":"2022-02-16T06:45:18.166519Z","shell.execute_reply.started":"2022-02-16T06:45:12.550630Z","shell.execute_reply":"2022-02-16T06:45:18.165847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h4 style=\"color:black;\">For 2018 data is available from Sep.</h4>\n<h4 style=\"color:black;\">For 2020 data is available from Jan to Sep.</h4>\n<h4 style=\"color:black;\">We can see growth in transaction from  Feb to June.</h4>","metadata":{"execution":{"iopub.status.busy":"2022-02-14T19:54:57.903989Z","iopub.execute_input":"2022-02-14T19:54:57.904855Z","iopub.status.idle":"2022-02-14T19:54:57.909334Z","shell.execute_reply.started":"2022-02-14T19:54:57.904798Z","shell.execute_reply":"2022-02-14T19:54:57.908348Z"}}},{"cell_type":"markdown","source":"# Top Selling Color Analysis","metadata":{}},{"cell_type":"code","source":"a = transactions_train[['article_id', 't_dat', 'year', 'mon']]\nb = articles[['article_id', 'colour_group_name']]\nc = pd.merge(a, b, how = 'inner', on = ['article_id']).reset_index()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-16T06:45:18.167458Z","iopub.execute_input":"2022-02-16T06:45:18.168225Z","iopub.status.idle":"2022-02-16T06:45:28.721187Z","shell.execute_reply.started":"2022-02-16T06:45:18.168150Z","shell.execute_reply":"2022-02-16T06:45:28.720267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Color wise Count of items sold","metadata":{}},{"cell_type":"code","source":"c['colour_group_name'].value_counts()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-16T06:45:28.722405Z","iopub.execute_input":"2022-02-16T06:45:28.722635Z","iopub.status.idle":"2022-02-16T06:45:33.300808Z","shell.execute_reply.started":"2022-02-16T06:45:28.722605Z","shell.execute_reply":"2022-02-16T06:45:33.299821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Let check if there is a correlation between the colors","metadata":{}},{"cell_type":"code","source":"color_piv = pd.pivot_table(c , index='t_dat', columns=['colour_group_name'], values = 'article_id', aggfunc='count')\nplt.figure(figsize=(40, 40))\nsns.heatmap(color_piv.corr(), cmap = 'crest', annot = True)\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-16T06:45:33.303970Z","iopub.execute_input":"2022-02-16T06:45:33.304822Z","iopub.status.idle":"2022-02-16T06:45:49.560440Z","shell.execute_reply.started":"2022-02-16T06:45:33.304766Z","shell.execute_reply":"2022-02-16T06:45:49.559314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### White has high correlation Blue, Light Blue, Dark Orange seems like people prefer these color combinations (just my intuition). We can see some other good correlations like Red and Blue.","metadata":{}}]}