FunctionRacerProblemsRace

Right Amount of Bend

Hardscikit-learnPython

A curve fitted to noisy points can be too stiff or too wiggly. For each degree from 1 to 5, fit a polynomial of that degree to the points (x[i], y[i]) (a pipeline of polynomial features and linear regression) and score it by 5-fold cross-validation with the negative mean squared error. Return the degree with the best average score.

Example 1

x [0.0,0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0,5.5,6.0,6.5,7.0,7.5,8.0,8.5,9.0,9.5,10.0,10.5,11.0,11.5,12.0]
y [1.3,1.8,3.1,3.6,5.2,6.3,6.8,8.1,8.6,10.2,11.3,11.8,13.1,13.6,15.2,16.3,16.8,18.1,18.6,20.2,21.3,21.8,23.1,23.6,25.2]
returns 1

The points follow a straight line with a little noise. Higher degrees only chase the noise.

Example 2

x [-4.0,-3.6666666666666665,-3.3333333333333335,-3.0,-2.666666666666667,-2.333333333333333,-2.0,-1.6666666666666665,-1.3333333333333335,-1.0,-0.6666666666666665,-0.3333333333333335,0.0,0.33333333333333304,0.666666666666667,1.0,1.333333333333333,1.666666666666667,2.0,2.333333333333333,2.666666666666667,3.0,3.333333333333333,3.666666666666667,4.0]
y [16.3,13.244444444444444,11.211111111111112,8.6,7.311111111111113,5.744444444444443,3.8,2.8777777777777773,1.3777777777777782,1.2,0.7444444444444442,-0.0888888888888888,0.1,-0.2888888888888891,0.6444444444444448,1.3,1.577777777777777,2.8777777777777787,3.6,5.644444444444443,7.411111111111112,8.8,11.211111111111109,13.044444444444446,16.2]
returns 2

The points follow a parabola: a line is too stiff and anything above degree 2 is too loose.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More scikit-learn problems