{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#Importing the required libraries\n\nimport numpy as np \nimport pandas as pd \nimport plotly.express as px\nfrom scipy.stats import chi2_contingency\nimport scipy.stats as stats\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport gc","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-18T08:27:17.743303Z","iopub.execute_input":"2023-12-18T08:27:17.743654Z","iopub.status.idle":"2023-12-18T08:27:20.517962Z","shell.execute_reply.started":"2023-12-18T08:27:17.743625Z","shell.execute_reply":"2023-12-18T08:27:20.516966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reading the articles data set and storing in data frame containing article information\n\ndf = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:27:20.520128Z","iopub.execute_input":"2023-12-18T08:27:20.522282Z","iopub.status.idle":"2023-12-18T08:27:21.603741Z","shell.execute_reply.started":"2023-12-18T08:27:20.522240Z","shell.execute_reply":"2023-12-18T08:27:21.601831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Displaying basic information about the DataFrame 'df' \n\ndf.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:27:21.605637Z","iopub.execute_input":"2023-12-18T08:27:21.606467Z","iopub.status.idle":"2023-12-18T08:27:21.693880Z","shell.execute_reply.started":"2023-12-18T08:27:21.606425Z","shell.execute_reply":"2023-12-18T08:27:21.692823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reading the customers data set and storing in data frame containing customer information\n\ndf_customers = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:27:21.695649Z","iopub.execute_input":"2023-12-18T08:27:21.696275Z","iopub.status.idle":"2023-12-18T08:27:26.863743Z","shell.execute_reply.started":"2023-12-18T08:27:21.696242Z","shell.execute_reply":"2023-12-18T08:27:26.862032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Displaying basic information about the DataFrame 'df_customers'\n\ndf_customers.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:27:26.865521Z","iopub.execute_input":"2023-12-18T08:27:26.865887Z","iopub.status.idle":"2023-12-18T08:27:27.076827Z","shell.execute_reply.started":"2023-12-18T08:27:26.865856Z","shell.execute_reply":"2023-12-18T08:27:27.075811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reading the transactions data set and storing in data frame containing transaction information\n\ndf_transaction_train = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:27:27.077936Z","iopub.execute_input":"2023-12-18T08:27:27.078258Z","iopub.status.idle":"2023-12-18T08:28:27.600922Z","shell.execute_reply.started":"2023-12-18T08:27:27.078231Z","shell.execute_reply":"2023-12-18T08:28:27.599631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Displaying basic information about the DataFrame 'df_transaction_train'\n\ndf_transaction_train.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:28:27.602901Z","iopub.execute_input":"2023-12-18T08:28:27.603383Z","iopub.status.idle":"2023-12-18T08:28:27.616954Z","shell.execute_reply.started":"2023-12-18T08:28:27.603336Z","shell.execute_reply":"2023-12-18T08:28:27.615176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Merging the articles and transactions data on article_id\n\nmerged_df = df_transaction_train.merge(df, on='article_id', validate='many_to_one')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:28:27.618197Z","iopub.execute_input":"2023-12-18T08:28:27.618459Z","iopub.status.idle":"2023-12-18T08:28:54.067382Z","shell.execute_reply.started":"2023-12-18T08:28:27.618437Z","shell.execute_reply":"2023-12-18T08:28:54.066012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Merging customers data with merged_df on customer_id\n\nmerged_df = merged_df.merge(df_customers,on='customer_id',validate='many_to_one')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:28:54.068919Z","iopub.execute_input":"2023-12-18T08:28:54.069348Z","iopub.status.idle":"2023-12-18T08:31:30.389531Z","shell.execute_reply.started":"2023-12-18T08:28:54.069316Z","shell.execute_reply":"2023-12-18T08:31:30.388162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Keeping only required columns for analysis\n\nselected_columns = ['t_dat','customer_id','article_id','price','sales_channel_id','product_type_name','colour_group_name','perceived_colour_value_name',\n                   'department_name','index_name','index_group_name','section_name','garment_group_name',\n                   'club_member_status','fashion_news_frequency','age','postal_code']\n\n# Select only the desired columns\nmerged_df = merged_df[selected_columns]","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:31:30.393418Z","iopub.execute_input":"2023-12-18T08:31:30.393784Z","iopub.status.idle":"2023-12-18T08:31:37.008000Z","shell.execute_reply.started":"2023-12-18T08:31:30.393759Z","shell.execute_reply":"2023-12-18T08:31:37.006564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Converting 't_dat' to datetime\nmerged_df['t_dat'] = pd.to_datetime(merged_df['t_dat'])\n\n#Converting 'price' to float\nmerged_df['price'] = merged_df['price'].astype(float)\n\n#Converting 'age' to int\nmerged_df['age'] = merged_df['age'].astype(float)\n\n#Converting all other columns to string\ncolumns_to_convert_to_string = ['customer_id', 'article_id', 'sales_channel_id', 'product_type_name', 'colour_group_name', 'perceived_colour_value_name',\n                                'department_name', 'index_name', 'index_group_name', 'section_name', 'garment_group_name', 'club_member_status',\n                                'fashion_news_frequency', 'postal_code']\n\nmerged_df[columns_to_convert_to_string] = merged_df[columns_to_convert_to_string].astype(str)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:31:37.009765Z","iopub.execute_input":"2023-12-18T08:31:37.010170Z","iopub.status.idle":"2023-12-18T08:32:22.393173Z","shell.execute_reply.started":"2023-12-18T08:31:37.010138Z","shell.execute_reply":"2023-12-18T08:32:22.391602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"column_data_types = merged_df.dtypes\n\n# Printing the columns and their data types\nprint(column_data_types)","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:32:22.394566Z","iopub.execute_input":"2023-12-18T08:32:22.394971Z","iopub.status.idle":"2023-12-18T08:32:22.402613Z","shell.execute_reply.started":"2023-12-18T08:32:22.394920Z","shell.execute_reply":"2023-12-18T08:32:22.401464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del selected_columns\ndel column_data_types\ndel columns_to_convert_to_string\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:32:22.404169Z","iopub.execute_input":"2023-12-18T08:32:22.404689Z","iopub.status.idle":"2023-12-18T08:32:22.513829Z","shell.execute_reply.started":"2023-12-18T08:32:22.404657Z","shell.execute_reply":"2023-12-18T08:32:22.512767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Describing price variable for segmentation in low, medium and high valued customer\nmerged_df['price'].describe()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:32:22.515156Z","iopub.execute_input":"2023-12-18T08:32:22.515439Z","iopub.status.idle":"2023-12-18T08:32:23.529188Z","shell.execute_reply.started":"2023-12-18T08:32:22.515412Z","shell.execute_reply":"2023-12-18T08:32:23.527734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Calculating the quartiles for \"price\" in the merged_df DataFrame\nquantiles = merged_df['price'].quantile([0.25, 0.50, 0.75])\nq1, q2, q3 = quantiles[0.25], quantiles[0.50], quantiles[0.75]\n\n#Creating bins and labels for segmentation\nbins = [-np.inf, q1, q2, np.inf]\nlabels = [\"Low-Value Customer\", \"Medium-Value Customer\", \"High-Value Customer\"]\n\n#Assigning Monetary Facet segments based on the price\nmerged_df['Monetary_Segment'] = pd.cut(merged_df['price'], bins=bins, labels=labels)\n\n#Displaying the resulting DataFrame with the Monetary Facet segments\nprint(merged_df[['customer_id', 'price', 'Monetary_Segment']])","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:32:23.530612Z","iopub.execute_input":"2023-12-18T08:32:23.531755Z","iopub.status.idle":"2023-12-18T08:32:26.647922Z","shell.execute_reply.started":"2023-12-18T08:32:23.531700Z","shell.execute_reply":"2023-12-18T08:32:26.646478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Extracting the year from the 't_dat' column\nmerged_df['year'] = merged_df['t_dat'].dt.year\n\n#Group by year and color, then count unique article IDs\ntop_colors_by_year = merged_df.groupby(['year', 'colour_group_name'])['article_id'].nunique().reset_index()\n\n#Get the top 5 colors for each year\ntop_5_colors_by_year = top_colors_by_year.groupby('year').apply(lambda x: x.nlargest(5, 'article_id')).reset_index(drop=True)\n\n#Printing the top 5 colors for each year\nfor year, group in top_5_colors_by_year.groupby('year'):\n    print(f'Most Common Colors in H&M Inventory in Year {year}:')\n    print(group[['colour_group_name', 'article_id']])\n    print()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:32:26.649645Z","iopub.execute_input":"2023-12-18T08:32:26.650809Z","iopub.status.idle":"2023-12-18T08:32:45.242157Z","shell.execute_reply.started":"2023-12-18T08:32:26.650754Z","shell.execute_reply":"2023-12-18T08:32:45.240291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating subplots for each year\nfig, axes = plt.subplots(nrows=len(top_5_colors_by_year['year'].unique()), figsize=(10, 6 * len(top_5_colors_by_year['year'].unique())))\n\n#Specifying the color\nbar_color = '#FF5733'\n\n#Iterating through each year and plot the top 5 colors\nfor i, year in enumerate(top_5_colors_by_year['year'].unique()):\n    ax = axes[i]\n    data_year = top_5_colors_by_year[top_5_colors_by_year['year'] == year]\n    \n    ax.bar(data_year['colour_group_name'], data_year['article_id'], color=bar_color)\n    ax.set_title(f'Most Common Colors in H&M Inventory in Year {year}')\n    ax.set_xlabel('Color Group')\n    ax.set_ylabel('Number of Article IDs')\n    ax.set_xticklabels(data_year['colour_group_name'], rotation=45, ha='right')\n    \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:32:45.244055Z","iopub.execute_input":"2023-12-18T08:32:45.244507Z","iopub.status.idle":"2023-12-18T08:32:45.962480Z","shell.execute_reply.started":"2023-12-18T08:32:45.244469Z","shell.execute_reply":"2023-12-18T08:32:45.961576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Group by year and category, then count unique article IDs\ntop_categories_by_year = merged_df.groupby(['year', 'product_type_name'])['article_id'].nunique().reset_index()\n\n#Get the top 5 categories for each year\ntop_5_categories_by_year = top_categories_by_year.groupby('year').apply(lambda x: x.nlargest(5, 'article_id')).reset_index(drop=True)\n\n#Printing the top 5 categories for each year\nfor year, group in top_5_categories_by_year.groupby('year'):\n    print(f'Top 5 Product Types offered by H&M in Year {year}:')\n    print(group[['product_type_name', 'article_id']])\n    print()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:32:45.963848Z","iopub.execute_input":"2023-12-18T08:32:45.964422Z","iopub.status.idle":"2023-12-18T08:33:02.415280Z","shell.execute_reply.started":"2023-12-18T08:32:45.964387Z","shell.execute_reply":"2023-12-18T08:33:02.414069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating subplots for each year\nfig, axes = plt.subplots(nrows=len(top_5_categories_by_year['year'].unique()), figsize=(10, 6 * len(top_5_categories_by_year['year'].unique())))\n\n#Specify the color\nbar_color = 'red'\n\n#Iterating through each year and plot the top 5 categories\nfor i, year in enumerate(top_5_categories_by_year['year'].unique()):\n    ax = axes[i]\n    data_year = top_5_categories_by_year[top_5_categories_by_year['year'] == year]\n    \n    ax.bar(data_year['product_type_name'], data_year['article_id'], color=bar_color)\n    ax.set_title(f'Top 5 Product Types offered by H&M in Year {year}')\n    ax.set_xlabel('Product Type Name')\n    ax.set_ylabel('Number of Article IDs')\n    ax.set_xticklabels(data_year['product_type_name'], rotation=45, ha='right')\n    \nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:33:02.416720Z","iopub.execute_input":"2023-12-18T08:33:02.417026Z","iopub.status.idle":"2023-12-18T08:33:03.045916Z","shell.execute_reply.started":"2023-12-18T08:33:02.417000Z","shell.execute_reply":"2023-12-18T08:33:03.044517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del top_colors_by_year\ndel top_5_colors_by_year\ndel top_categories_by_year\ndel top_5_categories_by_year\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:33:03.047699Z","iopub.execute_input":"2023-12-18T08:33:03.048692Z","iopub.status.idle":"2023-12-18T08:33:03.156308Z","shell.execute_reply.started":"2023-12-18T08:33:03.048652Z","shell.execute_reply":"2023-12-18T08:33:03.154506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Grouping the data by year and calculating the total unique customers for each year\nunique_customers_yoy = merged_df.groupby('year')['customer_id'].nunique().reset_index()\n\n#Calculating the YoY change in the number of unique customers\nunique_customers_yoy['yoy_change'] = unique_customers_yoy['customer_id'].pct_change()\n# Create a figure with subplots for the bar chart\nfig, ax1 = plt.subplots(1, 1, figsize=(5, 6))\n\n#Bar chart for total unique customers\nsns.barplot(data=unique_customers_yoy, x='year', y='customer_id', ax=ax1, color='red')\nax1.set_title('Total Unique Customers (YoY)')\nax1.set_xlabel('Year')\nax1.set_ylabel('Total Unique Customers')\n\n#Adding numbers on top of bars\nfor p in ax1.patches:\n    ax1.annotate(format(p.get_height(), '.0f'), \n                   (p.get_x() + p.get_width() / 2., p.get_height()), \n                   ha = 'center', va = 'center', \n                   xytext = (0, 9), \n                   textcoords = 'offset points')\n\n#Format the y-axis labels as regular numbers\nax1.get_yaxis().get_major_formatter().set_scientific(False)\n\n#Show the plot\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:33:03.157749Z","iopub.execute_input":"2023-12-18T08:33:03.158069Z","iopub.status.idle":"2023-12-18T08:33:14.173869Z","shell.execute_reply.started":"2023-12-18T08:33:03.158036Z","shell.execute_reply":"2023-12-18T08:33:14.172183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_customers_yoy","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:33:14.175748Z","iopub.execute_input":"2023-12-18T08:33:14.177028Z","iopub.status.idle":"2023-12-18T08:33:14.190996Z","shell.execute_reply.started":"2023-12-18T08:33:14.176984Z","shell.execute_reply":"2023-12-18T08:33:14.190036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Sorting the DataFrame by 'year' and 'customer_id'\nmerged_df.sort_values(by=['year', 'customer_id'], inplace=True)\n\n#Initializing variables to keep track of previous year's customers\nprevious_year_customers = set()\n\n#Creating lists to store the results for plotting\nyears = []\nnew_customers_count = []\nlost_customers_count = []\ntotal_customers_count = []\n\n#Iterating through each year\nfor year in merged_df['year'].unique():\n    current_year_customers = set(merged_df[merged_df['year'] == year]['customer_id'])\n    \n    #Calculating new customers (present in the current year but not in the previous year)\n    new_customers = current_year_customers - previous_year_customers\n    \n    #Calculating lost customers (present in the previous year but not in the current year)\n    lost_customers = previous_year_customers - current_year_customers\n    \n    #Updating previous_year_customers for the next iteration\n    previous_year_customers = current_year_customers\n    \n    #Storing the results for plotting\n    years.append(year)\n    new_customers_count.append(len(new_customers))\n    lost_customers_count.append(len(lost_customers))\n    \n    #Calculating the total customers for the current year\n    total_customers_count.append(len(current_year_customers))\n\n#Creating a DataFrame for plotting\nplot_data = pd.DataFrame({'Year': years, 'New Customers': new_customers_count, 'Lost Customers': lost_customers_count, 'Total Customers': total_customers_count})\n\n#Creating a grouped bar chart using Matplotlib\nplt.figure(figsize=(6, 3))\nwidth = 0.2  # Width of each bar\nx = range(len(years))\n\n#Solid red for total customers\nplt.bar(x, total_customers_count, width, label='Total Customers', color='red')\n\n#Light red for new customers\nplt.bar([i + width for i in x], new_customers_count, width, label='New Customers', color='lightcoral')\n\n#Lightest red for lost customers\nplt.bar([i + width * 2 for i in x], lost_customers_count, width, label='Lost Customers', color='mistyrose')\n\nplt.xlabel('Year')\nplt.ylabel('Count')\nplt.title('New, Lost, and Total Customers Over the Years')\nplt.xticks([i + width for i in x], years)  #Setting x-axis labels to years\nplt.ticklabel_format(style='plain', axis='y')\nplt.legend()\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:33:14.192972Z","iopub.execute_input":"2023-12-18T08:33:14.193411Z","iopub.status.idle":"2023-12-18T08:34:07.955582Z","shell.execute_reply.started":"2023-12-18T08:33:14.193378Z","shell.execute_reply":"2023-12-18T08:34:07.953869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_data","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:34:07.957318Z","iopub.execute_input":"2023-12-18T08:34:07.957689Z","iopub.status.idle":"2023-12-18T08:34:07.967933Z","shell.execute_reply.started":"2023-12-18T08:34:07.957660Z","shell.execute_reply":"2023-12-18T08:34:07.966794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del unique_customers_yoy\ndel plot_data\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:34:07.969492Z","iopub.execute_input":"2023-12-18T08:34:07.969892Z","iopub.status.idle":"2023-12-18T08:34:08.176001Z","shell.execute_reply.started":"2023-12-18T08:34:07.969856Z","shell.execute_reply":"2023-12-18T08:34:08.174344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Initializing a list to store the top 5 products for each year\ntop_5_products_yoy = []\n\n#Iterating through each year and perform analysis\nfor year in merged_df['year'].unique():\n    #Filtering data for the current year\n    data_year = merged_df[merged_df['year'] == year]\n\n    #Grouping by product and calculate total sales or transaction count\n    top_products = data_year.groupby('product_type_name').size().reset_index(name='transaction_count')\n    \n    #Selecting the top 5 selling products for the current year\n    top_5_products = top_products.nlargest(5, 'transaction_count')\n\n    #Appending the results to the list\n    top_5_products_yoy.append((year, top_5_products))\n\n#Creating a custom red color palette\nred_palette = ['#FF5733', '#FF794E', '#FF9E72', '#FFBFA1', '#FFDAC9']\n\n#Creating a grouped bar chart for the top 5 products by year\nfig, ax = plt.subplots(figsize=(12, 8))\n\n#Preparing data for plotting\nyears = [year for year, _ in top_5_products_yoy]\nproduct_names = [list(products['product_type_name']) for _, products in top_5_products_yoy]\ntransaction_counts = [list(products['transaction_count']) for _, products in top_5_products_yoy]\n\n#Setting bar width and spacing\nbar_width = 0.18  # Wider bar width\nspacing = 0   # Reduced spacing\n\nn = len(years)\nbar_positions = [j - (bar_width + spacing) * 2 for j in range(n)]  # Adjusted bar positions\n\n#Creating bars for each product using the custom red color palette\nfor i, product_name in enumerate(product_names[0]):\n    x = [j + i * (bar_width + spacing) for j in bar_positions]\n    y = [counts[i] for counts in transaction_counts]\n    ax.bar(x, y, width=bar_width, label=f'{product_name}', alpha=0.7, color=red_palette[i])\n\n#Setting x-axis ticks and labels\nax.set_xticks([j + (n / 2) * (bar_width + spacing) for j in bar_positions])\nax.set_xticklabels(years)\nax.set_xlabel('Year')\nax.set_ylabel('Transaction Count')\nax.set_title('Top 5 Selling Products Year-over-Year')\nax.legend()\n\n#Formatting y-axis labels as integers (no scientific notation)\nax.get_yaxis().get_major_formatter().set_scientific(False)\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:34:08.178596Z","iopub.execute_input":"2023-12-18T08:34:08.179006Z","iopub.status.idle":"2023-12-18T08:34:22.019565Z","shell.execute_reply.started":"2023-12-18T08:34:08.178976Z","shell.execute_reply":"2023-12-18T08:34:22.018424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del top_5_products_yoy\ndel data_year\ndel top_products\ndel top_5_products\ndel fig\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:34:22.020983Z","iopub.execute_input":"2023-12-18T08:34:22.021380Z","iopub.status.idle":"2023-12-18T08:34:23.027954Z","shell.execute_reply.started":"2023-12-18T08:34:22.021349Z","shell.execute_reply":"2023-12-18T08:34:23.026877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Grouping data by 'Monetary_Segment' and 'Year' and count unique customers\nsegment_year_counts = merged_df.groupby(['Monetary_Segment', merged_df['t_dat'].dt.year])['customer_id'].nunique().reset_index()\n\n#Calculating the YoY change\nsegment_year_counts['YoY Change'] = segment_year_counts.groupby('Monetary_Segment')['customer_id'].pct_change() * 100\n\nsegment_year_counts = segment_year_counts.rename(columns={'t_dat': 'year', 'customer_id': 'No. of customers'})\n\nsegment_year_counts\n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:34:23.033780Z","iopub.execute_input":"2023-12-18T08:34:23.034149Z","iopub.status.idle":"2023-12-18T08:34:34.280782Z","shell.execute_reply.started":"2023-12-18T08:34:23.034117Z","shell.execute_reply":"2023-12-18T08:34:34.279421Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Grouping the data by year and sales channel\nsales_by_year_channel = merged_df.groupby(['year', 'sales_channel_id'])['price'].sum().reset_index()\n\n#Defining a custom red color palette\nred_palette = ['#FF5733', '#FFDAC9']\n\n#Creating a Seaborn bar plot with the custom red color palette\nplt.figure(figsize=(12, 8))\nsns.set_palette(red_palette)  # Set the color palette\nsns.barplot(data=sales_by_year_channel, x='year', y='price', hue='sales_channel_id')\nplt.title('Sales Value for Each Year through Channels')\nplt.xlabel('Year')\nplt.ylabel('Sales Value')\nplt.legend(title='Channel')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:34:34.282378Z","iopub.execute_input":"2023-12-18T08:34:34.282703Z","iopub.status.idle":"2023-12-18T08:34:39.752679Z","shell.execute_reply.started":"2023-12-18T08:34:34.282669Z","shell.execute_reply":"2023-12-18T08:34:39.751164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_by_year_channel","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:34:39.755308Z","iopub.execute_input":"2023-12-18T08:34:39.755721Z","iopub.status.idle":"2023-12-18T08:34:39.766868Z","shell.execute_reply.started":"2023-12-18T08:34:39.755686Z","shell.execute_reply":"2023-12-18T08:34:39.764998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_channel_preference = merged_df.groupby(['sales_channel_id', 'Monetary_Segment'])['customer_id'].count().reset_index()\n#Defining a custom red color palette\nred_palette = ['#FF5733','#FF9E72', '#FFDAC9']\n\n#Pivoting the data to prepare for the stacked bar chart\npivot_table = sales_channel_preference.pivot(index='sales_channel_id', columns='Monetary_Segment', values='customer_id')\n\n#Creating the stacked bar chart using Matplotlib\nplt.figure(figsize=(12, 8))\nax = pivot_table.plot(kind='bar', stacked=True, color=red_palette)\nplt.title('Sales Channel Preference by Customer Segment')\nplt.xlabel('Sales Channel')\nplt.ylabel('Customer Count')\nplt.xticks(rotation=0)  # Rotate x-axis labels if needed\nplt.legend(title='Customer Segment')\n\n#Format y-axis labels as integers\nax.get_yaxis().set_major_formatter(plt.FuncFormatter(lambda x, loc: \"{:,}\".format(int(x))))\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:34:39.768884Z","iopub.execute_input":"2023-12-18T08:34:39.769260Z","iopub.status.idle":"2023-12-18T08:34:46.935103Z","shell.execute_reply.started":"2023-12-18T08:34:39.769230Z","shell.execute_reply":"2023-12-18T08:34:46.934167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sales_channel_preference","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:34:46.936447Z","iopub.execute_input":"2023-12-18T08:34:46.936760Z","iopub.status.idle":"2023-12-18T08:34:46.948116Z","shell.execute_reply.started":"2023-12-18T08:34:46.936729Z","shell.execute_reply":"2023-12-18T08:34:46.946917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Grouping data by product type and sales channel and calculate the total sales value\ngrouped_data = merged_df.groupby(['index_group_name', 'sales_channel_id'])['price'].sum().reset_index()\n\n#Creating a custom red color palette\nred_palette = ['#FF5733', '#FF794E', '#FF9E72', '#FFBFA1', '#FFDAC9']\n\n#Creating sunburst chart\nfig = px.sunburst(\n    grouped_data,\n    path=['index_group_name', 'sales_channel_id'],\n    values='price',\n    title='Sales Channel Preference by Product Type',\n    color_discrete_sequence=red_palette,  # Set a custom red color palette\n    labels={'price': 'Sales Value', 'index_group_name': 'Product Type'},\n)\n\n#Customizing the layout\nfig.update_layout(\n    margin=dict(t=50, l=50, r=50, b=50),  # Set margins\n    title_font=dict(size=20),  # Adjust the title font size\n)\n\n#Displaying the sunburst chart\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:34:46.950205Z","iopub.execute_input":"2023-12-18T08:34:46.950890Z","iopub.status.idle":"2023-12-18T08:34:56.556795Z","shell.execute_reply.started":"2023-12-18T08:34:46.950859Z","shell.execute_reply":"2023-12-18T08:34:56.555161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grouped_data","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:34:56.558806Z","iopub.execute_input":"2023-12-18T08:34:56.559307Z","iopub.status.idle":"2023-12-18T08:34:56.574185Z","shell.execute_reply.started":"2023-12-18T08:34:56.559265Z","shell.execute_reply":"2023-12-18T08:34:56.573121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del segment_year_counts\ndel sales_by_year_channel\ndel sales_channel_preference\ndel grouped_data\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:34:56.575523Z","iopub.execute_input":"2023-12-18T08:34:56.575881Z","iopub.status.idle":"2023-12-18T08:34:56.799424Z","shell.execute_reply.started":"2023-12-18T08:34:56.575850Z","shell.execute_reply":"2023-12-18T08:34:56.797908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Communication Frequency Analysis\nfrequency_distribution = merged_df['fashion_news_frequency'].value_counts()\n\n#Create Customer Segments (e.g., age groups)\nmerged_df['AgeGroup'] = pd.cut(merged_df['age'], bins=[0,18, 30, 40, 50, 60, float('inf')],\n                                   labels=['<18','18-30', '30-40', '40-50', '50-60', '60+'])\n\n#Analyze Communication Frequency by Age Group\ncommunication_by_age = merged_df.groupby('AgeGroup')['fashion_news_frequency'].value_counts()\n\n#Spending Habits Analysis\nspending_by_age_group = merged_df.groupby('AgeGroup')['price'].max()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:34:56.800626Z","iopub.execute_input":"2023-12-18T08:34:56.800915Z","iopub.status.idle":"2023-12-18T08:35:01.835457Z","shell.execute_reply.started":"2023-12-18T08:34:56.800887Z","shell.execute_reply":"2023-12-18T08:35:01.833431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"communication_by_age = communication_by_age.reset_index()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:35:01.837334Z","iopub.execute_input":"2023-12-18T08:35:01.837782Z","iopub.status.idle":"2023-12-18T08:35:01.845391Z","shell.execute_reply.started":"2023-12-18T08:35:01.837742Z","shell.execute_reply":"2023-12-18T08:35:01.843901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"communication_by_age[communication_by_age['fashion_news_frequency']=='NONE']","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:35:01.846963Z","iopub.execute_input":"2023-12-18T08:35:01.847912Z","iopub.status.idle":"2023-12-18T08:35:01.874190Z","shell.execute_reply.started":"2023-12-18T08:35:01.847874Z","shell.execute_reply":"2023-12-18T08:35:01.872634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"communication_by_age[communication_by_age['fashion_news_frequency']=='Regularly']","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:35:01.875418Z","iopub.execute_input":"2023-12-18T08:35:01.875768Z","iopub.status.idle":"2023-12-18T08:35:01.894309Z","shell.execute_reply.started":"2023-12-18T08:35:01.875737Z","shell.execute_reply":"2023-12-18T08:35:01.892992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"communication_by_age[communication_by_age['fashion_news_frequency']=='Monthly']","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:35:01.895617Z","iopub.execute_input":"2023-12-18T08:35:01.896027Z","iopub.status.idle":"2023-12-18T08:35:01.914520Z","shell.execute_reply.started":"2023-12-18T08:35:01.895982Z","shell.execute_reply":"2023-12-18T08:35:01.913571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spending_by_age_group = spending_by_age_group.reset_index()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:35:01.915663Z","iopub.execute_input":"2023-12-18T08:35:01.915940Z","iopub.status.idle":"2023-12-18T08:35:01.932050Z","shell.execute_reply.started":"2023-12-18T08:35:01.915917Z","shell.execute_reply":"2023-12-18T08:35:01.929620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"spending_by_age_group","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:35:01.934030Z","iopub.execute_input":"2023-12-18T08:35:01.934452Z","iopub.status.idle":"2023-12-18T08:35:01.952033Z","shell.execute_reply.started":"2023-12-18T08:35:01.934418Z","shell.execute_reply":"2023-12-18T08:35:01.949971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"custom_color_sequence = ['#FF5733', '#FF7043', '#FF8566', '#FFA07A', '#FFC0A9']\n\n#Create the pie chart with the custom color sequence\nfig = px.pie(spending_by_age_group, names='AgeGroup', values='price', hole=0.7, title=\"Spending by Age Group\",\n             color_discrete_sequence=custom_color_sequence)\n\n#Show the donut chart\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:35:01.954583Z","iopub.execute_input":"2023-12-18T08:35:01.954999Z","iopub.status.idle":"2023-12-18T08:35:02.038916Z","shell.execute_reply.started":"2023-12-18T08:35:01.954961Z","shell.execute_reply":"2023-12-18T08:35:02.037972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del communication_by_age\ndel spending_by_age_group\ndel fig\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:35:02.040770Z","iopub.execute_input":"2023-12-18T08:35:02.042416Z","iopub.status.idle":"2023-12-18T08:35:02.260054Z","shell.execute_reply.started":"2023-12-18T08:35:02.042363Z","shell.execute_reply":"2023-12-18T08:35:02.258477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Hypothesis: Highest selling colors in each product category and is it significantly higher than others YoY basis.\n\nTesting: Conduct ANOVA to compare the perceived color values of products across different departments.","metadata":{}},{"cell_type":"code","source":"#Group the data by department, color, and year and calculate the sales or revenue\ngrouped_data = merged_df.groupby(['department_name', 'perceived_colour_value_name', 'year'])['price'].sum().reset_index()\n\n#Perform the ANOVA\nresult = stats.f_oneway(*[grouped_data[grouped_data['department_name'] == department]['price'] for department in grouped_data['department_name'].unique()])\n\n#Define significance level (alpha)\nalpha = 0.05\n\n#Print the results\nprint(\"ANOVA F-statistic:\", result.statistic)\nprint(\"P-value:\", result.pvalue)\n\n#Compare the p-value to the significance level\nif result.pvalue < alpha:\n    print(\"Reject the null hypothesis. There are significant differences in perceived color values between departments.\")\nelse:\n    print(\"Fail to reject the null hypothesis. There are no significant differences in perceived color values between departments.\")","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:35:02.262789Z","iopub.execute_input":"2023-12-18T08:35:02.263217Z","iopub.status.idle":"2023-12-18T08:35:08.334306Z","shell.execute_reply.started":"2023-12-18T08:35:02.263185Z","shell.execute_reply":"2023-12-18T08:35:08.333149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del grouped_data\ndel result\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:35:08.337468Z","iopub.execute_input":"2023-12-18T08:35:08.338243Z","iopub.status.idle":"2023-12-18T08:35:08.572624Z","shell.execute_reply.started":"2023-12-18T08:35:08.338195Z","shell.execute_reply":"2023-12-18T08:35:08.571208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Hypothesis: Customers in different age groups have significantly different purchase patterns in terms of product types.\n\nTesting: Perform chi-squared tests to examine the association between age groups and product types.","metadata":{}},{"cell_type":"code","source":"#Create a contingency table of age groups and product types\ncontingency_table = pd.crosstab(merged_df['AgeGroup'], merged_df['product_type_name'])\n\n#Perform the chi-squared test\nchi2, p, _, _ = chi2_contingency(contingency_table)\n\n#Define significance level (alpha)\nalpha = 0.05\n\n#Print the results\nprint(\"Chi-Squared Statistic:\", chi2)\nprint(\"P-value:\", p)\n\n#Compare the p-value to the significance level\nif p < alpha:\n    print(\"Reject the null hypothesis. There is a significant association between age groups and product types.\")\nelse:\n    print(\"Fail to reject the null hypothesis. There is no significant association between age groups and product types.\")","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:35:08.574795Z","iopub.execute_input":"2023-12-18T08:35:08.575266Z","iopub.status.idle":"2023-12-18T08:35:16.022240Z","shell.execute_reply.started":"2023-12-18T08:35:08.575223Z","shell.execute_reply":"2023-12-18T08:35:16.021049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del contingency_table\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:35:16.024316Z","iopub.execute_input":"2023-12-18T08:35:16.024739Z","iopub.status.idle":"2023-12-18T08:35:16.238636Z","shell.execute_reply.started":"2023-12-18T08:35:16.024704Z","shell.execute_reply":"2023-12-18T08:35:16.237566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Hypothesis: Club members (club_member_status = 'ACTIVE') have a higher customer lifetime value (CLV) compared to non-club members.\n\nTesting: Conduct a t-test or ANOVA to compare CLV between club members and non-club members.","metadata":{}},{"cell_type":"code","source":"clv_data =merged_df.groupby('customer_id')['price'].sum().reset_index()\nclv_data.rename(columns={'price': 'historical_CLV'}, inplace=True)\n\nmerged_df = merged_df.merge(clv_data, on='customer_id', how='left')","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:35:16.239837Z","iopub.execute_input":"2023-12-18T08:35:16.240155Z","iopub.status.idle":"2023-12-18T08:35:38.545057Z","shell.execute_reply.started":"2023-12-18T08:35:16.240128Z","shell.execute_reply":"2023-12-18T08:35:38.543669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Separate data into two groups: 'ACTIVE' club members and non-club members\nclub_members = merged_df[merged_df['club_member_status'] == 'ACTIVE']\nnon_club_members = merged_df[merged_df['club_member_status'] != 'ACTIVE']\n\n#Perform a t-test to compare CLV between the two groups\nt_stat, p_value = stats.ttest_ind(club_members['historical_CLV'], non_club_members['historical_CLV'])\n\n#Define significance level (alpha)\nalpha = 0.05\n\n#Print the results\nprint(\"T-Statistic:\", t_stat)\nprint(\"P-value:\", p_value)\n\n#Compare the p-value to the significance level\nif p_value < alpha:\n    print(\"Reject the null hypothesis. Club members with 'ACTIVE' status have a higher CLV.\")\nelse:\n    print(\"Fail to reject the null hypothesis. There is no significant difference in CLV.\")","metadata":{"execution":{"iopub.status.busy":"2023-12-18T08:35:38.547599Z","iopub.execute_input":"2023-12-18T08:35:38.548050Z","iopub.status.idle":"2023-12-18T08:35:52.860047Z","shell.execute_reply.started":"2023-12-18T08:35:38.548012Z","shell.execute_reply":"2023-12-18T08:35:52.858194Z"},"trusted":true},"execution_count":null,"outputs":[]}]}