{"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":"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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\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,"execution":{"iopub.status.busy":"2025-01-29T09:54:46.921942Z","iopub.execute_input":"2025-01-29T09:54:46.922216Z","iopub.status.idle":"2025-01-29T09:54:47.663819Z","shell.execute_reply.started":"2025-01-29T09:54:46.922185Z","shell.execute_reply":"2025-01-29T09:54:47.662895Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install py7zr\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T09:54:47.664880Z","iopub.execute_input":"2025-01-29T09:54:47.665443Z","iopub.status.idle":"2025-01-29T09:54:53.795962Z","shell.execute_reply.started":"2025-01-29T09:54:47.665407Z","shell.execute_reply":"2025-01-29T09:54:53.795077Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import py7zr\nfrom subprocess import check_output\n\nfor dirname, _, filenames in os.walk('/kaggle/input/favorita-grocery-sales-forecasting'):\n    for filename in filenames:\n        archive = py7zr.SevenZipFile(os.path.join(dirname, filename), mode='r')\n        archive.extractall(path=\"/kaggle/working\")\n        archive.close()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T09:54:53.796870Z","iopub.execute_input":"2025-01-29T09:54:53.797131Z","iopub.status.idle":"2025-01-29T09:56:07.921700Z","shell.execute_reply.started":"2025-01-29T09:54:53.797108Z","shell.execute_reply":"2025-01-29T09:56:07.920927Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\n# Load datasets\ndata_paths = {\n    \"train\": \"/kaggle/working/train.csv\",\n    \"test\": \"/kaggle/working/test.csv\",\n    \"stores\": \"/kaggle/working/stores.csv\",\n    \"items\": \"/kaggle/working/items.csv\",\n    \"transactions\": \"/kaggle/working/transactions.csv\",\n    \"holidays\": \"/kaggle/working/holidays_events.csv\",\n    \"oil\": \"/kaggle/working/oil.csv\"\n}\n\n# Load all datasets into a dictionary\ndatasets = {name: pd.read_csv(path) for name, path in data_paths.items()}\n\n# Display basic information for each dataset\nfor name, df in datasets.items():\n    print(f\"Dataset: {name}\")\n    display(df.head())\n    print(df.info(), \"\\n\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T09:56:07.923812Z","iopub.execute_input":"2025-01-29T09:56:07.924121Z","iopub.status.idle":"2025-01-29T09:57:24.037947Z","shell.execute_reply.started":"2025-01-29T09:56:07.924091Z","shell.execute_reply":"2025-01-29T09:57:24.037111Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## DATA ANALYSIS","metadata":{}},{"cell_type":"code","source":"for name, df in datasets.items():\n    print(f\"Dataset: {name}\")\n    display(df.describe())\n    print(df.info(), \"\\n\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T09:57:24.039349Z","iopub.execute_input":"2025-01-29T09:57:24.039611Z","iopub.status.idle":"2025-01-29T09:57:36.539777Z","shell.execute_reply.started":"2025-01-29T09:57:24.039558Z","shell.execute_reply":"2025-01-29T09:57:36.539113Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"###   Data Preprocessing","metadata":{}},{"cell_type":"code","source":"for name, df in datasets.items():\n    print(f\"{name}: {df.isnull().sum()}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T09:57:36.540633Z","iopub.execute_input":"2025-01-29T09:57:36.540888Z","iopub.status.idle":"2025-01-29T09:57:47.832917Z","shell.execute_reply.started":"2025-01-29T09:57:36.540860Z","shell.execute_reply":"2025-01-29T09:57:47.832153Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Convert date columns to datetime format\ndate_columns = [\"train\", \"test\", \"transactions\", \"holidays\", \"oil\"]\n\nfor dataset in date_columns:\n    datasets[dataset][\"date\"] = pd.to_datetime(datasets[dataset][\"date\"])\n\n# Convert categorical columns\ncategory_columns = {\n    \"stores\": [\"city\", \"state\", \"type\", \"cluster\"],\n    \"items\": [\"family\"],\n    \"holidays\": [\"type\", \"locale\", \"locale_name\", \"description\", \"transferred\"],\n}\n\nfor dataset, cols in category_columns.items():\n    for col in cols:\n        datasets[dataset][col] = datasets[dataset][col].astype(\"category\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T09:57:47.833758Z","iopub.execute_input":"2025-01-29T09:57:47.833983Z","iopub.status.idle":"2025-01-29T09:58:00.220256Z","shell.execute_reply.started":"2025-01-29T09:57:47.833964Z","shell.execute_reply":"2025-01-29T09:58:00.219580Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"datasets[\"oil\"]['dcoilwtico'] = datasets[\"oil\"]['dcoilwtico'].interpolate()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T09:58:00.221020Z","iopub.execute_input":"2025-01-29T09:58:00.221313Z","iopub.status.idle":"2025-01-29T09:58:00.226403Z","shell.execute_reply.started":"2025-01-29T09:58:00.221274Z","shell.execute_reply":"2025-01-29T09:58:00.225647Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"datasets[\"oil\"]['dcoilwtico'] = datasets[\"oil\"]['dcoilwtico'].fillna(method='bfill')\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T09:58:00.227325Z","iopub.execute_input":"2025-01-29T09:58:00.227658Z","iopub.status.idle":"2025-01-29T09:58:00.252055Z","shell.execute_reply.started":"2025-01-29T09:58:00.227634Z","shell.execute_reply":"2025-01-29T09:58:00.251315Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Sales Ditribution","metadata":{}},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(12, 6))\nsns.histplot(datasets[\"train\"][\"unit_sales\"], bins=50, kde=True)\nplt.title(\"Distribution of Sales (Unit Sales)\")\nplt.xlabel(\"Unit Sales\")\nplt.ylabel(\"Frequency\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T09:58:00.252829Z","iopub.execute_input":"2025-01-29T09:58:00.253070Z","iopub.status.idle":"2025-01-29T10:05:39.822780Z","shell.execute_reply.started":"2025-01-29T09:58:00.253052Z","shell.execute_reply":"2025-01-29T10:05:39.821941Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nfor name, df in datasets.items():\n    datasets[name] = df.replace([np.inf, -np.inf], np.nan)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T10:05:39.825100Z","iopub.execute_input":"2025-01-29T10:05:39.825455Z","iopub.status.idle":"2025-01-29T10:06:06.765329Z","shell.execute_reply.started":"2025-01-29T10:05:39.825434Z","shell.execute_reply":"2025-01-29T10:06:06.764589Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for name, df in datasets.items():\n    print(f\"{name}: {df.isnull().sum()}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T10:06:06.766549Z","iopub.execute_input":"2025-01-29T10:06:06.766870Z","iopub.status.idle":"2025-01-29T10:06:12.939557Z","shell.execute_reply.started":"2025-01-29T10:06:06.766845Z","shell.execute_reply":"2025-01-29T10:06:12.938788Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Daily Sales Trends","metadata":{}},{"cell_type":"code","source":"# Aggregate daily sales\ndaily_sales = datasets[\"train\"].groupby(\"date\")[\"unit_sales\"].sum()\n\nplt.figure(figsize=(14, 6))\nplt.plot(daily_sales, marker=\"o\", linestyle=\"-\", label=\"Total Sales\")\nplt.title(\"Daily Sales Trend Over Time\")\nplt.xlabel(\"Date\")\nplt.ylabel(\"Total Unit Sales\")\nplt.legend()\nplt.grid(True)\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T10:06:12.940339Z","iopub.execute_input":"2025-01-29T10:06:12.940678Z","iopub.status.idle":"2025-01-29T10:06:15.800335Z","shell.execute_reply.started":"2025-01-29T10:06:12.940645Z","shell.execute_reply":"2025-01-29T10:06:15.799420Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Sales by stores","metadata":{}},{"cell_type":"code","source":"# Merge train dataset with stores\ntrain_stores = datasets[\"train\"].merge(datasets[\"stores\"], on=\"store_nbr\", how=\"left\")\n\n# Aggregate sales by store type\nsales_by_store_type = train_stores.groupby(\"type\")[\"unit_sales\"].sum()\n\n# Plot sales by store type\nplt.figure(figsize=(10, 6))\nsns.barplot(x=sales_by_store_type.index, y=sales_by_store_type.values)\nplt.title(\"Total Sales by Store Type\")\nplt.xlabel(\"Store Type\")\nplt.ylabel(\"Total Unit Sales\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T10:06:15.801208Z","iopub.execute_input":"2025-01-29T10:06:15.801482Z","iopub.status.idle":"2025-01-29T10:06:27.860622Z","shell.execute_reply.started":"2025-01-29T10:06:15.801449Z","shell.execute_reply":"2025-01-29T10:06:27.859856Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Sales by product family","metadata":{}},{"cell_type":"code","source":"# Merge train dataset with items\ntrain_items = datasets[\"train\"].merge(datasets[\"items\"], on=\"item_nbr\", how=\"left\")\n\n# Aggregate sales by product family\nsales_by_family = train_items.groupby(\"family\")[\"unit_sales\"].sum().sort_values(ascending=False)\n\n# Plot top product families\nplt.figure(figsize=(14, 6))\nsns.barplot(x=sales_by_family.index[:15], y=sales_by_family.values[:15])  # Top 15\nplt.xticks(rotation=45)\nplt.title(\"Top 15 Product Families by Sales\")\nplt.xlabel(\"Product Family\")\nplt.ylabel(\"Total Unit Sales\")\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T10:06:27.861396Z","iopub.execute_input":"2025-01-29T10:06:27.861736Z","iopub.status.idle":"2025-01-29T10:06:53.299297Z","shell.execute_reply.started":"2025-01-29T10:06:27.861700Z","shell.execute_reply":"2025-01-29T10:06:53.298580Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns\n\n# Boxplot to detect outliers\nplt.figure(figsize=(12, 6))\nsns.boxplot(x=datasets[\"train\"][\"unit_sales\"])\nplt.title(\"Boxplot of Sales Data (unit_sales)\")\nplt.show()\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T10:06:53.300196Z","iopub.execute_input":"2025-01-29T10:06:53.300492Z","iopub.status.idle":"2025-01-29T10:07:19.454050Z","shell.execute_reply.started":"2025-01-29T10:06:53.300457Z","shell.execute_reply":"2025-01-29T10:07:19.453157Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Feature Engineering","metadata":{}},{"cell_type":"code","source":"# Ensure 'date' is in datetime format\ndatasets[\"train\"][\"date\"] = pd.to_datetime(datasets[\"train\"][\"date\"])\n\n# Extract date features\ndatasets[\"train\"][\"day\"] = datasets[\"train\"][\"date\"].dt.day\ndatasets[\"train\"][\"month\"] = datasets[\"train\"][\"date\"].dt.month\ndatasets[\"train\"][\"year\"] = datasets[\"train\"][\"date\"].dt.year\ndatasets[\"train\"][\"day_of_week\"] = datasets[\"train\"][\"date\"].dt.dayofweek\ndatasets[\"train\"][\"is_weekend\"] = datasets[\"train\"][\"day_of_week\"].isin([5, 6]).astype(int)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-29T10:07:19.454878Z","iopub.execute_input":"2025-01-29T10:07:19.455137Z","iopub.status.idle":"2025-01-29T10:07:35.362344Z","shell.execute_reply.started":"2025-01-29T10:07:19.455114Z","shell.execute_reply":"2025-01-29T10:07:35.361422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}