Ravindra BagaleCourses & study guides मराठी Track your progress

65. How to Forecast Sales with Time Series in Python (pandas and sklearn)

65.7 MAE, MAPE and Plot Past vs Forecast

Why. “Looks okay on a chart” is not enough. MAE and MAPE give numbers you can put on WhatsApp to a manager.

What.

  • MAE — mean absolute error in the same units (₹). Easy to read.
  • MAPE — mean absolute percentage error. Good to compare scales.

How.

Score and plot

  1. Compute MAE = mean(|actual − pred|).
  2. Compute MAPE = mean(|actual − pred| / actual) * 100.
  3. Build one scoreboard for naive, seasonal naive, MA3, lag+LR.
  4. Plot actual line + forecast line; mark the split.

Worked example A — Scoreboard (Mauli May–Oct 2026).

Model MAE (₹) MAPE (%)
Naive last-value 93,583 11.87
Seasonal naive 103,867 14.58
MA(3) flat 123,416 15.85
Lag + LinearRegression 7,258 0.97
Holt-Winters (optional) 21,162 2.79

What it means. On this fictional series, lag + LR wins. Always re-check on your data.

Worked example B — MAPE in code.

import numpy as np

def mape(y, p):
    y = np.asarray(y, float)
    p = np.asarray(p, float)
    return float(np.mean(np.abs((y - p) / y)) * 100)

actual = [674800, 620700, 602500, 694700, 799900, 878700]
lr_pred = [664259, 623235, 606439, 697050, 807966, 862580]
print("MAPE %:", round(mape(actual, lr_pred), 2))

Output you should see.

MAPE %: 0.97

Worked example C — Plot.

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression

df = pd.read_csv("mauli_monthly_sales.csv", parse_dates=["month"]).sort_values("month")
# ... build pred for test months as in 65.5 ...
# plt.plot(df["month"], df["sales_inr"], label="Actual")
# plt.plot(test["month"], pred, label="Lag + LR")
# plt.axvline(pd.Timestamp("2026-05-01"), ls="--", label="Split")
# plt.legend(); plt.show()

The chapter figure ch65-forecast-plot.png shows actual vs lag+LR and Holt-Winters with the train/test line.

Mauli Kirana fictional monthly sales versus lag LinearRegression and Holt-Winters forecast

Ravindra Bagale's Tip

MAE = error in ₹. MAPE = error in %. Give managers both. The chart builds trust.