Cross-Validation

~12 mincode completion

Write cv_mean_score(X, y, folds) that runs cross-validation on a (with random_state=0) and returns the mean score as a float.

Examples

A perfectly separable dataset scores 1.0 across every fold

Input
cv_mean_score([[0], [1], [2], [8], [9], [10]], [0, 0, 0, 1, 1, 1], 3)
Output
1

The same data with two folds still scores 1.0

Input
cv_mean_score([[0], [1], [2], [8], [9], [10]], [0, 0, 0, 1, 1, 1], 2)
Output
1

Hints

Hint 1

Work directly with the arguments X, y, folds and return the result rather than printing it.

Hint 2

Make sure you are not returning all fold scores.

Requirements

  • X: 2-D feature array

  • y: 1-D label array

  • folds: how many folds, e.g. 3

  • Return the mean of the per-fold scores, as a float.

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~12 min

7 employers weight this skill

2 health and bio companies, 2 quant funds, 2 enterprise vendors, 1 AI product company. Top match scores 43.

Python
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score

def cv_mean_score(X, y, folds):
    """
    Mean cross-validated accuracy.

    Args:
        X: 2-D feature array
        y: 1-D label array
        folds: how many folds, e.g. 3

    Returns:
        The mean of the per-fold scores, as a float.
    """
    # YOUR CODE HERE
    pass

Run your code to see results

⌘↵ runs against the visible tests

Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.