Problem 509321 · easy · Level 05 Advanced Algorithms & Graphs

Every Day Takes a Turn

k-fold cross-validation · validation · mean squared error · baseline model

A café forecasts tomorrow's cups of hot chocolate with the simplest model there is: the average of the days it learned from. To see how good that forecast is on days it has not seen, the owner lets every part of the log take a turn at being held out.

Cut the log ys into k folds of consecutive days: the first n % k folds get n // k + 1 days and the others n // k days, where n = len(ys), in the order of the log. For each fold in turn, the model learns the average of all the days outside the fold and is scored by its mean squared error on the days inside the fold.

Write mean_model_cv(ys, k) that returns a tuple (fold_errors, score): the list of the k fold errors, in fold order, and their average.

Examples

Input:  ys = [4, 6, 8, 10, 12], k = 2
Output: ([27.666666666666668, 26.0], 26.833333333333336)
Explanation: the folds are [4, 6, 8] and [10, 12]. Holding out the first fold, the model
learns 11 and misses by 7, 5 and 3: (49 + 25 + 9) / 3. Holding out the second, it learns 6
and misses by 4 and 6: (16 + 36) / 2 = 26.

Input:  ys = [3, 3, 3], k = 3
Output: ([0.0, 0.0, 0.0], 0.0)

Constraints

  • 2 <= k <= len(ys) <= 10**4
  • floats are compared with a tolerance of 1e-6

Goals

  • Cut a data set into k folds of nearly equal size by a stated rule
  • Train on k - 1 folds and validate on the remaining one, for every fold in turn
  • Average the k validation errors into one cross-validation score
Starting Python…