Polynomial regression bends a straight line by giving it extra columns. From a single input it builds the row
and fits an ordinary linear model on those columns:
The degree 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 , with one row per training point and columns, and put the training answers into . The coefficients that give the smallest possible mean squared error on the training points solve
You never need itself. Each entry is a plain sum over the training points , with and running from to :
That is linear equations in unknowns. Solve them either way:
A and c as arrays, and np.linalg.solve(A, c) returns in that order. Turn your two final answers into plain floats with float(...).Task: write degree_check(train, test, degree).
train and test are lists of (x, y) pairs.train only, for the given degree. A degree of means the constant column alone.(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.