{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":31254,"databundleVersionId":3103714,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom datetime import datetime","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-23T03:45:24.881669Z","iopub.execute_input":"2024-08-23T03:45:24.882147Z","iopub.status.idle":"2024-08-23T03:45:24.888666Z","shell.execute_reply.started":"2024-08-23T03:45:24.882104Z","shell.execute_reply":"2024-08-23T03:45:24.887164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"articles = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv')\ncustomer = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv')\ntrans = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:29:50.670809Z","iopub.execute_input":"2024-08-23T03:29:50.671294Z","iopub.status.idle":"2024-08-23T03:31:16.711117Z","shell.execute_reply.started":"2024-08-23T03:29:50.671262Z","shell.execute_reply":"2024-08-23T03:31:16.709958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Customer ","metadata":{}},{"cell_type":"code","source":"customer.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:16.734881Z","iopub.execute_input":"2024-08-23T03:31:16.735402Z","iopub.status.idle":"2024-08-23T03:31:16.763075Z","shell.execute_reply.started":"2024-08-23T03:31:16.735336Z","shell.execute_reply":"2024-08-23T03:31:16.761806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer.info()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:16.764311Z","iopub.execute_input":"2024-08-23T03:31:16.764627Z","iopub.status.idle":"2024-08-23T03:31:17.388074Z","shell.execute_reply.started":"2024-08-23T03:31:16.764601Z","shell.execute_reply":"2024-08-23T03:31:17.387006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer['Active'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:17.390805Z","iopub.execute_input":"2024-08-23T03:31:17.391126Z","iopub.status.idle":"2024-08-23T03:31:17.415064Z","shell.execute_reply.started":"2024-08-23T03:31:17.391097Z","shell.execute_reply":"2024-08-23T03:31:17.413998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"**Tỷ lệ dữ liệu của tập Customer theo trường status**","metadata":{}},{"cell_type":"code","source":"column_name = 'club_member_status'\n\n# Count the values in the specified column\nvalue_counts = customer[column_name].value_counts()\n\n# Create a pie chart\nplt.figure(figsize=(10, 8))\nplt.pie(value_counts.values, labels=value_counts.index, autopct='%1.1f%%', startangle=180)\nplt.title(f'Distribution of {column_name}')\nplt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle\n\n# Display the chart\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:17.416577Z","iopub.execute_input":"2024-08-23T03:31:17.417714Z","iopub.status.idle":"2024-08-23T03:31:17.907039Z","shell.execute_reply.started":"2024-08-23T03:31:17.417682Z","shell.execute_reply":"2024-08-23T03:31:17.906027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****Tỷ lệ của tập Customer theo trường frequency ****","metadata":{}},{"cell_type":"code","source":"column_name = 'fashion_news_frequency'\n\n# Count the values in the specified column\nvalue_counts = customer[column_name].value_counts()\n\n# Create a pie chart\nplt.figure(figsize=(10, 8))\nplt.pie(value_counts.values, labels=value_counts.index, autopct='%1.1f%%', startangle=180)\nplt.title(f'Distribution of {column_name}')\nplt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle\n\n# Display the chart\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:17.908454Z","iopub.execute_input":"2024-08-23T03:31:17.908796Z","iopub.status.idle":"2024-08-23T03:31:18.439904Z","shell.execute_reply.started":"2024-08-23T03:31:17.908744Z","shell.execute_reply":"2024-08-23T03:31:18.438666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Boxplot của tập customer dựa trên trường tuổi**","metadata":{}},{"cell_type":"code","source":"sns.boxplot(customer['age'])","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:18.441580Z","iopub.execute_input":"2024-08-23T03:31:18.442169Z","iopub.status.idle":"2024-08-23T03:31:18.707885Z","shell.execute_reply.started":"2024-08-23T03:31:18.442130Z","shell.execute_reply":"2024-08-23T03:31:18.706834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"customer['age'].describe()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:18.709378Z","iopub.execute_input":"2024-08-23T03:31:18.709774Z","iopub.status.idle":"2024-08-23T03:31:18.796210Z","shell.execute_reply.started":"2024-08-23T03:31:18.709730Z","shell.execute_reply":"2024-08-23T03:31:18.795169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ phân bố của trường tuổi trên tập Customer**","metadata":{}},{"cell_type":"code","source":"sns.set_style('whitegrid')\nsns.displot(customer['age'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:18.798661Z","iopub.execute_input":"2024-08-23T03:31:18.799442Z","iopub.status.idle":"2024-08-23T03:31:21.427475Z","shell.execute_reply.started":"2024-08-23T03:31:18.799400Z","shell.execute_reply":"2024-08-23T03:31:21.426438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ số lượng khách hàng theo trường Status dựa trên trường Frequency**","metadata":{}},{"cell_type":"code","source":"column2 = 'fashion_news_frequency'\ncolumn1 = 'club_member_status'\n\n# Create a cross-tabulation of the two columns\ncrosstab = pd.crosstab(customer[column2], customer[column1])\n\n# Create the grouped bar chart\nax = crosstab.plot(kind='bar', figsize=(12, 6), width=0.8)\n\n# Customize the chart\nplt.title(f'{column1} grouped by {column2}')\nplt.xlabel(column2)\nplt.ylabel('Count')\nplt.legend(title=column1, bbox_to_anchor=(1.05, 1), loc='upper left')\n\n# Rotate x-axis labels for better readability\nplt.xticks(rotation=15, ha='right')\n\n# Adjust layout to prevent cutting off labels\nplt.tight_layout()\n\n# Add value labels on top of each bar\nfor container in ax.containers:\n    ax.bar_label(container, label_type='edge')\n\n# Display the chart\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:21.429012Z","iopub.execute_input":"2024-08-23T03:31:21.429430Z","iopub.status.idle":"2024-08-23T03:31:22.621330Z","shell.execute_reply.started":"2024-08-23T03:31:21.429390Z","shell.execute_reply":"2024-08-23T03:31:22.620161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bins = [0, 18, 35, 50, 65, 100]\nlabels = ['0-18', '19-35', '36-50', '51-65', '66+']\n\n# Create a new column 'age_group' with the corresponding age groups\ncustomer['age_group'] = pd.cut(customer['age'], bins=bins, labels=labels, right=False)\n\n# If you want to create separate columns for each age group with binary indicators\nfor label in labels:\n    customer[label] = (customer['age_group'] == label).astype(int)","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:22.623041Z","iopub.execute_input":"2024-08-23T03:31:22.623486Z","iopub.status.idle":"2024-08-23T03:31:22.687610Z","shell.execute_reply.started":"2024-08-23T03:31:22.623446Z","shell.execute_reply":"2024-08-23T03:31:22.686628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ phân bố số lượng theo Status khách hàng dựa trên các nhóm tuổi**","metadata":{}},{"cell_type":"code","source":"column2 = 'age_group'\ncolumn1 = 'club_member_status'\n\n# Create a cross-tabulation of the two columns\ncrosstab = pd.crosstab(customer[column2], customer[column1])\n\n# Create the grouped bar chart\nax = crosstab.plot(kind='bar', figsize=(12, 6), width=0.8)\n\n# Customize the chart\nplt.title(f'{column1} grouped by {column2}')\nplt.xlabel(column2)\nplt.ylabel('Count')\nplt.legend(title=column1, bbox_to_anchor=(1.05, 1), loc='upper left')\n\n# Rotate x-axis labels for better readability\nplt.xticks(rotation=45, ha='right')\n\n# Adjust layout to prevent cutting off labels\nplt.tight_layout()\n\n# Add value labels on top of each bar\nfor container in ax.containers:\n    ax.bar_label(container, label_type='edge')\n\n# Display the chart\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:22.689076Z","iopub.execute_input":"2024-08-23T03:31:22.689425Z","iopub.status.idle":"2024-08-23T03:31:23.726920Z","shell.execute_reply.started":"2024-08-23T03:31:22.689397Z","shell.execute_reply":"2024-08-23T03:31:23.725704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ phân bố số lượng theo Frequency khách hàng dựa trên các nhóm tuổi**","metadata":{}},{"cell_type":"code","source":"column2 = 'age_group'\ncolumn1 = 'fashion_news_frequency'\n\n# Create a cross-tabulation of the two columns\ncrosstab = pd.crosstab(customer[column2], customer[column1])\n\n# Create the grouped bar chart\nax = crosstab.plot(kind='bar', figsize=(12, 6), width=0.8)\n\n# Customize the chart\nplt.title(f'{column1} grouped by {column2}')\nplt.xlabel(column2)\nplt.ylabel('Count')\nplt.legend(title=column1, bbox_to_anchor=(1.05, 1), loc='upper left')\n\n# Rotate x-axis labels for better readability\nplt.xticks(rotation=45, ha='right')\n\n# Adjust layout to prevent cutting off labels\nplt.tight_layout()\n\n# Add value labels on top of each bar\nfor container in ax.containers:\n    ax.bar_label(container, label_type='edge')\n\n# Display the chart\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:23.732560Z","iopub.execute_input":"2024-08-23T03:31:23.732947Z","iopub.status.idle":"2024-08-23T03:31:24.790321Z","shell.execute_reply.started":"2024-08-23T03:31:23.732912Z","shell.execute_reply":"2024-08-23T03:31:24.788454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Transactions","metadata":{}},{"cell_type":"code","source":"trans.head(5)","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:24.791885Z","iopub.execute_input":"2024-08-23T03:31:24.792303Z","iopub.status.idle":"2024-08-23T03:31:24.809381Z","shell.execute_reply.started":"2024-08-23T03:31:24.792266Z","shell.execute_reply":"2024-08-23T03:31:24.807828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans.info()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:24.811057Z","iopub.execute_input":"2024-08-23T03:31:24.811465Z","iopub.status.idle":"2024-08-23T03:31:24.826367Z","shell.execute_reply.started":"2024-08-23T03:31:24.811431Z","shell.execute_reply":"2024-08-23T03:31:24.825207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Phân bố của trường giá theo các kênh bán hàng trong tập Transaction**","metadata":{}},{"cell_type":"code","source":"df = trans.sample(100_000)\nfig, ax = plt.subplots(1, 1, figsize=(14, 7))\nsns.kdeplot(np.log(df.loc[df[\"sales_channel_id\"]==1].price.value_counts()))\nsns.kdeplot(np.log(df.loc[df[\"sales_channel_id\"]==2].price.value_counts()))\nax.legend(labels=['Sales channel 1', 'Sales channel 2'])\nplt.title(\"Logaritmic distribution of price frequency in transactions, grouped per sales channel (100k sample)\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:44:07.953612Z","iopub.execute_input":"2024-08-23T03:44:07.954367Z","iopub.status.idle":"2024-08-23T03:44:10.284215Z","shell.execute_reply.started":"2024-08-23T03:44:07.954319Z","shell.execute_reply":"2024-08-23T03:44:10.283083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ thể hiện số lượng giao dịch theo từng ngày**","metadata":{}},{"cell_type":"code","source":"df = trans.sample(100_000).groupby([\"t_dat\"])[\"article_id\"].count().reset_index()\ndf[\"t_dat\"] = df[\"t_dat\"].apply(lambda x: datetime.strptime(x, '%Y-%m-%d'))\ndf.columns = [\"Date\", \"Transactions\"]\nfig, ax = plt.subplots(1, 1, figsize=(16,6))\nplt.plot(df[\"Date\"], df[\"Transactions\"], color=\"Darkgreen\")\nplt.xlabel(\"Date\")\nplt.ylabel(\"Transactions\")\nplt.title(f\"Transactions per day (100k sample; to get the real volume, please consider that real transaction count is {round(trans.shape[0]/10.e6,2)}M)\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:45:55.585555Z","iopub.execute_input":"2024-08-23T03:45:55.586615Z","iopub.status.idle":"2024-08-23T03:45:58.011866Z","shell.execute_reply.started":"2024-08-23T03:45:55.586577Z","shell.execute_reply":"2024-08-23T03:45:58.010522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ thể hiện số giao dịch theo từng ngày bởi các kênh khác nhau**","metadata":{}},{"cell_type":"code","source":"df = trans.sample(100_000).groupby([\"t_dat\", \"sales_channel_id\"])[\"article_id\"].count().reset_index()\ndf[\"t_dat\"] = df[\"t_dat\"].apply(lambda x: datetime.strptime(x, '%Y-%m-%d'))\ndf.columns = [\"Date\", \"Sales Channel Id\", \"Transactions\"]\nfig, ax = plt.subplots(1, 1, figsize=(16,6))\ng1 = ax.plot(df.loc[df[\"Sales Channel Id\"]==1, \"Date\"], df.loc[df[\"Sales Channel Id\"]==1, \"Transactions\"], label=\"Sales Channel 1\", color=\"Darkblue\")\ng2 = ax.plot(df.loc[df[\"Sales Channel Id\"]==2, \"Date\"], df.loc[df[\"Sales Channel Id\"]==2, \"Transactions\"], label=\"Sales Channel 2\", color=\"Magenta\")\nplt.xlabel(\"Date\")\nplt.ylabel(\"Transactions\")\nax.legend()\nplt.title(f\"Transactions per day, grouped by Sales Channel (100k sample; to get the real volume, please consider that real transaction count is {round(trans.shape[0]/10.e6,2)}M)\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:48:03.919171Z","iopub.execute_input":"2024-08-23T03:48:03.919641Z","iopub.status.idle":"2024-08-23T03:48:06.644683Z","shell.execute_reply.started":"2024-08-23T03:48:03.919605Z","shell.execute_reply":"2024-08-23T03:48:06.643626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đề thể hiện số lượng sản phẩm riêng biệt được bán ra theo từng ngày theo các kênh bán hàng khác nhau**","metadata":{}},{"cell_type":"code","source":"df = trans.groupby([\"t_dat\", \"sales_channel_id\"])[\"article_id\"].nunique().reset_index()\ndf[\"t_dat\"] = df[\"t_dat\"].apply(lambda x: datetime.strptime(x, '%Y-%m-%d'))\ndf.columns = [\"Date\", \"Sales Channel Id\", \"Unique Articles\"]\nfig, ax = plt.subplots(1, 1, figsize=(16,6))\ng1 = ax.plot(df.loc[df[\"Sales Channel Id\"]==1, \"Date\"], df.loc[df[\"Sales Channel Id\"]==1, \"Unique Articles\"], label=\"Sales Channel 1\", color=\"Blue\")\ng2 = ax.plot(df.loc[df[\"Sales Channel Id\"]==2, \"Date\"], df.loc[df[\"Sales Channel Id\"]==2, \"Unique Articles\"], label=\"Sales Channel 2\", color=\"Green\")\nplt.xlabel(\"Date\")\nplt.ylabel(\"Unique Articles / Day\")\nax.legend()\nplt.title(f\"Unique articles per day, grouped by Sales Channel\")\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:49:11.822694Z","iopub.execute_input":"2024-08-23T03:49:11.823461Z","iopub.status.idle":"2024-08-23T03:49:22.177529Z","shell.execute_reply.started":"2024-08-23T03:49:11.823427Z","shell.execute_reply":"2024-08-23T03:49:22.176400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans1 = trans.groupby('t_dat').size().reset_index(name='number of transaction')\n\ntrans1['t_dat'] = pd.to_datetime(trans1['t_dat'])\ntrans1['month'] = trans1['t_dat'].dt.to_period('M')\nmonthly_avg = trans1.groupby('month')['number of transaction'].mean()\n\n# 3. Plot the average monthly transactions\nplt.figure(figsize=(10, 6))\nmonthly_avg.plot(kind='bar', color='skyblue')\nplt.title('Average Monthly Transactions')\nplt.ylabel('Average Transactions')\nplt.xlabel('Month')\nplt.xticks(rotation=45)\nplt.show()\n\n\n\n\n# Show the plot\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:24.828416Z","iopub.execute_input":"2024-08-23T03:31:24.828826Z","iopub.status.idle":"2024-08-23T03:31:31.771051Z","shell.execute_reply.started":"2024-08-23T03:31:24.828791Z","shell.execute_reply":"2024-08-23T03:31:31.769911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trans2 = trans.groupby('t_dat')['price'].sum().reset_index(name='price')\ntrans2['t_dat'] = pd.to_datetime(trans2['t_dat'])\ntrans2['month'] = trans2['t_dat'].dt.to_period('M')\nmonthly_avg = trans2.groupby('month')['price'].mean()\n\n# 3. Plot the average monthly transactions\nplt.figure(figsize=(10, 6))\nmonthly_avg.plot(kind='bar', color='red')\nplt.title('Average Monthly Price')\nplt.ylabel('Average Price')\nplt.xlabel('Month')\nplt.xticks(rotation=45)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:31.772419Z","iopub.execute_input":"2024-08-23T03:31:31.772729Z","iopub.status.idle":"2024-08-23T03:31:38.417733Z","shell.execute_reply.started":"2024-08-23T03:31:31.772702Z","shell.execute_reply":"2024-08-23T03:31:38.416454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Articles","metadata":{}},{"cell_type":"code","source":"articles.info()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:38.419501Z","iopub.execute_input":"2024-08-23T03:31:38.419950Z","iopub.status.idle":"2024-08-23T03:31:38.594272Z","shell.execute_reply.started":"2024-08-23T03:31:38.419909Z","shell.execute_reply":"2024-08-23T03:31:38.593184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ thể hiện tỷ lệ sản phẩm dựa trên nhóm sản phẩm**","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\n\n# Assuming 'articles' is your DataFrame\ncolumn_name = 'product_group_name'\n\n# 1. Count the values in the specified column\nvalue_counts = articles[column_name].value_counts()\n\n# 2. Calculate the total number of entries and the percentage for each category\ntotal_count = value_counts.sum()\npercentage = (value_counts / total_count) * 100\n\n# 3. Group categories with less than 0.1% into \"Other\"\nsmall_portions = percentage[percentage < 1].index\nvalue_counts.loc['Other'] = value_counts[small_portions].sum()\nvalue_counts = value_counts.drop(small_portions)\n\n# 4. Create the pie chart\nplt.figure(figsize=(10, 8))\nplt.pie(value_counts.values, labels=value_counts.index, autopct='%1.1f%%', startangle=180)\nplt.title(f'Distribution of {column_name}')\nplt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle\n\n# Display the chart\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:38.595934Z","iopub.execute_input":"2024-08-23T03:31:38.596705Z","iopub.status.idle":"2024-08-23T03:31:38.931982Z","shell.execute_reply.started":"2024-08-23T03:31:38.596662Z","shell.execute_reply":"2024-08-23T03:31:38.930057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ thể hiện tỷ lệ sản phẩm dựa trên màu sắc**","metadata":{}},{"cell_type":"code","source":"# Assuming 'articles' is your DataFrame\ncolumn_name = 'colour_group_name'\n\n# 1. Count the values in the specified column\nvalue_counts = articles[column_name].value_counts()\n\n# 2. Calculate the total number of entries and the percentage for each category\ntotal_count = value_counts.sum()\npercentage = (value_counts / total_count) * 100\n\n# 3. Group categories with less than 0.1% into \"Other\"\nsmall_portions = percentage[percentage < 1].index\nvalue_counts.loc['Other'] = value_counts[small_portions].sum()\nvalue_counts = value_counts.drop(small_portions)\n\n# 4. Create the pie chart\nplt.figure(figsize=(10, 8))\nplt.pie(value_counts.values, labels=value_counts.index, autopct='%1.1f%%', startangle=180)\nplt.title(f'Distribution of {column_name}')\nplt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle\n\n# Display the chart\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:38.934209Z","iopub.execute_input":"2024-08-23T03:31:38.935017Z","iopub.status.idle":"2024-08-23T03:31:39.411921Z","shell.execute_reply.started":"2024-08-23T03:31:38.934971Z","shell.execute_reply":"2024-08-23T03:31:39.410837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ thể hiện tỷ lệ sản phẩm dựa trên loại vải**","metadata":{}},{"cell_type":"code","source":"# Assuming 'articles' is your DataFrame\ncolumn_name = 'graphical_appearance_name'\n\n# 1. Count the values in the specified column\nvalue_counts = articles[column_name].value_counts()\n\n# 2. Calculate the total number of entries and the percentage for each category\ntotal_count = value_counts.sum()\npercentage = (value_counts / total_count) * 100\n\n# 3. Group categories with less than 0.1% into \"Other\"\nsmall_portions = percentage[percentage < 1].index\nvalue_counts.loc['Other'] = value_counts[small_portions].sum()\nvalue_counts = value_counts.drop(small_portions)\n\n# 4. Create the pie chart\nplt.figure(figsize=(10, 8))\nplt.pie(value_counts.values, labels=value_counts.index, autopct='%1.1f%%', startangle=180)\nplt.title(f'Distribution of {column_name}')\nplt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle\n\n# Display the chart\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:39.413321Z","iopub.execute_input":"2024-08-23T03:31:39.413731Z","iopub.status.idle":"2024-08-23T03:31:39.786964Z","shell.execute_reply.started":"2024-08-23T03:31:39.413702Z","shell.execute_reply":"2024-08-23T03:31:39.785651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ thể hiện tỷ lệ sản phẩm dựa trên các loại sản phẩm**","metadata":{}},{"cell_type":"code","source":"\n# Assuming 'articles' is your DataFrame\ncolumn_name = 'index_name'\n\n# 1. Count the value in the specified column\nvalue_counts = articles[column_name].value_counts()\n\n# 2. Calculate the total number of entries and the percentage for each category\ntotal_count = value_counts.sum()\npercentage = (value_counts / total_count) * 100\n\n# 3. Group categories with less than 0.1% into \"Other\"\nsmall_portions = percentage[percentage < 1].index\nvalue_counts.loc['Other'] = value_counts[small_portions].sum()\nvalue_counts = value_counts.drop(small_portions)\n\n# 4. Create the pie chart\nplt.figure(figsize=(10, 8))\nplt.pie(value_counts.values, labels=value_counts.index, autopct='%1.1f%%', startangle=180)\nplt.title(f'Distribution of {column_name}')\nplt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle\n\n# Display the chart\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:40.423950Z","iopub.execute_input":"2024-08-23T03:31:40.424336Z","iopub.status.idle":"2024-08-23T03:31:40.760380Z","shell.execute_reply.started":"2024-08-23T03:31:40.424299Z","shell.execute_reply":"2024-08-23T03:31:40.759224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ tỷ lệ sản phẩm dựa trên giá để sản phẩm**","metadata":{}},{"cell_type":"code","source":"\n\n# Assuming 'articles' is your DataFrame\ncolumn_name = 'garment_group_name'\n\n# 1. Count the value in the specified column\nvalue_counts = articles[column_name].value_counts()\n\n# 2. Calculate the total number of entries and the percentage for each category\ntotal_count = value_counts.sum()\npercentage = (value_counts / total_count) * 100\n\n# 3. Group categories with less than 0.1% into \"Other\"\nsmall_portions = percentage[percentage <= 1].index\nvalue_counts.loc['Other'] = value_counts[small_portions].sum()\nvalue_counts = value_counts.drop(small_portions)\n\n# 4. Create the pie chart\nplt.figure(figsize=(10, 8))\nplt.pie(value_counts.values, labels=value_counts.index, autopct='%1.1f%%', startangle=180)\nplt.title(f'Distribution of {column_name}')\nplt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle\n\n# Display the chart\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:40.761788Z","iopub.execute_input":"2024-08-23T03:31:40.762174Z","iopub.status.idle":"2024-08-23T03:31:41.235427Z","shell.execute_reply.started":"2024-08-23T03:31:40.762139Z","shell.execute_reply":"2024-08-23T03:31:41.233970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Customer X Transactions","metadata":{}},{"cell_type":"code","source":"merged_df = pd.merge(trans, customer, on='customer_id')","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:31:41.237916Z","iopub.execute_input":"2024-08-23T03:31:41.238418Z","iopub.status.idle":"2024-08-23T03:32:11.780184Z","shell.execute_reply.started":"2024-08-23T03:31:41.238377Z","shell.execute_reply":"2024-08-23T03:32:11.779103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ tỷ lệ giao dịch dựa trên nhóm tuổi**","metadata":{}},{"cell_type":"code","source":"column_name = 'age_group'\n\n# Count the values in the specified column\nvalue_counts = merged_df[column_name].value_counts()\n\n# Create a pie chart\nplt.figure(figsize=(10, 8))\nplt.pie(value_counts.values, labels=value_counts.index, autopct='%1.1f%%', startangle=180)\nplt.title(f'Distribution of {column_name}')\nplt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle\n\n# Display the chart\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:32:11.781719Z","iopub.execute_input":"2024-08-23T03:32:11.782085Z","iopub.status.idle":"2024-08-23T03:32:12.252297Z","shell.execute_reply.started":"2024-08-23T03:32:11.782056Z","shell.execute_reply":"2024-08-23T03:32:12.250985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ tỷ lệ giao dịch dựa trên Frequency của customer**","metadata":{}},{"cell_type":"code","source":"column_name = 'fashion_news_frequency'\n\n# Count the values in the specified column\nvalue_counts = merged_df[column_name].value_counts()\n\n# Create a pie chart\nplt.figure(figsize=(10, 8))\nplt.pie(value_counts.values, labels=value_counts.index, autopct='%1.1f%%', startangle=180)\nplt.title(f'Distribution of {column_name}')\nplt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle\n\n# Display the chart\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:32:12.254047Z","iopub.execute_input":"2024-08-23T03:32:12.254446Z","iopub.status.idle":"2024-08-23T03:32:18.136161Z","shell.execute_reply.started":"2024-08-23T03:32:12.254416Z","shell.execute_reply":"2024-08-23T03:32:18.135087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ tỷ lệ giao dịch dựa trên Status của customer**","metadata":{}},{"cell_type":"code","source":"column_name = 'club_member_status'\n\n# Count the values in the specified column\nvalue_counts = merged_df[column_name].value_counts()\n\n# Create a pie chart\nplt.figure(figsize=(10, 8))\nplt.pie(value_counts.values, labels=value_counts.index, autopct='%1.1f%%', startangle=180)\nplt.title(f'Distribution of {column_name}')\nplt.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle\n\n# Display the chart\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:32:18.137530Z","iopub.execute_input":"2024-08-23T03:32:18.137889Z","iopub.status.idle":"2024-08-23T03:32:23.952265Z","shell.execute_reply.started":"2024-08-23T03:32:18.137859Z","shell.execute_reply":"2024-08-23T03:32:23.951109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ thể hiện tổng giá trị giao dịch dựa trên nhóm tuổi theo từng tháng**","metadata":{}},{"cell_type":"code","source":"merged_df['t_dat'] = pd.to_datetime(merged_df['t_dat'])\n\n# 2. Extract the month and year from the time column\nmerged_df['month'] = merged_df['t_dat'].dt.to_period('M')\n\n# 3. Group by 'month' and 'customer_type' and calculate the average price\ngrouped = merged_df.groupby(['month', 'age_group'])['price'].sum().unstack()\n\n# 4. Pivot the dataframe so that customer types become columns\n\n\n# 5. Plot the stacked bar chart\ngrouped.plot(kind='bar', figsize=(10, 6))\n\n# 6. Customize the plot\nplt.title('Total spending per Customer age per Month')\nplt.ylabel('Total spending')\nplt.xlabel('Month')\nplt.xticks(rotation=45)\nplt.legend(title='Age group')\nplt.tight_layout()\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:32:23.953856Z","iopub.execute_input":"2024-08-23T03:32:23.954264Z","iopub.status.idle":"2024-08-23T03:32:34.406290Z","shell.execute_reply.started":"2024-08-23T03:32:23.954225Z","shell.execute_reply":"2024-08-23T03:32:34.405163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ thể hiện tổng giá tra giao dịch dựa trên Status của khách hàng dựa trên từng tháng**","metadata":{}},{"cell_type":"code","source":"merged_df['t_dat'] = pd.to_datetime(merged_df['t_dat'])\n\n# 2. Extract the month and year from the time column\nmerged_df['month'] = merged_df['t_dat'].dt.to_period('M')\n\n# 3. Group by 'month' and 'customer_type' and calculate the average price\ngrouped = merged_df.groupby(['month', 'club_member_status'])['price'].sum().unstack()\n\n# 4. Pivot the dataframe so that customer types become columns\n\n\n# 5. Plot the stacked bar chart\ngrouped.plot(kind='bar', figsize=(10, 6))\n\n# 6. Customize the plot\nplt.title('Total spending per Customer Status per Month')\nplt.ylabel('Total spending')\nplt.xlabel('Month')\nplt.xticks(rotation=45)\nplt.legend(title='Customer Status')\nplt.tight_layout()\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:32:34.408062Z","iopub.execute_input":"2024-08-23T03:32:34.408508Z","iopub.status.idle":"2024-08-23T03:32:45.536801Z","shell.execute_reply.started":"2024-08-23T03:32:34.408457Z","shell.execute_reply":"2024-08-23T03:32:45.535630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ thể hiện tổng giá tra giao dịch dựa trên Frequency của khách hàng dựa trên từng tháng**","metadata":{}},{"cell_type":"code","source":"merged_df['t_dat'] = pd.to_datetime(merged_df['t_dat'])\n\n# 2. Extract the month and year from the time column\nmerged_df['month'] = merged_df['t_dat'].dt.to_period('M')\n\n# 3. Group by 'month' and 'customer_type' and calculate the average price\ngrouped = merged_df.groupby(['month', 'fashion_news_frequency'])['price'].sum().unstack()\n\n# 4. Pivot the dataframe so that customer types become columns\n\n\n# 5. Plot the stacked bar chart\ngrouped.plot(kind='bar', figsize=(10, 6))\n\n# 6. Customize the plot\nplt.title('Total spending per Fashion News Frequency per Month')\nplt.ylabel('Total spending')\nplt.xlabel('Month')\nplt.xticks(rotation=45)\nplt.legend(title='Customer News Frequency')\nplt.tight_layout()\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:32:45.538390Z","iopub.execute_input":"2024-08-23T03:32:45.538741Z","iopub.status.idle":"2024-08-23T03:32:56.802689Z","shell.execute_reply.started":"2024-08-23T03:32:45.538709Z","shell.execute_reply":"2024-08-23T03:32:56.801597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"# Article X Trans","metadata":{}},{"cell_type":"code","source":"merged_df = pd.merge(trans, articles, on='article_id')","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:32:56.804215Z","iopub.execute_input":"2024-08-23T03:32:56.804636Z","iopub.status.idle":"2024-08-23T03:33:22.239163Z","shell.execute_reply.started":"2024-08-23T03:32:56.804596Z","shell.execute_reply":"2024-08-23T03:33:22.237831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ thể hiện top 5 nhóm sản phẩm mang lại giá trị giao dịch cao theo từng tháng**","metadata":{}},{"cell_type":"code","source":"merged_df['t_dat'] = pd.to_datetime(merged_df['t_dat'])\n\n# 2. Extract the month and year from the time column\nmerged_df['month'] = merged_df['t_dat'].dt.to_period('M')\n\n# 3. Group by 'month' and 'customer_type' and calculate the average price\ngrouped = merged_df.groupby(['month', 'product_group_name'])['price'].sum().reset_index()\n\ntop_5_per_month = grouped.groupby('month').apply(lambda x: x.nlargest(5, 'price')).reset_index(drop=True)\n\n# 5. Pivot the dataframe so that product groups become columns\npivot_df = top_5_per_month.pivot(index='month', columns='product_group_name', values='price')\n\n# 6. Plot the stacked bar chart\npivot_df.plot(kind='bar', stacked=True, figsize=(10, 6))\n\n# 6. Customize the plot\nplt.title('Top 5 total price per Product group per Month')\nplt.ylabel('Total Price')\nplt.xlabel('Month')\nplt.xticks(rotation=45)\nplt.legend(title='Product group')\nplt.tight_layout()\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:33:22.240792Z","iopub.execute_input":"2024-08-23T03:33:22.241160Z","iopub.status.idle":"2024-08-23T03:33:36.926937Z","shell.execute_reply.started":"2024-08-23T03:33:22.241122Z","shell.execute_reply":"2024-08-23T03:33:36.925812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ thể hiện top 5 nhóm màu mang lại giá trị cao theo từng tháng**","metadata":{}},{"cell_type":"code","source":"column_name = 'colour_group_name'\ngrouped = merged_df.groupby(['month', column_name])['price'].sum().reset_index()\n\ntop_5_per_month = grouped.groupby('month').apply(lambda x: x.nlargest(5, 'price')).reset_index(drop=True)\n\n# 5. Pivot the dataframe so that product groups become columns\npivot_df = top_5_per_month.pivot(index='month', columns=column_name, values='price')\n\n# 6. Plot the stacked bar chart\npivot_df.plot(kind='bar', stacked=True, figsize=(10, 6))\n\n# 6. Customize the plot\nplt.title('Top 5 total price per Colour group per Month')\nplt.ylabel('Total Price')\nplt.xlabel('Month')\nplt.xticks(rotation=45)\nplt.legend(title='Colour group')\nplt.tight_layout()\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:33:36.928311Z","iopub.execute_input":"2024-08-23T03:33:36.928640Z","iopub.status.idle":"2024-08-23T03:33:43.345564Z","shell.execute_reply.started":"2024-08-23T03:33:36.928612Z","shell.execute_reply":"2024-08-23T03:33:43.344416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ thể hiện top 5 loại vải mang lại giá trị giao dịch cao nhất theo từng tháng**","metadata":{}},{"cell_type":"code","source":"column_name = 'graphical_appearance_name'\ngrouped = merged_df.groupby(['month', column_name])['price'].sum().reset_index()\n\ntop_5_per_month = grouped.groupby('month').apply(lambda x: x.nlargest(5, 'price')).reset_index(drop=True)\n\n# 5. Pivot the dataframe so that product groups become columns\npivot_df = top_5_per_month.pivot(index='month', columns=column_name, values='price')\n\n# 6. Plot the stacked bar chart\npivot_df.plot(kind='bar', stacked=True, figsize=(10, 6))\n\n# 6. Customize the plot\nplt.title('Top 5 total price per Graphical appearance per Month')\nplt.ylabel('Total Price')\nplt.xlabel('Month')\nplt.xticks(rotation=45)\nplt.legend(title='Graphical appearance')\nplt.tight_layout()\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:33:43.346997Z","iopub.execute_input":"2024-08-23T03:33:43.347351Z","iopub.status.idle":"2024-08-23T03:33:49.737378Z","shell.execute_reply.started":"2024-08-23T03:33:43.347314Z","shell.execute_reply":"2024-08-23T03:33:49.736191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ thể hiện top 5 loại sản phẩm dựa trên các loại sản phẩm**","metadata":{}},{"cell_type":"code","source":"column_name = 'index_name'\ngrouped = merged_df.groupby(['month', column_name])['price'].sum().reset_index()\n\ntop_5_per_month = grouped.groupby('month').apply(lambda x: x.nlargest(5, 'price')).reset_index(drop=True)\n\n# 5. Pivot the dataframe so that product groups become columns\npivot_df = top_5_per_month.pivot(index='month', columns=column_name, values='price')\n\n# 6. Plot the stacked bar chart\npivot_df.plot(kind='bar', stacked=True, figsize=(10, 6))\n\n# 6. Customize the plot\nplt.title('Top 5 total price per Index per Month')\nplt.ylabel('Total Price')\nplt.xlabel('Month')\nplt.xticks(rotation=45)\nplt.legend(title='Index')\nplt.tight_layout()\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:33:56.382592Z","iopub.execute_input":"2024-08-23T03:33:56.382942Z","iopub.status.idle":"2024-08-23T03:34:02.715857Z","shell.execute_reply.started":"2024-08-23T03:33:56.382913Z","shell.execute_reply":"2024-08-23T03:34:02.714791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Biểu đồ thể hiện top 5 giá hàng mang lại giá trị giao dịch cao nhất**","metadata":{}},{"cell_type":"code","source":"\ncolumn_name = 'garment_group_name'\ngrouped = merged_df.groupby(['month', column_name])['price'].sum().reset_index()\n\ntop_5_per_month = grouped.groupby('month').apply(lambda x: x.nlargest(5, 'price')).reset_index(drop=True)\n\n# 5. Pivot the dataframe so that product groups become columns\npivot_df = top_5_per_month.pivot(index='month', columns=column_name, values='price')\n\n# 6. Plot the stacked bar chart\npivot_df.plot(kind='bar', stacked=True, figsize=(10, 6))\n\n# 6. Customize the plot\nplt.title('Top 5 total price per Garment group per Month')\nplt.ylabel('Total Price')\nplt.xlabel('Month')\nplt.xticks(rotation=45)\nplt.legend(title='Garment group')\nplt.tight_layout()\n\n# Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-23T03:34:02.717284Z","iopub.execute_input":"2024-08-23T03:34:02.717638Z","iopub.status.idle":"2024-08-23T03:34:09.199678Z","shell.execute_reply.started":"2024-08-23T03:34:02.717608Z","shell.execute_reply":"2024-08-23T03:34:09.198532Z"},"trusted":true},"execution_count":null,"outputs":[]}]}