{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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\nfrom datetime import datetime","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:04:36.426243Z","iopub.execute_input":"2025-08-03T14:04:36.426599Z","iopub.status.idle":"2025-08-03T14:04:37.947455Z","shell.execute_reply.started":"2025-08-03T14:04:36.426576Z","shell.execute_reply":"2025-08-03T14:04:37.946202Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sns.set_style(\"whitegrid\")\nplt.style.use(\"seaborn-v0_8-whitegrid\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:04:58.542641Z","iopub.execute_input":"2025-08-03T14:04:58.542992Z","iopub.status.idle":"2025-08-03T14:04:58.548554Z","shell.execute_reply.started":"2025-08-03T14:04:58.542968Z","shell.execute_reply":"2025-08-03T14:04:58.547389Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"try:\n    transactions_df = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/transactions_train.csv')\n    articles_df = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/articles.csv')\n    customers_df = pd.read_csv('/kaggle/input/h-and-m-personalized-fashion-recommendations/customers.csv')\n    print(\"Data loaded successfully.\")\nexcept FileNotFoundError as e:\n    print(\"Error: One or more files not found. Please check the file paths: \", e)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:34:16.980618Z","iopub.execute_input":"2025-08-03T14:34:16.980979Z","iopub.status.idle":"2025-08-03T14:35:20.486246Z","shell.execute_reply.started":"2025-08-03T14:34:16.980956Z","shell.execute_reply":"2025-08-03T14:35:20.483679Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n--- Initial Data Inspection (transactions_df) ---\")\nprint(\"Shape of the dataframe:\", transactions_df.shape)\nprint(\"\\nFirst 5 rows:\")\nprint(transactions_df.head())\nprint(\"\\nColumn information:\")\ntransactions_df.info()\n\nprint(\"\\n--- Initial Data Inspection (customers_df) ---\")\nprint(\"Shape of the dataframe:\", customers_df.shape)\nprint(\"\\nFirst 5 rows:\")\nprint(customers_df.head())\nprint(\"\\nColumn information:\")\ncustomers_df.info()\n\nprint(\"\\n--- Initial Data Inspection (articles_df) ---\")\nprint(\"Shape of the dataframe:\", articles_df.shape)\nprint(\"\\nFirst 5 rows:\")\nprint(articles_df.head())\nprint(\"\\nColumn information:\")\narticles_df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:10:04.759083Z","iopub.execute_input":"2025-08-03T14:10:04.759427Z","iopub.status.idle":"2025-08-03T14:10:05.211094Z","shell.execute_reply.started":"2025-08-03T14:10:04.759407Z","shell.execute_reply":"2025-08-03T14:10:05.210036Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transactions_df['t_dat'] = pd.to_datetime(transactions_df['t_dat'])\n\nprint(\"\\n--- Missing Value Check ---\")\nprint(\"Missing values in transactions_df:\\n\", transactions_df.isnull().sum())\nprint(\"\\nMissing values in customers_df:\\n\", customers_df.isnull().sum())\nprint(\"\\nMissing values in articles_df:\\n\", articles_df.isnull().sum())\n\ncustomers_df['club_member_status'].fillna('UNKNOWN', inplace=True)\ncustomers_df['fashion_news_frequency'].fillna('None', inplace=True)\n\ncustomers_df['age'].fillna(customers_df['age'].median(), inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:11:15.278732Z","iopub.execute_input":"2025-08-03T14:11:15.279043Z","iopub.status.idle":"2025-08-03T14:11:18.805226Z","shell.execute_reply.started":"2025-08-03T14:11:15.279021Z","shell.execute_reply":"2025-08-03T14:11:18.803899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n--- Merging Dataframes ---\")\nmerged_df = pd.merge(transactions_df, customers_df, on='customer_id', how='left')\nmerged_df = pd.merge(merged_df, articles_df, on='article_id', how='left')\n\nprint(\"Shape of the merged dataframe:\", merged_df.shape)\nprint(\"First 5 rows of the merged dataframe:\")\nprint(merged_df.head())\nprint(\"Column information of the merged dataframe:\")\nmerged_df.info()\n\n# Create new features\nmerged_df['sales_month'] = merged_df['t_dat'].dt.to_period('M')\nmerged_df['sales_dayofweek'] = merged_df['t_dat'].dt.day_name()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:15:39.992989Z","iopub.execute_input":"2025-08-03T14:15:39.994021Z","iopub.status.idle":"2025-08-03T14:16:48.607003Z","shell.execute_reply.started":"2025-08-03T14:15:39.993989Z","shell.execute_reply":"2025-08-03T14:16:48.606029Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n--- EDA on Customer Behavior & Buying Patterns ---\")\n\nprint(\"\\n--- Overall Sales Trend ---\")\nsales_by_date = merged_df.groupby('t_dat')['price'].sum()\nplt.figure(figsize=(15, 6))\nsales_by_date.plot(title='Total Sales Over Time', color='skyblue')\nplt.xlabel('Date')\nplt.ylabel('Total Sales (in millions)')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:22:38.767475Z","iopub.execute_input":"2025-08-03T14:22:38.767778Z","iopub.status.idle":"2025-08-03T14:22:39.857535Z","shell.execute_reply.started":"2025-08-03T14:22:38.767756Z","shell.execute_reply":"2025-08-03T14:22:39.856288Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n--- Top 10 Bestselling Products ---\")\ntop_articles = merged_df['article_id'].value_counts().head(10)\ntop_articles_info = pd.merge(top_articles, articles_df[['article_id', 'prod_name']], on='article_id')\nprint(top_articles_info)\n\nplt.figure(figsize=(12, 6))\nsns.barplot(x=top_articles_info['count'], y=top_articles_info['prod_name'], palette='viridis')\nplt.title('Top 10 Bestselling Products')\nplt.xlabel('Number of Transactions')\nplt.ylabel('Product Name')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:23:14.327052Z","iopub.execute_input":"2025-08-03T14:23:14.327531Z","iopub.status.idle":"2025-08-03T14:23:16.970817Z","shell.execute_reply.started":"2025-08-03T14:23:14.327500Z","shell.execute_reply":"2025-08-03T14:23:16.969595Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n--- Top 10 Bestselling Product Groups ---\")\ntop_prod_groups = merged_df['product_group_name'].value_counts().head(10)\nplt.figure(figsize=(12, 6))\nsns.barplot(x=top_prod_groups.values, y=top_prod_groups.index, palette='magma')\nplt.title('Top 10 Bestselling Product Groups')\nplt.xlabel('Number of Transactions')\nplt.ylabel('Product Group')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:24:09.697926Z","iopub.execute_input":"2025-08-03T14:24:09.698240Z","iopub.status.idle":"2025-08-03T14:24:12.360787Z","shell.execute_reply.started":"2025-08-03T14:24:09.698217Z","shell.execute_reply":"2025-08-03T14:24:12.359639Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n--- Customer Segmentation: RFM Analysis ---\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:24:37.600858Z","iopub.execute_input":"2025-08-03T14:24:37.601185Z","iopub.status.idle":"2025-08-03T14:24:37.608708Z","shell.execute_reply.started":"2025-08-03T14:24:37.601163Z","shell.execute_reply":"2025-08-03T14:24:37.607298Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"latest_date = transactions_df['t_dat'].max()\nrfm_df = transactions_df.groupby('customer_id').agg(\n    Recency=('t_dat', lambda x: (latest_date - x.max()).days),\n    Frequency=('t_dat', 'count'),\n    Monetary=('price', 'sum')\n)\nprint(\"RFM DataFrame Head:\")\nprint(rfm_df.head())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:25:06.230217Z","iopub.execute_input":"2025-08-03T14:25:06.230657Z","iopub.status.idle":"2025-08-03T14:27:14.639076Z","shell.execute_reply.started":"2025-08-03T14:25:06.230631Z","shell.execute_reply":"2025-08-03T14:27:14.638116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fig, axes = plt.subplots(1, 3, figsize=(18, 5))\nsns.histplot(rfm_df['Recency'], bins=50, kde=True, ax=axes[0])\naxes[0].set_title('Recency Distribution')\nsns.histplot(rfm_df['Frequency'], bins=50, kde=True, ax=axes[1])\naxes[1].set_title('Frequency Distribution')\nsns.histplot(rfm_df['Monetary'], bins=50, kde=True, ax=axes[2])\naxes[2].set_title('Monetary Distribution')\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:27:14.640460Z","iopub.execute_input":"2025-08-03T14:27:14.640751Z","iopub.status.idle":"2025-08-03T14:27:33.868713Z","shell.execute_reply.started":"2025-08-03T14:27:14.640721Z","shell.execute_reply":"2025-08-03T14:27:33.867563Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n--- Age and Spending Behavior ---\")\nplt.figure(figsize=(10, 6))\nsns.histplot(customers_df['age'], bins=50, kde=True, color='purple')\nplt.title('Distribution of Customer Ages')\nplt.xlabel('Age')\nplt.ylabel('Number of Customers')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:27:33.870204Z","iopub.execute_input":"2025-08-03T14:27:33.870803Z","iopub.status.idle":"2025-08-03T14:27:40.433700Z","shell.execute_reply.started":"2025-08-03T14:27:33.870778Z","shell.execute_reply":"2025-08-03T14:27:40.432373Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bins = [0, 18, 25, 35, 45, 55, 65, 100]\nlabels = ['<18', '18-24', '25-34', '35-44', '45-54', '55-64', '65+']\nmerged_df['age_group'] = pd.cut(merged_df['age'], bins=bins, labels=labels, right=False)\n\navg_spending_by_age = merged_df.groupby('age_group')['price'].mean().sort_index()\nplt.figure(figsize=(12, 6))\nsns.barplot(x=avg_spending_by_age.index, y=avg_spending_by_age.values, palette='coolwarm')\nplt.title('Average Transaction Price by Age Group')\nplt.xlabel('Age Group')\nplt.ylabel('Average Price')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:27:43.388411Z","iopub.execute_input":"2025-08-03T14:27:43.388660Z","iopub.status.idle":"2025-08-03T14:27:44.723831Z","shell.execute_reply.started":"2025-08-03T14:27:43.388639Z","shell.execute_reply":"2025-08-03T14:27:44.722790Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n--- Seasonality and Day of the Week Analysis ---\")\n# Sales by month\nsales_by_month = merged_df.groupby('sales_month')['price'].sum()\nplt.figure(figsize=(15, 6))\nsales_by_month.plot(kind='bar', title='Total Sales by Month', color='teal')\nplt.xlabel('Month')\nplt.ylabel('Total Sales')\nplt.xticks(rotation=45)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:27:45.868856Z","iopub.execute_input":"2025-08-03T14:27:45.869199Z","iopub.status.idle":"2025-08-03T14:27:47.049606Z","shell.execute_reply.started":"2025-08-03T14:27:45.869168Z","shell.execute_reply":"2025-08-03T14:27:47.048438Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"day_order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']\nsales_by_day = merged_df.groupby('sales_dayofweek')['price'].sum().reindex(day_order)\nplt.figure(figsize=(10, 6))\nsns.barplot(x=sales_by_day.index, y=sales_by_day.values, palette='pastel')\nplt.title('Total Sales by Day of the Week')\nplt.xlabel('Day of the Week')\nplt.ylabel('Total Sales')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:27:47.051361Z","iopub.execute_input":"2025-08-03T14:27:47.052114Z","iopub.status.idle":"2025-08-03T14:27:50.953651Z","shell.execute_reply.started":"2025-08-03T14:27:47.052089Z","shell.execute_reply":"2025-08-03T14:27:50.952529Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n--- Impact of Fashion News & Club Membership ---\")\nspending_by_news = merged_df.groupby('fashion_news_frequency')['price'].mean().sort_values(ascending=False)\nplt.figure(figsize=(10, 6))\nsns.barplot(x=spending_by_news.index, y=spending_by_news.values, palette='dark')\nplt.title('Average Transaction Price by Fashion News Frequency')\nplt.xlabel('Fashion News Frequency')\nplt.ylabel('Average Price')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:28:02.634224Z","iopub.execute_input":"2025-08-03T14:28:02.634712Z","iopub.status.idle":"2025-08-03T14:28:05.483312Z","shell.execute_reply.started":"2025-08-03T14:28:02.634684Z","shell.execute_reply":"2025-08-03T14:28:05.482272Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"spending_by_club = merged_df.groupby('club_member_status')['price'].mean().sort_values(ascending=False)\nplt.figure(figsize=(10, 6))\nsns.barplot(x=spending_by_club.index, y=spending_by_club.values, palette='cubehelix')\nplt.title('Average Transaction Price by Club Member Status')\nplt.xlabel('Club Member Status')\nplt.ylabel('Average Price')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:28:08.558982Z","iopub.execute_input":"2025-08-03T14:28:08.560205Z","iopub.status.idle":"2025-08-03T14:28:11.336645Z","shell.execute_reply.started":"2025-08-03T14:28:08.560163Z","shell.execute_reply":"2025-08-03T14:28:11.335555Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"\\n--- Summary of Key Findings ---\")\nprint(\"Based on the EDA, we can draw the following insights:\")\nprint(\"- The total sales show a clear trend over time, likely with seasonal peaks.\")\nprint(\"- Bestselling products and product groups can be identified, guiding inventory and marketing efforts.\")\nprint(\"- RFM analysis helps segment customers into different tiers (e.g., high-value, recent, frequent).\")\nprint(\"- Customer age has a visible impact on average spending, which can inform targeted advertising.\")\nprint(\"- Certain days of the week or months show higher sales, useful for staffing and promotion planning.\")\nprint(\"- Fashion news subscription and club membership appear to be correlated with customer spending, suggesting these are effective engagement tools.\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-03T14:28:27.744596Z","iopub.execute_input":"2025-08-03T14:28:27.744970Z","iopub.status.idle":"2025-08-03T14:28:27.752083Z","shell.execute_reply.started":"2025-08-03T14:28:27.744945Z","shell.execute_reply":"2025-08-03T14:28:27.750732Z"}},"outputs":[],"execution_count":null}]}