Errors Come Out Negative

Machine Learning in CodeMedium+10 XP+10

Errors Come Out Negative

A hospital wants to know how far off a diabetes-progression model typically is. You will cross-validate it with different scores. One catch: scikit-learn always treats bigger as better, so it hands error scores back negated.

Task: write cv_regression_score(scoring, n_splits).

  1. Load the data with load_diabetes(return_X_y=True, as_frame=True).
  2. Use a plain LinearRegression().
  3. Cross-validate it with cross_val_score, using cv=KFold(n_splits=n_splits, shuffle=True, random_state=0) and scoring=scoring.
  4. Take the mean of the fold scores.

scoring is one of "neg_mean_absolute_error", "neg_mean_squared_error", "neg_root_mean_squared_error" or "r2".

If the name starts with "neg_", return the mean as a positive error. Otherwise return it as it is. Round to 2 decimal places.

Examples

scoring = "neg_mean_absolute_error"
n_splits = 5
returns 44.29
scoring = "r2"
n_splits = 5
returns 0.49

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