Data analysis and machine learning with Python · Lesson 2 of 3
Machine learning with scikit-learn
Build a baseline model and evaluate it without leaking test information.
Lesson
1. Concept and mental model
Machine learning uses examples to estimate a relationship between features and a target. In scikit-learn, a typical workflow separates data into training and test sets, fits a model on training data, and evaluates it on held-out data. Keep the test set out of feature selection, preprocessing decisions, and repeated model tuning; otherwise the reported test score becomes an optimistic estimate rather than an independent check.
2. How it works
Preprocessing must follow the same fitted transformation for training and production data. Use a pipeline to combine steps such as imputation, scaling, encoding, and model fitting. This reduces the chance of fitting a scaler to test data accidentally and makes the transformation reproducible. Select metrics based on the problem: accuracy can be misleading for imbalanced classes, while precision, recall, F1, or ranking metrics may better reflect the cost of errors.
3. Use it safely and effectively
Compare a model against a simple baseline and inspect errors by relevant subgroup or condition. A high score does not prove the model is fair, robust, or useful in deployment. Document data provenance, assumptions, and known limitations. Practice by training a small classifier with a pipeline and compare its evaluation to a baseline that predicts the majority class.
4. Syntax reference
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
model = make_pipeline(StandardScaler(), LogisticRegression())
model.fit(X_train, y_train)
print(model.score(X_test, y_test))5. Guided walkthrough
Separate features from the target, split data before fitting transformations, and build preprocessing and model steps into a pipeline. Evaluate on held-out data that the model did not use during training, and compare against a simple baseline.
6. Common mistakes to avoid
- Fitting preprocessing on the full dataset before the train/test split, causing data leakage.
- Reporting accuracy alone when class imbalance or unequal error costs make it misleading.
Visual guide
Evaluate a model
-
Step 01
Split
Separate training and held-out test data.
-
Step 02
Fit
Train preprocessing and model on training data.
-
Step 03
Evaluate
Measure held-out results against a baseline.
Code examples
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=7)
model = make_pipeline(StandardScaler(), LogisticRegression())
model.fit(X_train, y_train)
print(model.score(X_test, y_test))
A held-out score between 0 and 1; its value depends on the dataset.
from sklearn.model_selection import train_test_split
X = [[1], [2], [3], [4], [5]]
y = [0, 0, 0, 1, 1]
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.4, random_state=42, stratify=y
)
print(len(X_train), len(X_test))
3 2
from sklearn.dummy import DummyClassifier
model = DummyClassifier(strategy='most_frequent')
model.fit(X_train, y_train)
print(model.score(X_test, y_test))
A baseline score between 0.0 and 1.0.
Key points
- Build a baseline model and evaluate it without leaking test information.
- Prefer explicit, testable behavior over hidden side effects.
- Validate assumptions at system boundaries and handle failure deliberately.
Check your understanding
Practice
Your learning progress