{"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":"import numpy as np \nimport pandas as pd \nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.preprocessing import LabelEncoder","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-14T13:09:45.639560Z","iopub.execute_input":"2022-05-14T13:09:45.640340Z","iopub.status.idle":"2022-05-14T13:09:47.100225Z","shell.execute_reply.started":"2022-05-14T13:09:45.640290Z","shell.execute_reply":"2022-05-14T13:09:47.099138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transactions_train_file = '/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv'\narticles_file = '/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv'\ncustomers_file = '/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv'","metadata":{"execution":{"iopub.status.busy":"2022-05-14T13:09:47.183178Z","iopub.execute_input":"2022-05-14T13:09:47.183728Z","iopub.status.idle":"2022-05-14T13:09:47.189545Z","shell.execute_reply.started":"2022-05-14T13:09:47.183679Z","shell.execute_reply":"2022-05-14T13:09:47.188431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles_data = pd.read_csv(articles_file)\ncustomers_data = pd.read_csv(customers_file)\ntransactions_data = pd.read_csv(transactions_train_file)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T09:38:38.486855Z","iopub.execute_input":"2022-05-14T09:38:38.487143Z","iopub.status.idle":"2022-05-14T09:39:53.434109Z","shell.execute_reply.started":"2022-05-14T09:38:38.487115Z","shell.execute_reply":"2022-05-14T09:39:53.433062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"markdown","source":"## 1. Articles data:","metadata":{}},{"cell_type":"code","source":"#articles_data = pd.read_csv(articles_file)\narticles_data.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Most of the products are ladiesware and sports wear has the least portion.**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (20,10))\nsns.histplot(data=articles_data,y='index_name',kde=False,color = 'salmon')\nplt.title(\"Count The Number of Each Index Name\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Jersey Fancy covered highest percentage if the products, and most selling to baby and ladies. Second selling peoduct is accessories.**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (20,10))\nsns.histplot(data=articles_data,y='garment_group_name',hue = 'index_name',kde=False,color = 'olive')\nplt.title(\"The Garment Group Distributed by Index Group\")\nplt.xlabel(\"Number of Each Garment Group\")\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Checking the number of unique values in each columns.**","metadata":{}},{"cell_type":"code","source":"col = [cname for cname in articles_data.columns if articles_data[cname].dtype in ['object']]\nfor i in col:\n    columns = articles_data[i].nunique()\n    print(f'{i} : {columns} \\n')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10,5))\ngroup_sales = articles_data.groupby(['product_group_name', 'product_type_name']).count().sort_values('article_id',ascending = False)['article_id'][:20]\ngroup_sales.plot(kind = 'bar',grid = True,color = 'salmon')\nplt.xlabel(\"Products Related\")\nplt.ylabel(\"Article Numbers Include\")\nplt.title(\"Top 20 Article Numbers Related\")\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Customer data:","metadata":{}},{"cell_type":"code","source":"#customers_data = pd.read_csv(customers_file)\ncustomers_data.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**None of the duplicates values in Customers**","metadata":{}},{"cell_type":"code","source":"len(customers_data)- customers_data['customer_id'].nunique()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**One of the postcode shows abnormal value. It include 120303 customer id, which might be nan of address or distribution center address.**","metadata":{}},{"cell_type":"code","source":"postal_num = customers_data.groupby(['postal_code'],as_index = 'False').count().sort_values('customer_id',ascending = False)\npostal_num.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The most common age is 21-25:**","metadata":{}},{"cell_type":"code","source":"age_distribute = customers_data.groupby(['age'],as_index = 'False').count().sort_values('customer_id',ascending = False)\nage_distribute.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10,5))\nsns.histplot(data=customers_data,x ='age',kde=True,color = 'salmon')\nplt.title(\"Distribution Of Customer Ages\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Most of customers has an active club status,some of them begin to activate it (pre-create).Seldom of the customers give up membership. 64.73% of the customers refused the news sended by H&M.**","metadata":{}},{"cell_type":"code","source":"fig, axes = plt.subplots(2,1,figsize=(10,10))\nsns.histplot(data=customers_data,x ='club_member_status',kde=False,color = 'chocolate',ax = axes[0])\nsns.histplot(data=customers_data,x ='fashion_news_frequency',kde=False,color = 'rosybrown',ax = axes[1])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f, ax = plt.subplots(figsize=(7,7))\npie_data = customers_data[['customer_id', 'fashion_news_frequency']].groupby('fashion_news_frequency').count()\nax.pie(pie_data.customer_id, labels=pie_data.index, colors = sns.color_palette('pastel'),autopct = '%.2f%%',pctdistance = 0.6,labeldistance = 0.8,startangle= 0)\nax.set_xlabel('Distribution of fashion news frequency')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3.Transation data","metadata":{}},{"cell_type":"markdown","source":"**Transactions data description:**\n* t_dat : A unique identifier of every customer\n* customer_id : A unique identifier of every customer (in  customers table)\n* article_id : A unique identifier of every article (in  articles table)\n* price : Price of purchase\n* sales_channel_id : 1 or 2","metadata":{}},{"cell_type":"code","source":"transactions_data = pd.read_csv(transactions_train_file)\ntransactions_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T13:09:59.141131Z","iopub.execute_input":"2022-05-14T13:09:59.141682Z","iopub.status.idle":"2022-05-14T13:11:19.718312Z","shell.execute_reply.started":"2022-05-14T13:09:59.141645Z","shell.execute_reply":"2022-05-14T13:11:19.717297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Checking the outlier values of the prices.**","metadata":{}},{"cell_type":"code","source":"pd.set_option('display.float_format',  '{:,.6f}'.format)\nprint(transactions_data.describe()['price'])\nplt.figure(figsize = (10,5))\nplt.title(\"Price Outliers\")\nsns.boxplot(data = transactions_data,x = 'price',color = 'salmon')\nsns.displot(transactions_data['price'], bins = 10,kde=False,color = 'salmon')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Top 10 customers purchases:**","metadata":{}},{"cell_type":"code","source":"df1 = transactions_data.groupby(['customer_id'])['price'].count()\ndf2 = transactions_data.groupby(['customer_id'])['price'].sum()\nsales = pd.merge(df1,df2,on=\"customer_id\")\nsales.columns = [\"purchase_times\",\"money_purchased\"]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales.sort_values(by = 'money_purchased',ascending=False)[:10]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Picking up the data to create an basic-table for analysis the outlier values.**","metadata":{}},{"cell_type":"code","source":"data1 = articles_data[['article_id','prod_name','product_group_name','product_type_name','colour_group_name','index_name']]\ndata2 = transactions_data [['customer_id','article_id','price']]\ndata  = pd.merge(data2,data1,on = \"article_id\",how = \"left\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Garment Upper/Lower/Full body shows huge price varience. Besides, Accessories shows higher price varience than Shoes product price.**","metadata":{}},{"cell_type":"code","source":"f,ax = plt.subplots(figsize=(25,18))\nax = sns.boxplot(data=data, 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)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**In Accessories products' section, bag and scarf shows higher price variences than other types.**","metadata":{}},{"cell_type":"code","source":"f,ax = plt.subplots(figsize=(25,18))\nax1 = sns.boxplot(data=data[data['product_group_name'] == 'Accessories'], x='price', y='product_type_name')\nax1.set_xlabel('Price outliers', fontsize=22)\nax1.set_ylabel('Index names', fontsize=22)\nax1.xaxis.set_tick_params(labelsize=22)\nax1.yaxis.set_tick_params(labelsize=22)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Striping date time (t_dat) for training dataset:**","metadata":{}},{"cell_type":"code","source":"transactions_data.t_dat = pd.to_datetime(transactions_data.t_dat)\ntransactions_data['year'] = (transactions_data.t_dat.dt.year-2000).astype('int8')\ntransactions_data['month'] = (transactions_data.t_dat.dt.month).astype('int8')\ntransactions_data['week'] = (transactions_data.t_dat.dt.isocalendar().week).astype('int8')\ntransactions_data['Month_Of_Year'] = transactions_data.t_dat.apply(lambda x:x.strftime('%Y%m'))","metadata":{"execution":{"iopub.status.busy":"2022-05-14T13:11:19.721354Z","iopub.execute_input":"2022-05-14T13:11:19.722391Z","iopub.status.idle":"2022-05-14T13:16:51.377728Z","shell.execute_reply.started":"2022-05-14T13:11:19.722335Z","shell.execute_reply":"2022-05-14T13:16:51.376426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Base on the data between 2018-09-20 and 2020-09-22, top article transaction numbers shows in June of 2019 and 2020.**","metadata":{}},{"cell_type":"code","source":"article_nums = transactions_data.groupby(['Month_Of_Year']).count().reset_index()\narticle_nums = pd.DataFrame(article_nums)\narticle_sales = article_nums[['Month_Of_Year','article_id']]\narticle_sales.columns = ['Month','Article_Transaction_Number']","metadata":{"execution":{"iopub.status.busy":"2022-05-14T11:38:26.169034Z","iopub.execute_input":"2022-05-14T11:38:26.169365Z","iopub.status.idle":"2022-05-14T11:38:38.870824Z","shell.execute_reply.started":"2022-05-14T11:38:26.169334Z","shell.execute_reply":"2022-05-14T11:38:38.869572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,5))\nax = sns.lineplot(data=article_sales,x = 'Month',y = 'Article_Transaction_Number',color = 'salmon')\nax.set(title = \"Article Transaction Number Each Month\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-14T11:45:58.855287Z","iopub.execute_input":"2022-05-14T11:45:58.856557Z","iopub.status.idle":"2022-05-14T11:45:59.143248Z","shell.execute_reply.started":"2022-05-14T11:45:58.856475Z","shell.execute_reply":"2022-05-14T11:45:59.142356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Base on the data shows by week","metadata":{}},{"cell_type":"code","source":"data2018 = transactions_data[(transactions_data[\"year\"] == 18) & (transactions_data[\"week\"] != 1)]\ndata2019 = transactions_data[(transactions_data[\"year\"] == 19) & (transactions_data[\"week\"] != 1)]\ndata2020 = transactions_data[(transactions_data[\"year\"] == 20) & (transactions_data[\"week\"] != 1)]","metadata":{"execution":{"iopub.status.busy":"2022-05-14T13:16:51.379825Z","iopub.execute_input":"2022-05-14T13:16:51.380210Z","iopub.status.idle":"2022-05-14T13:16:57.627745Z","shell.execute_reply.started":"2022-05-14T13:16:51.380169Z","shell.execute_reply":"2022-05-14T13:16:57.626565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"article2018 = data2018.groupby(['week']).count().reset_index().sort_values('week',ascending = False)\narticle2019 = data2019.groupby(['week']).count().reset_index().sort_values('week',ascending = False)\narticle2020 = data2020.groupby(['week']).count().reset_index().sort_values('week',ascending = False)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T13:16:57.628911Z","iopub.execute_input":"2022-05-14T13:16:57.629168Z","iopub.status.idle":"2022-05-14T13:17:07.754558Z","shell.execute_reply.started":"2022-05-14T13:16:57.629139Z","shell.execute_reply":"2022-05-14T13:17:07.753521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib\nmatplotlib.rcParams.update({'font.size':20})\nfig, axes = plt.subplots(figsize=(20,10))\nsns.lineplot(data=article2018,x = 'week',y = 'article_id',label = \"2018\",color = 'salmon',linewidth = 3)\nsns.lineplot(data=article2019,x = 'week',y = 'article_id',label = \"2019\",color = 'olive',linewidth = 3)\nsns.lineplot(data=article2020,x = 'week',y = 'article_id',label = \"2020\",color = 'blue',linewidth = 3)\naxes.set(title = \"Article Transaction Number Each Week\",xlabel = \"Week\",ylabel = \"Article Transaction Number\")\nplt.xticks(article2019['week'],fontsize=10)\nplt.legend(fontsize=16)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T13:17:07.757381Z","iopub.execute_input":"2022-05-14T13:17:07.758469Z","iopub.status.idle":"2022-05-14T13:17:08.764615Z","shell.execute_reply.started":"2022-05-14T13:17:07.758408Z","shell.execute_reply":"2022-05-14T13:17:08.762311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Checking the customer purchasing the number of articles during different time period.**","metadata":{}},{"cell_type":"code","source":"customer2018 = data2018.groupby('week',as_index=False).apply(lambda x:x.drop_duplicates('customer_id'))\ncustomer2019 = data2019.groupby('week',as_index=False).apply(lambda x:x.drop_duplicates('customer_id'))\ncustomer2020 = data2020.groupby('week',as_index=False).apply(lambda x:x.drop_duplicates('customer_id'))","metadata":{"execution":{"iopub.status.busy":"2022-05-14T13:36:42.464940Z","iopub.execute_input":"2022-05-14T13:36:42.465254Z","iopub.status.idle":"2022-05-14T13:37:00.105465Z","shell.execute_reply.started":"2022-05-14T13:36:42.465221Z","shell.execute_reply":"2022-05-14T13:37:00.104391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer2018_num = customer2018.groupby('week',as_index=False).count().reset_index().sort_values('week',ascending = False)\ncustomer2019_num = customer2019.groupby('week',as_index=False).count().reset_index().sort_values('week',ascending = False)\ncustomer2020_num = customer2020.groupby('week',as_index=False).count().reset_index().sort_values('week',ascending = False)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T13:37:00.107238Z","iopub.execute_input":"2022-05-14T13:37:00.107492Z","iopub.status.idle":"2022-05-14T13:37:03.960411Z","shell.execute_reply.started":"2022-05-14T13:37:00.107462Z","shell.execute_reply":"2022-05-14T13:37:03.959249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib\nmatplotlib.rcParams.update({'font.size':20})\nfig, axes = plt.subplots(figsize=(20,10))\nsns.lineplot(data=customer2018_num,x = 'week',y = 'customer_id',label = \"2018\",color = 'salmon',linewidth = 3)\nsns.lineplot(data=customer2019_num,x = 'week',y = 'customer_id',label = \"2019\",color = 'olive',linewidth = 3)\nsns.lineplot(data=customer2020_num,x = 'week',y = 'customer_id',label = \"2020\",color = 'blue',linewidth = 3)\naxes.set(title = \"Number Of Transaction Customers Each Week\",xlabel = \"Week\",ylabel = \"Customer Number\")\nplt.xticks(article2019['week'],fontsize=10)\nplt.legend(fontsize=16)","metadata":{"execution":{"iopub.status.busy":"2022-05-14T13:41:05.236665Z","iopub.execute_input":"2022-05-14T13:41:05.237039Z","iopub.status.idle":"2022-05-14T13:41:06.063438Z","shell.execute_reply.started":"2022-05-14T13:41:05.236999Z","shell.execute_reply":"2022-05-14T13:41:06.062660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#cust_data = customers_data[['age','customer_id','postal_code']]\n#cust_tr_data = pd.merge(data2,cust_data,on = \"customer_id\",how = \"left\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**3.best seller and lowest seller will effect the most sales section:\ncheck it and finding the price changing during the time period.**  \n1. try different sales product combine with the price from transaction\n2. analysis the customer group by purchasing withe the price from transaction ","metadata":{}},{"cell_type":"markdown","source":"## 4. H&M Analytics-RFM","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5. k-means for analytic","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}