The degree-9 curve hit every training day with error 0, then missed 9 new days by 69. The simple degree-3 curve scored 15 on both, and that's the one to ship.
python3 --version.pip install "numpy>=1.26"git clone https://github.com/DayanEbrar0X/data-anatomy.ai.git cd data-anatomy.ai
python3 -m venv .venv source .venv/bin/activate
pip install -r requirements.txt # or just this lesson: pip install "numpy>=1.26"
cd machine-learning/10-overfitting python3 src/overfit.py
from icecream import temp, cones, train # 19 days
import numpy as np
for d in (9, 3):
p = np.polyfit(temp[train], cones[train], d)
err = np.polyval(p, temp) - cones
tr = np.sqrt(np.mean(err[train] ** 2))
va = np.sqrt(np.mean(err[~train] ** 2))
print(f"deg {d}: train {tr:.0f} val {va:.0f}")This model got a perfect training score. That's the problem. It memorized the data, not the pattern. Nineteen days of ice cream sales: ten for training, nine held back.
Fit a wild curve, degree nine, and a simple one, degree three. Score each on days it learned, and days it never saw. Run it. Degree nine: zero training error.
On the unseen days, it's off by 69 cones. Degree three misses by 15 on both. It holds up. A perfect training score means it memorized.
Memorizing isn't learning.
Read the lesson on GitHub →