typestar

Cross validation in Python

One split is a coin toss; cross validation scores every fold and averages.

from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

X, y = load_iris(return_X_y=True)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=500))

scores = cross_val_score(model, X, y, cv=5, scoring="accuracy")
print(scores.round(3))
print(f"{scores.mean():.3f} +/- {scores.std():.3f}")

How it works

  1. cross_val_score fits a fresh clone of the estimator per fold.
  2. cv=5 means five folds, so five scores come back.
  3. The mean and the spread together tell you what to trust.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
484
Tokens
109
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 59 seconds.

Type this snippet

Step 1 of 4 in Validation, step 8 of 25 in Machine learning with scikit-learn.

← Previous Next →