How Much Bend Is Enough?

Classic MLMedium+20 XP+20

How Much Bend Is Enough?

Polynomial regression bends a straight line by giving it extra columns. From a single input xxx it builds the row

[ 1,  x,  x2,  …,  xd ][\,1,\; x,\; x^2,\; \dots,\; x^d\,][1,x,x2,…,xd]

and fits an ordinary linear model on those columns:

y^=b0+b1x+b2x2+⋯+bdxd\hat y = b_0 + b_1 x + b_2 x^2 + \dots + b_d x^dy^​=b0​+b1​x+b2​x2+⋯+bd​xd

The degree ddd is the dial from Polynomial Regression. Set it too low and the curve misses the bend; set it too high and the curve chases the noise. To tell which is happening, fit on training points, then measure the error on points the fit never saw.

Fitting. In Linear Regression the coefficients came from gradient descent, which creeps toward the best fit without ever landing exactly on it. The tests here expect the exact best fit, so jump straight to it with the normal equations from Linear Algebra. Put the expanded training rows into a table XXX, with one row per training point and d+1d + 1d+1 columns, and put the training answers into yyy. The coefficients b=(b0,…,bd)b = (b_0, \dots, b_d)b=(b0​,…,bd​) that give the smallest possible mean squared error on the training points solve

A b=c,A=X⊤X,c=X⊤yA\, b = c, \qquad A = X^\top X, \qquad c = X^\top yAb=c,A=X⊤X,c=X⊤y

You never need XXX itself. Each entry is a plain sum over the training points (x,y)(x, y)(x,y), with iii and jjj running from 000 to ddd:

Aij=∑x i+j,ci=∑x i yA_{ij} = \sum x^{\,i+j}, \qquad c_i = \sum x^{\,i}\, yAij​=∑xi+j,ci​=∑xiy

That is d+1d + 1d+1 linear equations in d+1d + 1d+1 unknowns. Solve them either way:

  • With numpy (already imported in the starter): build A and c as arrays, and np.linalg.solve(A, c) returns b0,…,bdb_0, \dots, b_db0​,…,bd​ in that order. Turn your two final answers into plain floats with float(...).
  • By elimination, as in Solving by Elimination: for each column in turn, subtract the right multiple of that column's row from every row below it, until every entry under the diagonal is 0. The last equation then has one unknown. Solve it, and work back up, one new unknown per row. Because the training set has at least d+1d + 1d+1 different xxx values, the diagonal number you divide by is never 0, so no row swaps are needed.

Task: write degree_check(train, test, degree).

  • train and test are lists of (x, y) pairs.
  • Fit the coefficients on train only, for the given degree. A degree of 000 means the constant column alone.
  • Return a tuple (train_mse, test_mse): the mean squared error of that one fitted curve on the training points and on the test points. Round each to 4 decimal places and return them as plain Python floats.

Model Complexity & Bias-Variance Tradeoff

Adjust model complexity (degree of curve fit) to see how it trades underfitting (high bias) for overfitting (high variance).

Error Metrics

Fit State: Optimal
Training MSE: 0.28
Validation MSE: 0.35

Optimal Tradeoff: Model fits the trend without learning random sample noise. Lowest validation error.

Examples

train = [(0, 1.2), (1, 0.4), (2, 1.1), (3, 3.3), (4, 7.4), (5, 12.9)]
test = [(1.5, 0.5), (3.5, 5.6)]
degree = 1
returns (4.0388, 1.8243)
train = [(0, 1.2), (1, 0.4), (2, 1.1), (3, 3.3), (4, 7.4), (5, 12.9)]
test = [(1.5, 0.5), (3.5, 5.6)]
degree = 2
returns (0.0031, 0.0907)

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run