typestar

Does more data help? in Python

A learning curve answers whether to gather data or change the model.

import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import learning_curve
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC

X, y = load_digits(return_X_y=True)
model = make_pipeline(StandardScaler(), SVC(gamma=0.001))

sizes, train_scores, valid_scores = learning_curve(
    model, X, y, train_sizes=np.linspace(0.2, 1.0, 4), cv=4)

for n, train, valid in zip(sizes, train_scores.mean(axis=1),
                           valid_scores.mean(axis=1)):
    print(f"{n:5.0f} rows  train {train:.3f}  valid {valid:.3f}")

How it works

  1. learning_curve refits at several training sizes.
  2. Train and validation scores converging means more data will not help.
  3. A wide gap is variance: regularise or simplify.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
587
Tokens
143
Three-star pace
110 tpm

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

Type this snippet

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

← Previous Next →