Sub topic 1
Why Every Project Starts with a Baseline
Prerequisites
This chapter assumes you know the following:
- Basic supervised learning: features, labels, training and prediction.
- Common metrics such as accuracy, precision, recall and mean absolute error.
- Basic Python and scikit-learn usage.
- Mean, standard deviation and standard error.
What a baseline is
A baseline is a simple reference method that you score before you build anything complex. It answers one question: how well can we do with almost no effort? Every later model is judged against that number.
A score alone says little. An accuracy of 0.95 sounds strong, but it may be what a model gets by always predicting the most common class. A baseline gives the score a meaning.
Why it matters
A baseline does four jobs:
- It sets a floor. A model that cannot beat a trivial predictor has learned nothing useful.
- It exposes pipeline bugs. If a complex model scores worse than a constant guess, the data handling or the metric is probably wrong.
- It shows the size of the gain. You can report "the model cuts error by 30% compared with the baseline" instead of a bare number.
- It supports the decision to stop. Later chapters build on this: you can only ask whether a model is good enough if you know what the cheap option achieves.
A worked example
The next example builds a trivial classifier on imbalanced data. About 95% of the labels are in one class. The baseline always predicts that class.
from sklearn.datasets import make_classification
from sklearn.dummy import DummyClassifier
from sklearn.metrics import accuracy_score, balanced_accuracy_score
from sklearn.model_selection import train_test_split
X, y = make_classification(n_samples=2000, weights=[0.95, 0.05], random_state=0)
X_train, X_test, y_train, y_test = train_test_split(
X, y, stratify=y, random_state=0
)
baseline = DummyClassifier(strategy="most_frequent").fit(X_train, y_train)
pred = baseline.predict(X_test)
print("accuracy:", accuracy_score(y_test, pred))
print("balanced accuracy:", balanced_accuracy_score(y_test, pred))Accuracy comes out near 0.95, but balanced accuracy is exactly 0.5. Balanced accuracy is the average recall over the classes. The baseline finds every majority example and no minority example, so it scores (1.0 + 0.0) / 2.
The point is not that accuracy is always bad. The point is that the metric must be chosen before anyone calls a score good. A model with 0.94 accuracy would look worse than this baseline on accuracy, yet it might be far more useful if it finds the rare class.
Where baselines fit in a project
The baseline comes early, right after you define the goal and the metric. The order below shows how it feeds later decisions.
Kinds of baseline
Baselines range from trivial to strong. You will build each kind in later chapters.
| Kind | Example | Typical use |
|---|---|---|
| Trivial | Majority class, mean, median, previous value | The floor every model must beat |
| Heuristic | A hand-written rule, or the current production system | Shows what users already get today |
| Simple model | Logistic regression, linear regression, shallow tree | A strong, cheap, easy-to-explain reference |
Common errors
- Choosing the metric after seeing results. This lets you pick whichever number looks best.
- Scoring the baseline and the candidate on different splits. The difference may then come from the data, not the model.
- Skipping the baseline because the problem seems obvious. Obvious problems often have strong simple solutions.
- Using a weak baseline on purpose. A deliberately poor reference makes any model look good.
Exercise
A fraud dataset has 1% fraud. Your first model has 99% accuracy. Why is this not yet evidence of a good model, and what two things would you do next?
Show answer
Always predicting "not fraud" also gets about 99% accuracy, so the score matches a trivial baseline and shows nothing. First, build that majority-class baseline and score it on the same split. Second, switch to a metric that reflects the goal, such as recall on the fraud class, precision, or balanced accuracy, and compare both models on it.
Exercise
Your candidate regression model has a mean absolute error of 4.2. What do you need before you can say whether that is good?
Show answer
You need baseline errors on the same held-out split and metric, for example a median predictor and a plain linear regression. You also need to know what error the task requires. The number 4.2 means nothing alone.