Welcome to Day 25: Data Odyssey, our 365-day journey to master data science and artificial intelligence (AI), launched on Shivaratri, February 26, 2025! Yesterday, in Day 24: Data Odyssey – What is Time Series Analysis?, we explored Priya’s sales as a time series. Her 7-row dataset revealed a 9 AM peak (₹630 avg), daily cycles (7 AM low, 8-9 AM high), and a slight upward trend (₹1300 to ₹1350 daily totals)—all indexed by time with Pandas. Today, we look ahead: How do we forecast with time series, and what’s Priya’s next 9 AM sales prediction?
Predicting the Future
Forecasting extends time series analysis (Day 24) to predict future values—Priya’s Thursday 9 AM sales after Wednesday’s ₹640. It’s “model” and “communicate” in our workflow (Day 1), blending trends (rising sales), seasonality (9 AM spikes), and noise (Tuesday’s ₹150 dip). Unlike her Random Forest (Day 23, ₹642), time series forecasting uses sequence, not just features.
Think of it as Priya planning tomorrow’s stock. Her 9 AM ₹630 avg hints at ₹640+, but forecasting nails it—40 samosas or 42? Day 25: Data Odyssey predicts this.
Why Forecasting Matters
Priya’s deployed model (Day 23) guesses ₹642—static, feature-based. Time series forecasting:
- Trends: Catches ₹600 to ₹650 rise.
- Cycles: Locks 9 AM’s rush.
- Proactive: Stock Thursday before it hits.
Her 7 rows limit precision—Day 12’s 35 rows beckon—but simple methods start her off. Day 25: Data Odyssey forecasts this.
Priya’s Time Series Recap
Her data (Day 24):
Sales
2025-03-03 07:00:00 200
2025-03-03 08:00:00 500
2025-03-03 09:00:00 600
2025-04-03 07:00:00 150
2025-04-03 08:00:00 550
2025-04-03 09:00:00 650
2025-05-03 09:00:00 640
- Trend: 9 AM rises—₹600, ₹650, ₹640.
- Seasonality: 7 AM low (₹175 avg), 8-9 AM high (₹525, ₹630).
- Sparse: 7 points, gaps.
Goal: Forecast Thursday, March 6, 9 AM. Day 25: Data Odyssey starts here.
Simple Forecasting Methods
Start basic—no complex stats yet:
- Moving Average:
- Average last n points—smooths noise.
- 3-point MA for 9 AM.
- Exponential Smoothing:
- Weights recent sales more—adapts fast.
- Simple version for her short series.
- Trend Extension:
- Extend 9 AM’s rise—₹600 to ₹650.
7 rows suit simple—Day 12’s 35 unlock ARIMA later. Day 25: Data Odyssey tries these.
Moving Average
For 9 AM (3 points):
import pandas as pd
data = pd.DataFrame({
"Datetime": ["2025-03-03 09:00", "2025-03-04 09:00", "2025-03-05 09:00"],
"Sales": [600, 650, 640]
})
data["Datetime"] = pd.to_datetime(data["Datetime"])
data.set_index("Datetime", inplace=True)
ma = data["Sales"].rolling(window=3, min_periods=1).mean()
print(ma)
Output:
2025-03-03 09:00 600.0
2025-03-04 09:00 625.0
2025-03-05 09:00 630.0
Last MA: ₹630—Thursday’s 9 AM ≈ ₹630. Too flat? Day 25: Data Odyssey averages this.
Exponential Smoothing
Weight recent—use pandas:
es = data["Sales"].ewm(span=3, adjust=False).mean()
print(es)
Output:
2025-03-03 09:00 600.000
2025-03-04 09:00 625.000
2025-03-05 09:00 632.500
Last ES: ₹632.5—Thursday ≈ ₹632-633. Closer! Day 25: Data Odyssey smooths this.
Trend Extension
9 AM: ₹600, ₹650, ₹640—up, then steady:
- Avg change: (50 + (-10)) / 2 = ₹20/day.
- Next: ₹640 + ₹20 = ₹660—too high?
Adjust: Last two (₹650, ₹640) avg ₹645—next ≈ ₹645. Day 25: Data Odyssey trends this.
Full Script
Combine for 9 AM:
import pandas as pd
import matplotlib.pyplot as plt
# 9 AM data
data = pd.DataFrame({
"Datetime": ["2025-03-03 09:00", "2025-03-04 09:00", "2025-03-05 09:00"],
"Sales": [600, 650, 640]
})
data["Datetime"] = pd.to_datetime(data["Datetime"])
data.set_index("Datetime", inplace=True)
# Forecasts
ma = data["Sales"].rolling(window=3, min_periods=1).mean().iloc[-1] # 630
es = data["Sales"].ewm(span=3, adjust=False).mean().iloc[-1] # 632.5
trend = (data["Sales"].iloc[-2:].mean() + 5) # 645 + tweak
print("Moving Avg Forecast:", ma)
print("Exp Smoothing Forecast:", es)
print("Trend Forecast:", trend)
# Plot
data["Sales"].plot(label="Actual", marker="o", color="teal")
plt.axhline(ma, color="blue", linestyle="--", label=f"MA: {ma}")
plt.axhline(es, color="green", linestyle="--", label=f"ES: {es}")
plt.axhline(trend, color="red", linestyle="--", label=f"Trend: {trend}")
plt.title("9 AM Sales Forecast")
plt.xlabel("Date")
plt.ylabel("Sales (₹)")
plt.legend()
plt.show()
Output:
Moving Avg Forecast: 630.0
Exp Smoothing Forecast: 632.5
Trend Forecast: 650.0
Plot: ₹630-650 range—Thursday’s guess! Day 25: Data Odyssey forecasts this.
Picking a Forecast
- MA ₹630: Smooth, conservative—40 samosas.
- ES ₹632.5: Recent-weighted—40-41.
- Trend ₹650: Aggressive—42.
ES ₹632.5 aligns with Random Forest’s ₹642 (Day 23)—blend both? Day 25: Data Odyssey weighs this.
Adding Features
Day 21’s Weather_Rainy, Rush_Hour—time series ignores:
data["Weather_Rainy"] = [0, 1, 0]
es_adj = data["Sales"].ewm(span=3).mean() + data["Weather_Rainy"] * 10 # Rain +₹10
print(es_adj.iloc[-1])
Output: 632.5—no rain Thursday, steady. Day 25: Data Odyssey tweaks this.
Why Forecast?
- Precision: ₹632.5 vs. ₹642—time refines.
- Planning: 41 samosas, not guesswork.
- Scale: Weeks ahead—Day 12’s 35 rows.
Priya’s ₹632.5—stock aligns. Day 25: Data Odyssey predicts this.
Real-World Forecasting
India’s monsoons forecast rain—crops plan. Amazon predicts daily sales—stock adjusts. Priya’s ₹632.5 is her café’s crystal ball—small, sharp. Day 25: Data Odyssey mirrors this.
Challenges
- Short: 3 9 AMs—35 rows (Day 12) firm it.
- Gaps: 7-8 AM ignored—fill for full day.
- Noise: ₹150 skews—smooth more.
More data—Priya grows. Day 25: Data Odyssey flags this.
Why This Matters
Forecasting ₹632.5 for 9 AM—41 samosas, no waste, no shortage—beats ₹642’s guess. Without it, she drifts; with it, she plans—profit up. Scale it: forecasted traffic clears India’s jams—lives ease. Day 25: Data Odyssey sees her future.
Recap Summary
Yesterday, Day 24: Data Odyssey analyzed Priya’s time series—9 AM ₹630 avg, daily cycles. Today, Day 25: Data Odyssey forecasted—MA ₹630, ES ₹632.5, Trend ₹650 for Thursday’s 9 AM. It’s her next step.
What’s Next
Tomorrow, in Day 26: Data Odyssey – What is Anomaly Detection?, we’ll spot oddities: Why ₹150 at 7 AM? We’ll use her series to catch outliers, refining forecasts. Bring your curiosity, and I’ll see you there!










