Sahaj.blogs
1 Aug 2024 · StudyPhase 7 · Machine Learning

Machine Learning · The ML workflow

The end-to-end supervised learning loop: define the task, split data, pick a baseline, fit, evaluate on held-out data, iterate.

Phase 7, machine learning, starts today. Before the algorithms, I wanted the workflow that all of them sit inside:

  1. Frame the task. What is the target? Is it classification (a category) or regression (a number)? Which metric matters?
  2. Split the data: train / (validation) / test. The test set is touched once, at the end.
  3. Baseline: the simplest possible predictor (most frequent class, mean value). Any real model has to beat it.
  4. Fit a model on the training data.
  5. Evaluate on data the model hasn’t seen.
  6. Iterate: features, model choice, hyperparameters, all decided on validation data or with cross-validation.

Every scikit-learn estimator has the same API: fit(X, y), predict(X), score(X, y).

from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline

X, y = load_breast_cancer(return_X_y=True)
print("data:", X.shape, "classes:", sorted(set(y.tolist())))

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=0)

baseline = DummyClassifier(strategy="most_frequent").fit(X_train, y_train)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000)).fit(X_train, y_train)

print("baseline accuracy:", round(baseline.score(X_test, y_test), 3))
print("model accuracy   :", round(model.score(X_test, y_test), 3))

Output:

data: (569, 30) classes: [0, 1]
baseline accuracy: 0.632
model accuracy   : 0.982

#machine-learning#scikit-learn

Sources

  1. scikit-learn: Getting started — scikit-learn
  2. Google Machine Learning Crash Course — Google for Developers
  3. Stanford CS229 Machine Learning — Stanford University
  4. sklearn.dummy.DummyClassifier — scikit-learn