Skip to content
mentoor

Topic 5 of 5 · 5 min read

Linear and logistic regression baselines

Goal: Train a logistic regression baseline in scikit-learn and report its validation score as the bar later models must beat.

Fit a simple model first, record its validation score, and make every later model, including an LLM prompt, beat that number. A baseline turns "this feels accurate" into "this is 6 points better than the cheap option".

Check yourself · 1 of 3

Logistic regression is used for which kind of task?

Choose one answer

Why it matters

Without a baseline you cannot tell whether a complicated pipeline adds anything. Teams spend weeks on gradient boosting or an LLM classifier and discover that logistic regression on five columns scored almost the same, trains in a second and can be explained. A baseline also catches data bugs early: if a linear model scores at chance, the features probably carry no signal, or the label is wrong.

The two baselines

  • Linear regression predicts a number as a weighted sum of the features. Use it for regression tasks and score with mean absolute error or R squared.
  • Logistic regression predicts the probability of a class, then thresholds it. Despite the name it is a classifier. Score with accuracy, precision, recall or F1, depending on the cost of each error type.

Also keep a dumb reference: always predict the majority class (DummyClassifier) or the mean (DummyRegressor). If your baseline does not beat the dummy, nothing is learned yet. On data with 90 percent negatives, 90 percent accuracy is what the dummy gets for free.

How to do it

from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import f1_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

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

print("dummy F1   ", f1_score(y_val, dummy.predict(X_val)))
print("logreg F1  ", f1_score(y_val, base.predict(X_val)))

Write the score, the split, the metric and the date in your notes. That line is the bar. When you try a random forest or an LLM few-shot prompt on the same validation rows with the same metric, compare against it.

Before and after

Before: "The LLM classifier gets 88 percent right." After: "On the same 500 validation tickets, the majority-class dummy scores 71 percent, logistic regression on TF-IDF scores 84 percent and the LLM prompt scores 88 percent, at a hundred times the cost per call." Now the team can decide whether four points are worth it.

What goes wrong

  • Different data for each model. The baseline is scored on one split and the new model on another, so the comparison means nothing.
  • Wrong metric. Accuracy on a rare-class problem rewards the dummy.
  • Skipping the baseline because the simple model feels beneath the project, then having no way to justify the expensive one.
  • Unscaled features. Large-range columns slow the solver and distort regularization. Put the scaler in the pipeline.

Examples

Baseline table for a ticket router

Model Validation accuracy (500 tickets) Cost per 1,000 predictions
Majority class 0.71 none
Logistic regression on TF-IDF 0.84 fraction of a cent
LLM few-shot prompt 0.88 highest

All three were scored on the same 500 held-out tickets. The decision now rests on whether four points are worth the cost.

Baseline as a bug detector

A churn model built on 12 columns scores 0.50 F1 with logistic regression, no better than the dummy. Before trying a bigger model, the team checks the label and finds the churn flag was joined to the wrong month. Fixing the join lifts the baseline to 0.68.

Key points

  • Fit a simple model and a dummy model first and record their validation scores as the bar to beat.
  • Linear regression predicts numbers; logistic regression predicts class probabilities and is used for classification.
  • If a model does not beat the dummy (majority class or mean), it has not learned anything useful yet.
  • Compare models on the same split with the same metric, or the comparison is meaningless.
  • The same baseline discipline applies when the challenger is an LLM prompt instead of a trained model.

Common mistakes

  • Scoring the baseline and the challenger on different splits or with different metrics.
  • Reporting accuracy on a heavily imbalanced label, where the majority-class dummy already scores high.
  • Skipping the dummy model, so there is no floor to compare against.
  • Treating the baseline as throwaway and not recording its score.

Check yourself · 2 of 3

On a dataset where 92 percent of rows are negative, a model scores 0.92 accuracy. A DummyClassifier(most_frequent) scores the same. What does this tell you?

Choose one answer

Check yourself · 3 of 3

A team wants to try an LLM to classify support tickets. What should they do first?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic