Choose a Polynomial Degree by Validation

medium
Unit 13 · Recommenders & Metrics0/3 to milestone

Implement select_degree(x_train, y_train, x_val, y_val, max_degree):

  • for each degree 0..max_degree, fit a polynomial by least squares with np.polyfit(x_train, y_train, d)
  • compute validation MSE
  • return (best_degree, val_errors) where val_errors is a list indexed by degree. Ties go to the lower degree.

Training error always falls with degree; validation error is what reveals overfitting.

#model-selection#overfitting#regression2 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Choose a Polynomial Degree by Validation · TechGradify