{"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 matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:29:30.704210Z","iopub.execute_input":"2022-03-16T17:29:30.704684Z","iopub.status.idle":"2022-03-16T17:29:31.936550Z","shell.execute_reply.started":"2022-03-16T17:29:30.704544Z","shell.execute_reply":"2022-03-16T17:29:31.934945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_articles = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv\")\ndf_articles.drop_duplicates(inplace=True)\ndf_articles['article_id'] = df_articles['article_id'].astype('int32')\nprint(\"Missing values (%):\")\nprint(df_articles.isna().sum() * 100 / len(df_articles))","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:29:31.938378Z","iopub.execute_input":"2022-03-16T17:29:31.938635Z","iopub.status.idle":"2022-03-16T17:29:33.802064Z","shell.execute_reply.started":"2022-03-16T17:29:31.938607Z","shell.execute_reply":"2022-03-16T17:29:33.800863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_customers = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv\")\ndf_customers.drop_duplicates(inplace=True)\ndf_customers['customer_id'] =\\\n    df_customers['customer_id'].apply(lambda x: int(x[-16:],16) ).astype('int64')\nprint(\"Missing values (%):\")\nprint(df_customers.isna().sum() * 100 / len(df_customers))","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:29:33.803245Z","iopub.execute_input":"2022-03-16T17:29:33.803530Z","iopub.status.idle":"2022-03-16T17:29:45.102857Z","shell.execute_reply.started":"2022-03-16T17:29:33.803495Z","shell.execute_reply":"2022-03-16T17:29:45.101739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_transactions = pd.read_csv(\"/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv\")\ndf_transactions['article_id'] = df_transactions['article_id'].astype('int32')\ndf_transactions['customer_id'] =\\\n    df_transactions['customer_id'].apply(lambda x: int(x[-16:],16) ).astype('int64')\ndf_transactions.t_dat = pd.to_datetime(df_transactions.t_dat)\ndf_transactions['year'] = (df_transactions.t_dat.dt.year-2000).astype('int8')\ndf_transactions['month'] = (df_transactions.t_dat.dt.month).astype('int8')\ndf_transactions['day'] = (df_transactions.t_dat.dt.day).astype('int8')\nprint(\"Missing values (%):\")\nprint(df_transactions.isna().sum() * 100 / len(df_transactions))","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:29:45.104425Z","iopub.execute_input":"2022-03-16T17:29:45.104828Z","iopub.status.idle":"2022-03-16T17:31:50.417704Z","shell.execute_reply.started":"2022-03-16T17:29:45.104795Z","shell.execute_reply":"2022-03-16T17:31:50.416545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bestsellers_ranking = df_transactions.groupby('article_id').count().sort_values(by='customer_id', ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:31:50.419909Z","iopub.execute_input":"2022-03-16T17:31:50.420181Z","iopub.status.idle":"2022-03-16T17:31:53.243725Z","shell.execute_reply.started":"2022-03-16T17:31:50.420146Z","shell.execute_reply":"2022-03-16T17:31:53.242374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"article_sales = df_transactions.groupby('article_id').count()\ndef f(x):\n    try:\n        return article_sales.loc[x]['customer_id']\n    except:\n        return 0\ndf_articles['total_sales_2019'] = df_articles['article_id'].apply(f)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:31:53.246145Z","iopub.execute_input":"2022-03-16T17:31:53.246479Z","iopub.status.idle":"2022-03-16T17:32:05.600157Z","shell.execute_reply.started":"2022-03-16T17:31:53.246441Z","shell.execute_reply":"2022-03-16T17:32:05.599005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"to_drop = ['product_type_no',\n 'graphical_appearance_no',\n 'colour_group_code',\n 'perceived_colour_value_id',\n 'perceived_colour_master_id',\n 'department_no',\n 'index_code',\n 'index_group_no',\n 'section_no',\n 'garment_group_no']\ndf_articles.drop(columns=to_drop, axis=1, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:32:05.601888Z","iopub.execute_input":"2022-03-16T17:32:05.602168Z","iopub.status.idle":"2022-03-16T17:32:05.629626Z","shell.execute_reply.started":"2022-03-16T17:32:05.602136Z","shell.execute_reply":"2022-03-16T17:32:05.628824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bestsellers = df_articles.sort_values(by='total_sales_2019', ascending=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:32:05.631859Z","iopub.execute_input":"2022-03-16T17:32:05.632519Z","iopub.status.idle":"2022-03-16T17:32:05.678109Z","shell.execute_reply.started":"2022-03-16T17:32:05.632468Z","shell.execute_reply":"2022-03-16T17:32:05.677090Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Color Group of Bestsellers on H&M**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\ng = sns.countplot(x=\"colour_group_name\",data=bestsellers,palette=\"pastel\")\ng.bar_label(g.containers[0])\ng.tick_params(axis='x', rotation=90)\nplt.title('Color Group of Bestsellers on H&M')\nplt.show(g)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:32:05.679559Z","iopub.execute_input":"2022-03-16T17:32:05.680077Z","iopub.status.idle":"2022-03-16T17:32:06.955826Z","shell.execute_reply.started":"2022-03-16T17:32:05.679948Z","shell.execute_reply":"2022-03-16T17:32:06.954793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,5))\ng = sns.countplot(x=\"garment_group_name\",data=df_articles,palette=\"pastel\")\ng.bar_label(g.containers[0])\ng.tick_params(axis='x', rotation=90)\nplt.title('All Articles on H&M')\nplt.show(g)\nplt.figure(figsize=(15,5))\nh = sns.countplot(x=\"garment_group_name\", data=bestsellers,palette=\"pastel\")\nh.bar_label(h.containers[0])\nh.tick_params(axis='x', rotation=90)\nplt.title('Bestselling Articles on H&M')\nplt.show(h)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:32:06.957406Z","iopub.execute_input":"2022-03-16T17:32:06.957648Z","iopub.status.idle":"2022-03-16T17:32:08.149013Z","shell.execute_reply.started":"2022-03-16T17:32:06.957618Z","shell.execute_reply":"2022-03-16T17:32:08.147758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bestsellers_transactions = df_transactions[df_transactions['article_id'].isin(bestsellers['article_id'])]\nbestsellers_contributors = df_customers[df_customers['customer_id'].isin(bestsellers_transactions['customer_id'])]","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:32:08.150763Z","iopub.execute_input":"2022-03-16T17:32:08.151116Z","iopub.status.idle":"2022-03-16T17:32:11.367935Z","shell.execute_reply.started":"2022-03-16T17:32:08.151073Z","shell.execute_reply":"2022-03-16T17:32:11.366513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"g = sns.histplot(df_customers['age'],kde=False)\nplt.title('Age of All Customers')\nplt.show(g)\ng = sns.histplot(bestsellers_contributors['age'],kde=False)\nplt.title('Customers Age Who Bought Bestsellers')\nplt.show(g)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:53:00.129468Z","iopub.execute_input":"2022-03-16T17:53:00.130559Z","iopub.status.idle":"2022-03-16T17:53:03.626042Z","shell.execute_reply.started":"2022-03-16T17:53:00.130475Z","shell.execute_reply":"2022-03-16T17:53:03.624886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_articles.columns","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:41:14.269395Z","iopub.execute_input":"2022-03-16T17:41:14.269749Z","iopub.status.idle":"2022-03-16T17:41:14.278190Z","shell.execute_reply.started":"2022-03-16T17:41:14.269713Z","shell.execute_reply":"2022-03-16T17:41:14.277475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"article_revenue = df_transactions.groupby('article_id').sum()\ndef f(x):\n    try:\n        return article_revenue.loc[x]['price']\n    except:\n        return 0\ndf_articles['total_revenue_2019'] = df_articles['article_id'].apply(f)","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:44:29.862657Z","iopub.execute_input":"2022-03-16T17:44:29.863130Z","iopub.status.idle":"2022-03-16T17:44:44.820625Z","shell.execute_reply.started":"2022-03-16T17:44:29.863064Z","shell.execute_reply":"2022-03-16T17:44:44.819251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index_group_revenue = df_articles[['index_group_name','total_revenue_2019']].groupby('index_group_name').total_revenue_2019.sum().reset_index()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:44:52.914451Z","iopub.execute_input":"2022-03-16T17:44:52.915570Z","iopub.status.idle":"2022-03-16T17:44:52.937473Z","shell.execute_reply.started":"2022-03-16T17:44:52.915520Z","shell.execute_reply":"2022-03-16T17:44:52.936121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Revenue % by each Group**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8,8))\ncolors = sns.color_palette('pastel')\nplt.pie(x=index_group_revenue['total_revenue_2019'], labels=index_group_revenue['index_group_name'], colors=colors, autopct='%1.1f%%')\nplt.title('Revenue Contribution by Index Group')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-16T17:45:24.846830Z","iopub.execute_input":"2022-03-16T17:45:24.847195Z","iopub.status.idle":"2022-03-16T17:45:25.062672Z","shell.execute_reply.started":"2022-03-16T17:45:24.847160Z","shell.execute_reply":"2022-03-16T17:45:25.060954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}