Skip to chapter content
navan✳THE BUILD LABMACHINE LEARNING / GUIDE
0/28 read
COOKBOOK 01 / 08

A classifier with an honest baseline.

Split a built-in dataset, compare a simple baseline, then read the classifier's result.

BY NAVAN SAMICOMPLETE EXAMPLEPYTHON 3
01 / THE PURPOSE

Why this workflow exists.

This example predicts a flower category from four numeric measurements. It keeps one quarter of the rows out of fitting, so the final comparison uses examples the learner did not see. A baseline that always chooses the most common training class gives the model score context. The script is intentionally complete but small: it shows the sequence of decisions, not a claim that flower classification is a difficult production problem.

The same training and test rows are used for both baseline and candidate. The split is stratified so each category remains represented. StandardScaler belongs inside the classifier pipeline, where it learns statistics from training rows only. The code prints both accuracy and a confusion matrix so the reader can see whether mistakes concentrate in one category.

Code context. Python 3 with scikit-learn installed. The dataset is bundled with scikit-learn; no download or account is needed.

02 / COMPLETE PYTHON

The whole example.

first-classifier.py
from sklearn.datasets import load_iris
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, stratify=y, random_state=7
)

baseline = DummyClassifier(strategy="most_frequent")
baseline.fit(X_train, y_train)

model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
model.fit(X_train, y_train)
predictions = model.predict(X_test)

print("Baseline accuracy:", accuracy_score(y_test, baseline.predict(X_test)))
print("Model accuracy:", accuracy_score(y_test, predictions))
print("Confusion matrix:\n", confusion_matrix(y_test, predictions))
03 / READ THE FLOW

What each stage does.

load_iris returns X and y with aligned rows. train_test_split makes the holdout. The baseline fits only the target prevalence; it ignores measurements. The candidate pipeline fits a scaler and a logistic classifier in that order. fit changes both objects' learned state, while predict only applies that state to held-out feature rows.

accuracy_score counts correct labels divided by total labels. confusion_matrix lays out which true categories were predicted as which categories. A model score higher than the baseline is evidence of useful signal on this particular holdout, but the small built-in dataset does not establish performance on every flower image or a new collection process.

04 / THE RESULT

What the output means.

The classifier should exceed the most-frequent baseline on this dataset. Exact displayed scores are tied to the bundled data, split, and installed library version.

05 / THE BOUNDARY

Where the example stops.

Model choice and settings were fixed before the test comparison. A larger selection search would need validation inside the training portion and a still-untouched final test.

Built with curiosity. By Navan Sami. ↗Machine learning with Python · From examples to trustworthy models.