{ "cells": [ { "cell_type": "markdown", "id": "c8395d78", "metadata": {}, "source": [ "## **Forecasting Supermarket Grocery Sales Using Facebook Prophet**" ] }, { "cell_type": "markdown", "id": "1154b558", "metadata": {}, "source": [ "\n", "\n", "#### Facebook Prophet is an open-source forecasting tool developed by Meta. It is designed for time series data with **seasonality**, **trend shifts**, and **holiday effects**, making it ideal for business use cases like sales and demand forecasting.\n", "\n", "---\n", "\n", "### **Why Use Prophet?**\n", "\n", "* Handles **missing data** and **outliers**\n", "* Automatically detects **trends** and **seasonal patterns**\n", "* Supports **holiday effects**\n", "* Easy to use and **interpretable**\n", "* Scales well for **multiple time series**\n", "\n", "---\n", "\n", "### **How It Works**\n", "\n", "Prophet models time series using this additive formula:\n", "\n", "$$\n", "y(t) = \\text{Trend} + \\text{Seasonality} + \\text{Holidays} + \\text{Error}\n", "$$\n", "\n", "It supports both **linear** and **logistic trends**, and allows **custom seasonality** if needed.\n", "\n", "---\n" ] }, { "cell_type": "markdown", "id": "5793fb88", "metadata": {}, "source": [ "## **Overview**\n", "\n", "The notebook focuses on forecasting **daily grocery sales** for a supermarket over time. \n", "The dataset contains records of **sales and corresponding profit** from **2015 to 2019**.\n" ] }, { "cell_type": "markdown", "id": "8e2bd78c", "metadata": {}, "source": [ "# Step1- Importing libraries" ] }, { "cell_type": "code", "execution_count": null, "id": "b626268e", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from prophet.plot import plot_plotly, plot_components_plotly\n", "from prophet import Prophet\n", "import matplotlib.pyplot as plt\n", "from statsmodels.tools.eval_measures import rmse\n", "\n", "from sklearn.metrics import mean_squared_error, r2_score, mean_absolute_error\n", "import math \n", "import numpy as np\n" ] }, { "cell_type": "markdown", "id": "6337e0d3", "metadata": {}, "source": [ "# Step2 - Dataset loading and Preparation" ] }, { "cell_type": "code", "execution_count": 2, "id": "77f8fc6b", "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('I:/CQAI/TSA/TSD/TSD/data/Supermart Grocery Sales - Retail Analytics Dataset.csv',usecols=[\"Order Date\",\"Sales\",\"Profit\"], header=0)" ] }, { "cell_type": "code", "execution_count": 3, "id": "4fd95f47", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Order Date 0\n", "Sales 0\n", "Profit 0\n", "dtype: int64" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.isnull().sum()\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "8cd05f22", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | ds | \n", "y | \n", "Profit | \n", "
---|---|---|---|
3323 | \n", "2015-01-03 | \n", "864 | \n", "380.16 | \n", "
739 | \n", "2015-01-04 | \n", "2033 | \n", "325.28 | \n", "
740 | \n", "2015-01-04 | \n", "698 | \n", "223.36 | \n", "
741 | \n", "2015-01-04 | \n", "759 | \n", "174.57 | \n", "
3872 | \n", "2015-01-05 | \n", "878 | \n", "149.26 | \n", "
\n", " | ds | \n", "yhat | \n", "yhat_lower | \n", "yhat_upper | \n", "
---|---|---|---|---|
1086 | \n", "2018-07-24 | \n", "1071.949815 | \n", "496.997468 | \n", "1681.719495 | \n", "
1087 | \n", "2018-07-25 | \n", "1981.472473 | \n", "1425.276393 | \n", "2534.658759 | \n", "
1088 | \n", "2018-07-26 | \n", "1161.112842 | \n", "563.374886 | \n", "1723.003795 | \n", "
1089 | \n", "2018-07-27 | \n", "845.224947 | \n", "272.928750 | \n", "1380.084360 | \n", "
1090 | \n", "2018-07-28 | \n", "2175.722300 | \n", "1640.317020 | \n", "2721.998965 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
1231 | \n", "2018-12-16 | \n", "1256.040185 | \n", "704.810679 | \n", "1811.339167 | \n", "
1232 | \n", "2018-12-17 | \n", "2152.991117 | \n", "1598.704995 | \n", "2715.232044 | \n", "
1233 | \n", "2018-12-18 | \n", "1207.028503 | \n", "677.870623 | \n", "1787.738389 | \n", "
1234 | \n", "2018-12-19 | \n", "1498.112389 | \n", "949.918018 | \n", "2049.403703 | \n", "
1235 | \n", "2018-12-20 | \n", "1474.992419 | \n", "927.072825 | \n", "2003.218239 | \n", "
150 rows × 4 columns
\n", "\n", " | ds | \n", "y | \n", "Profit | \n", "
---|---|---|---|
1086 | \n", "2018-07-24 | \n", "1611 | \n", "128.88 | \n", "
1087 | \n", "2018-07-25 | \n", "1633 | \n", "718.52 | \n", "
1088 | \n", "2018-07-26 | \n", "1525 | \n", "198.25 | \n", "
1089 | \n", "2018-07-27 | \n", "541 | \n", "27.05 | \n", "
1090 | \n", "2018-07-28 | \n", "2076 | \n", "830.40 | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "
1231 | \n", "2018-12-26 | \n", "741 | \n", "244.53 | \n", "
1232 | \n", "2018-12-27 | \n", "2233 | \n", "870.87 | \n", "
1233 | \n", "2018-12-28 | \n", "900 | \n", "252.00 | \n", "
1234 | \n", "2018-12-29 | \n", "2284 | \n", "433.96 | \n", "
1235 | \n", "2018-12-30 | \n", "997 | \n", "438.68 | \n", "
150 rows × 3 columns
\n", "