Is 95% Good?
A model that detects faulty parts on a production line is right 95% of the time. That sounds impressive until you learn that 95% of the parts are fine. A "model" that declares every part fine, without looking at anything, is also right 95% of the time, and it never finds a single faulty part.
A score means nothing on its own. It needs a comparison: what would a trivial approach achieve on the same data? That comparison is a baseline. Measuring it first, before any real model, tells you what "better" has to mean.
What a Baseline Is
A baseline ignores the features entirely and makes the same simple guess for every sample:
- For classification, guess the most common class from the training data.
- For regression, predicting a number, guess the average of the training labels.
scikit-learn provides both as dummy models, which follow the usual fit
and predict pattern:
DummyClassifier()uses the strategy"prior"by default. It learns how often each class appears in the training data and always predicts the most frequent one.DummyRegressor()uses the strategy"mean"by default and always predicts the average training label.
These are not models anyone would use. They are the floor a real model must clear: if a model cannot beat its baseline, it has not learned anything useful from the features.
Accuracy and Imbalanced Classes
Accuracy is the share of predictions that are correct: correct
predictions divided by all predictions, often written as a decimal, so 0.95
means 95%. It is the default score of every scikit-learn classifier's score
method.
Accuracy becomes misleading when one class is much rarer than the other, a situation called imbalanced classes. Fraud, faulty parts, rare diseases and late deliveries are all usually rare. On such data the majority-class baseline reaches high accuracy while finding none of the rare cases, which are usually the ones you care about. The metrics lesson introduces measures, including recall, the share of real positive cases a model finds, that expose this.
Exercise: Measure Baselines First
You will measure a classification baseline on imbalanced generated data and a regression baseline on generated numbers.
Starting state: the course environment, active in your terminal. The script generates its own data.
-
Create the exercise folder. macOS and Linux:
mkdir -p ~/ai-practice/baseline-first cd ~/ai-practice/baseline-firstWindows PowerShell:
mkdir $HOME\ai-practice\baseline-first cd $HOME\ai-practice\baseline-firstSave this as
baseline_first.pyin that folder:"""Measure baselines for a classification task and a regression task.""" from sklearn.datasets import make_classification, make_regression from sklearn.dummy import DummyClassifier, DummyRegressor from sklearn.model_selection import train_test_split # Classification: about 95% of cases are negative (0), 5% positive (1). X, y = make_classification(n_samples=1000, weights=[0.95], random_state=2) X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=2, stratify=y) baseline = DummyClassifier().fit(X_train, y_train) predictions = baseline.predict(X_test) print("Test cases:", len(y_test), "| positive:", y_test.sum()) print("Baseline accuracy:", round(baseline.score(X_test, y_test), 3)) print("Positive cases found:", ((predictions == 1) & (y_test == 1)).sum()) # Regression: predict a number. X, y = make_regression(n_samples=300, n_features=3, noise=15, random_state=3) X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=3) mean_baseline = DummyRegressor().fit(X_train, y_train) print("Training mean:", round(y_train.mean(), 1)) print("Baseline predictions:", mean_baseline.predict(X_test[:3]).round(1)) -
Predict the baseline's accuracy, then run it:
python baseline_first.pyOutput, which should match exactly:
Test cases: 250 | positive: 13 Baseline accuracy: 0.948 Positive cases found: 0 Training mean: 1.7 Baseline predictions: [1.7 1.7 1.7]
How the Script Works
Imbalanced data. make_classification(n_samples=1000, weights=[0.95])
generates 1,000 samples in two classes, with about 95% in class 0. Class 1 is
the rare "positive" class, such as a faulty part. The stratified split keeps
that balance in the 250 test samples, of which 13 are positive.
The classification baseline. DummyClassifier() is fitted on the training
labels only; it learns that class 0 is the most frequent. Its predictions are
therefore 0 for all 250 test samples. baseline.score returns accuracy:
237 of 250 correct, 0.948. The last classification line counts the samples
that are both predicted positive and really positive. (predictions == 1)
and (y_test == 1) each give True or False per sample, & keeps the
samples where both are True, and .sum() counts them: none.
The regression baseline. make_regression generates 300 samples whose
label is a number, with noise=15 adding random variation. DummyRegressor()
learns the training labels' average, 1.7, and predict returns 1.7 for
every sample, as the first three predictions show. The regression lesson
measures how far such predictions are from the truth.
If you changed weights=[0.95] to weights=[0.99], the baseline's accuracy
would rise towards 0.99, while it still found no positive case: a better
score for an equally useless model.
Success: you can state the classification baseline's accuracy, explain why it finds no positive cases, and say what the regression baseline predicts.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
| Different counts or accuracy | A random_state or weights value was changed | Restore the values shown |
Positive cases found: followed by a number above 0 | The DummyClassifier was given a strategy other than the default | Use DummyClassifier() with no arguments |
NameError: name 'X_train' is not defined | The split line was not pasted | Paste the whole script again |
ModuleNotFoundError | The course environment is not active | Activate it and rerun |
-
Clean up. macOS and Linux:
cd ~/ai-practice ls rm -rf baseline-first lsWindows PowerShell:
cd $HOME\ai-practice ls Remove-Item -Recurse baseline-first lsCheck that the first
lsshowsbaseline-firstbefore removing it. Do the independent task first.
Independent task: change weights=[0.95] to weights=[0.5], which
generates two equally common classes. Predict the baseline's accuracy before
running the script, then check.
Check Your Understanding
- A model detecting a rare disease reports 95% accuracy. Why is that not enough to judge it, and what should be reported beside it?
- A new model scores 0.946 accuracy on the faulty-parts test data from the exercise. Is it better than the baseline?
Answer and Reasoning
Independent task
The baseline's accuracy falls to 0.5, and the test set holds 125 positive
cases out of 250. With two equally common classes, always guessing one class is
right half the time. The same baseline method gives 0.948 or 0.5 depending only
on the class balance, which is why a model's score must always be compared with
its own baseline on the same data.
Understanding check
- If 95% of patients do not have the disease, a model that never predicts it also scores 95%. Report the baseline's score on the same data beside it, and a measure of how many real cases the model finds, as the metrics lesson shows.
- No. It is slightly below the baseline's 0.948, so on accuracy it has not beaten the trivial guess. It might still find some faulty parts, which accuracy alone does not show.