{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":7391,"databundleVersionId":44328,"sourceType":"competition"}],"dockerImageVersionId":30839,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Problem\n### Objective : predict unit_sales, given the following variables:  'date', 'store_nbr', 'item_nbr', 'onpromotion'\nAccurately predict the sales volume of goods, avoid overstocking or understocking, and improve customer satisfaction.\n\n\n3. How will the solution be used? \n\nAssumption 1: With Human Intervention the machine learning model’s forecasts may be examined by humans (e.g., inventory managers or planners), who adjust or validate the predictions based on their expertise before translating them into actual supply quantities.\n\nAssumption 2: The predictions may flow into another model or system that has additional constraints (e.g., logistics, storage capacity, or budget limitations) before finalizing the quantities to supply.\n\n4. Framing the problem: online learning?\n    Supervised \n    Online learning:\n\n5. Is human expertise available?\n\n6. Assumptions (verify if possible)","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\npaths = []\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        path = os.path.join(dirname, filename)\n        print(path)\n        paths.append(path)\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"hello\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install py7zr","metadata":{"trusted":true,"_kg_hide-input":true,"_kg_hide-output":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import py7zr\nfor filenames in paths:\n    with py7zr.SevenZipFile(filenames, mode='r') as z_ref:\n        z_ref.extractall(path='/kaggle/working')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/working/train.csv\")\nstores = pd.read_csv(\"/kaggle/working/stores.csv\")\nitems = pd.read_csv(\"/kaggle/working/items.csv\")\nholiday_events = pd.read_csv(\"/kaggle/working/holidays_events.csv\")\ntransactions = pd.read_csv(\"/kaggle/working/transactions.csv\")\noil = pd.read_csv(\"/kaggle/working/oil.csv\")\ntest = pd.read_csv('/kaggle/working/test.csv')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Exploratory Data Analysis \n\n","metadata":{}},{"cell_type":"code","source":"print(\"train: \",train.columns)\nprint(\"stores: \",stores.columns)\nprint(\"items: \",items.columns)\nprint(\"holidays_events: \",holiday_events.columns)\nprint(\"transactions: \",transactions.columns)\nprint(\"oil: \",oil.columns)\nprint(\"test: \", test.columns)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Available data\n\n* stores:  contains the location of the stores. fields: store_nbr', 'city', 'state', 'type', 'cluster'\n* items : product information, fields: item_nbr', 'family', 'class', 'perishable'\n* holidays_events: 'date', 'type', 'locale', 'locale_name', 'description', 'transferred'\n* transactions:  'date', 'store_nbr', 'transactions'\n* oil: 'date', 'dcoilwtico' : Ecuador is an oil-dependent country and it's economical health is highly vulnerable to shocks in oil prices.\n  \n* train","metadata":{}},{"cell_type":"code","source":"train","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\n\n# Convert date column to datetime\ntrain[\"date\"] = pd.to_datetime(train[\"date\"])\n\n# Aggregate unit sales by date\ndaily_sales = train.groupby(\"date\")[\"unit_sales\"].sum().reset_index()\n\n# Plot line graph\nplt.figure(figsize=(12, 6))\nplt.plot(daily_sales[\"date\"], daily_sales[\"unit_sales\"], label=\"Total Daily Sales\", color=\"blue\")\n\n# Formatting\nplt.xlabel(\"Date\")\nplt.ylabel(\"Total Sales\")\nplt.title(\"Daily Sales Over Time\")\nplt.xticks(rotation=45)\nplt.legend()\nplt.grid(True, linestyle=\"--\", alpha=0.7)\n\nplt.show()\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stats = []\nfor col in train.columns:  # Iterate over each column in the DataFrame\n    stats.append((\n        col,  # Column name\n        train[col].nunique(),  # Count of unique values in the column\n        round(train[col].isnull().sum() * 100 / train.shape[0], 3),  # Percentage of missing values\n        round(train[col].value_counts(normalize=True, dropna=False).values[0] * 100, 3),  # Frequency of the most common value (as %)\n        train[col].dtype  # Data type of the column\n    ))","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stats_df = pd.DataFrame(stats, columns=[\n    'features ',  # Column name\n    'the number of unique attributes ',  # Number of unique values in the column\n    'the proportion of missing values ',  # Percentage of missing values\n    'the proportion of maximum attributes ',  # Frequency of the most common value\n    'feature type '  # Data type of the column\n])\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stats_df.drop(columns = ['the proportion of maximum attributes '])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stores","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stores['type'].unique()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"## cluster is a grouping of similar stores.\nstores['cluster'].unique()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"perishable_counts = stores['cluster'].value_counts()\n\n# Plotting the bar graph\nplt.figure(figsize=(6, 4))\nperishable_counts.plot(kind='bar', color='green')\nplt.title('Count of Clusters')\nplt.xlabel('Clusters')\nplt.ylabel('Count')\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"hello\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n# Count the number of stores in each city-state combination\n\ncity_state_counts = stores.groupby([\"state\", \"city\"]).size().unstack()\n\n# Plot stacked bar chart with states on X-axis\ncity_state_counts.plot(kind=\"bar\", stacked=True, figsize=(9, 6), colormap=\"tab20\")\n\nplt.xlabel(\"State\")\nplt.ylabel(\"Number of Stores\")\nplt.title(\"Distribution of Stores by State and City\")\nplt.legend(title=\"City\", bbox_to_anchor=(1.05, 1), loc=\"upper left\")\nplt.xticks(rotation=45, ha=\"right\")\n\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stores_stats = []\nfor col in stores.columns:  # Iterate over each column in the DataFrame\n    stores_stats.append((\n        col,  # Column name\n        stores[col].nunique(),  # Count of unique values in the column\n        round(stores[col].isnull().sum() * 100 / train.shape[0], 3),  # Percentage of missing values\n        round(stores[col].value_counts(normalize=True, dropna=False).values[0] * 100, 3),  # Frequency of the most common value (as %)\n        stores[col].dtype  # Data type of the column\n    ))\n\nstores_stats_df = pd.DataFrame(stores_stats, columns=[\n    'features ',  # Column name\n    'the number of unique attributes ',  # Number of unique values in the column\n    'the proportion of missing values ',  # Percentage of missing values\n    'the proportion of maximum attributes ',  # Frequency of the most common value\n    'feature type '  # Data type of the column\n])\n\nstores_stats_df.drop(columns = ['the proportion of maximum attributes '])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"oil[\"date\"] = pd.to_datetime(oil[\"date\"])\n\n# Drop rows with NaN values\noil_ = oil.dropna()\n\n# Plot bar graph\nplt.figure(figsize=(12, 6))\nplt.plot(oil_[\"date\"], oil_[\"dcoilwtico\"],label=\"Oil Price\")\n\n# Formatting\nplt.xlabel(\"Date\")\nplt.ylabel(\"Oil Price (USD)\")\nplt.title(\"Oil Price Over Time\")\nplt.xticks(rotation=45)\nplt.legend()\nplt.grid(True, linestyle=\"--\", alpha=0.7)\n\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transactions","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"transactions[\"date\"] = pd.to_datetime(transactions[\"date\"])\n\n\n# Aggregate transactions by date\ndaily_transactions = transactions.groupby(\"date\")[\"transactions\"].sum().reset_index()\n\n# Plot line graph\nplt.figure(figsize=(12, 6))\nplt.plot(daily_transactions[\"date\"], daily_transactions[\"transactions\"], label=\"Total Transactions\", color=\"blue\")\n\n# Formatting\nplt.xlabel(\"Date\")\nplt.ylabel(\"Total Transactions\")\nplt.title(\"Daily Transactions Over Time\")\nplt.xticks(rotation=45)\nplt.legend()\nplt.grid(True, linestyle=\"--\", alpha=0.7)\n\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"daily_transactions ","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"holiday_events","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(train['store_nbr'].unique())","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"oil","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"items","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"items['family'].unique()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tems_stats = []\nfor col in items.columns:  # Iterate over each column in the DataFrame\n    tems_stats.append((\n        col,  # Column name\n        items[col].nunique(),  # Count of unique values in the column\n        round(items[col].isnull().sum() * 100 / train.shape[0], 3),  # Percentage of missing values\n        round(items[col].value_counts(normalize=True, dropna=False).values[0] * 100, 3),  # Frequency of the most common value (as %)\n        items[col].dtype  # Data type of the column\n    ))\n\nitems_stats_df = pd.DataFrame(tems_stats, columns=[\n    'features ',  # Column name\n    'the number of unique attributes ',  # Number of unique values in the column\n    'the proportion of missing values ',  # Percentage of missing values\n    'the proportion of maximum attributes ',  # Frequency of the most common value\n    'feature type '  # Data type of the column\n])\n\nitems_stats_df.drop(columns = ['the proportion of maximum attributes '])","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"perishable_counts = items['perishable'].value_counts()\n\n# Plotting the bar graph\nplt.figure(figsize=(6, 4))\nperishable_counts.plot(kind='bar', color=['blue', 'orange'])\nplt.title('Count of Perishable vs Non-Perishable Items')\nplt.xlabel('Perishable (0: Non-Perishable, 1: Perishable)')\nplt.ylabel('Count')\nplt.xticks(ticks=[0, 1], labels=['Non-Perishable', 'Perishable'], rotation=0)\nplt.show()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\n\n# Assuming your DataFrame is 'items'\n# Count the occurrences of each family type\nfamily_counts = items['family'].value_counts()\n\n# Generate a list of colors (one for each family)\ncolors = plt.cm.get_cmap('tab20', len(family_counts))\n\n# Plot the horizontal bar graph with different colors\nplt.figure(figsize=(10, 6))\nfamily_counts.plot(kind='barh', color=colors(range(len(family_counts))))\nplt.xlabel('Number of Items')\nplt.ylabel('Family')\nplt.title('Distribution of Items Across Families')\nplt.show()\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"holiday_events","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"holiday_events['type'].unique()","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}