A fourth-degree polynomial can pass through five training observations almost perfectly. That does not automatically make it a better predictor than a quadratic. This Python example compares three model degrees on a fixed set of separate observations.
Run the example
I ran this script in PythonIDE on an English iPhone simulator. All observations are fictional.
import numpy as np import matplotlib.pyplot as plt # Fictional response curve and fixed, disjoint observations. x_train = np.array([0, 2, 4, 6, 8], dtype=float) y_train = np.array([1.2, 4.7, 16.4, 36.8, 64.3]) x_check = np.array([1, 3, 5, 7], dtype=float) y_check = np.array([2.0, 9.8, 25.6, 49.7]) assert not set(x_train) & set(x_check) models = {} print('FICTIONAL MODEL COMPARISON') for degree in [1, 2, 4]: coef = np.polyfit(x_train, y_train, degree) train_pred = np.polyval(coef, x_train) check_pred = np.polyval(coef, x_check) train_rmse = np.sqrt(np.mean((y_train-train_pred)**2)) check_rmse = np.sqrt(np.mean((y_check-check_pred)**2)) models[degree] = (coef, train_rmse, check_rmse) print(f'Degree {degree}: training={train_rmse:.3f}, ' f'held-out={check_rmse:.3f}') # The held-out split is now a validation set, not a final test. best = min(models, key=lambda degree: models[degree][2]) print('Lowest held-out RMSE in this experiment:', best) print('A further untouched test set is needed after selection.') grid = np.linspace(0, 8, 150) fig, ax = plt.subplots(figsize=(7, 4)) ax.scatter(x_train, y_train, label='Training observations') ax.scatter(x_check, y_check, marker='x', s=60, color='black', label='Held-out observations') for degree, (coef, _, _) in models.items(): ax.plot(grid, np.polyval(coef, grid), label=f'Degree {degree}') ax.set(title='Model choice on fictional data', xlabel='Input', ylabel='Response') ax.legend() ax.grid(alpha=.2) plt.tight_layout() plt.show() Read the behavior and its boundaries
The input split is explicit and disjoint. polyfit estimates coefficients on training observations only, while polyval evaluates the same coefficients on both sets. Report training and held-out RMSE separately, then plot all models over the observed range. The example never claims the highest-degree model must be worst on every dataset.
Once held-out results are used to choose a degree, that split is a validation set. It is no longer an untouched final test. A further independent dataset is needed for a final performance claim. Nine fictional observations do not support broad scientific conclusions, and the polynomial units and extrapolation behavior need care.
Only the .py script was tested in PythonIDE. If you adapt it to a notebook, separate setup, fitting, and display into cells, then restart and run all before comparing screenshots. Change only one modeling decision at a time and record it; stale notebook variables can otherwise make the displayed curve disagree with the code.
Try one change
Change one training response by two units and compare all three curves and both error columns. Then explain why repeatedly choosing models on the held-out set consumes its independence.
I build PythonIDE, a Python coding app for iPhone and iPad. AI assisted with drafting and translation; the script, numerical output and graph were checked in PythonIDE on an iPhone simulator.
Observed run: degree 1 training/held-out RMSE 6.780/5.005; degree 2 0.192/0.154; degree 4 0.000/0.194. Degree 2 wins this small validation comparison, despite degree 4 fitting the training data more closely.




