Two Scores per Degree

Machine Learning in CodeEasy+10 XP+10

Two Scores per Degree

A plant's height over a season rises, levels off, then drops as it wilts. You have some measurements split into training points and held-back test points.

Write scores_by_degree(x_train, y_train, x_test, y_test, degrees).

For each degree in degrees, in order:

  • Fit make_pipeline(PolynomialFeatures(degree), LinearRegression()) on the training points, with x as the only feature.
  • Score it on the training points, then on the test points (score gives R²).

Return a list with one [degree, train_score, test_score] per degree. Round both scores to 3 decimal places.

Examples

x_train = [0.34, 0.45, 0.64, 1.14, 1.56, 1.73, 1.92, 2.33, 2.35, 2.94, 2.95, 3.83]
y_train = [12.05, 12.18, 13.6, 14.34, 14.93, 15.71, 14.59, 14.03, 13.2, 11.35, 11.18, 7.09]
x_test = [0.38, 0.95, 1.72, 2.07, 2.59, 3.21]
y_test = [11.77, 14.29, 15.16, 14.29, 12.5, 9.69]
degrees = [1, 2]
returns [[1, 0.282, 0.126], [2, 0.957, 0.923]]
x_train = [0.34, 0.45, 0.64, 1.14, 1.56, 1.73, 1.92, 2.33, 2.35, 2.94, 2.95, 3.83]
y_train = [12.05, 12.18, 13.6, 14.34, 14.93, 15.71, 14.59, 14.03, 13.2, 11.35, 11.18, 7.09]
x_test = [0.38, 0.95, 1.72, 2.07, 2.59, 3.21]
y_test = [11.77, 14.29, 15.16, 14.29, 12.5, 9.69]
degrees = [3]
returns [[3, 0.977, 0.979]]

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