Polynomial Features and Overfitting

easy
Unit 4 · Preprocessing & Features0/3 to milestone

A straight line can't fit a curve, but linear regression on polynomial features can. For one input column xx, degree dd gives the columns [1,x,x2,…,xd][1, x, x^2, \dots, x^d].

Implement:

  • poly_features(x, degree): shape (n, degree + 1), starting with the column of ones.
  • fit_poly(x, y, degree): least-squares coefficients via np.linalg.lstsq on those features.
  • predict_poly(x, coef).
  • train_test_mse(x_train, y_train, x_test, y_test, degree): returns (train_mse, test_mse), so you can watch overfitting happen as the degree grows.
#features#regression#overfitting2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Polynomial Features and Overfitting · TechGradify