Sub topic 2
Choosing Metrics and Setting a Baseline
Assume you know Python, pandas, NumPy, and the meaning of train, validation, and test splits. You should also know the basic definitions of accuracy, precision, recall, and F1. This chapter focuses on choosing among them for an NLP task and creating a comparison point before improving a model.
Why accuracy is often insufficient
Accuracy is the fraction of predictions that are correct:
\text{accuracy}=\frac{\text{correct predictions}}{\text{all predictions}}It works well when classes have similar importance and similar frequencies. Text datasets often violate both assumptions. For example, if 95% of support messages are “general,” a model that always predicts “general” gets 95% accuracy while failing every important minority category.
For a class, precision measures how many predicted examples are actually that class. Recall measures how many true examples of that class the model finds. F1 is their harmonic mean.
| Metric | Main question | Prefer it when |
|---|---|---|
| Precision | When the model predicts this class, how often is it right? | False positives are costly |
| Recall | Of the real examples of this class, how many did it find? | False negatives are costly |
| F1 | How well are precision and recall balanced? | Both error types matter |
| Accuracy | How often is any prediction correct? | Classes and costs are reasonably balanced |
For multiclass classification, inspect per-class metrics instead of only one aggregate score. Macro F1 averages each class's F1 equally, so it exposes poor minority-class performance. Weighted F1 weights classes by their support, or number of true examples. Micro F1 pools all decisions and is often close to accuracy in single-label multiclass tasks.
Set a baseline before changing the model
A baseline is a simple, reproducible reference system. A useful baseline can be:
- The majority-class predictor.
- A rule based on obvious keywords.
- A TF-IDF representation with logistic regression.
- A previously deployed model.
The baseline answers an essential question: does a more complex model improve performance enough to justify its cost? Evaluate the baseline on exactly the same held-out data and with exactly the same preprocessing and metrics as later models.
from sklearn.dummy import DummyClassifier
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
texts = [
"delivery was late", "package arrived early", "shipping was slow",
"the app crashes", "cannot log in", "button does not work",
"great product", "I love this", "excellent quality",
"delivery took too long", "tracking stopped", "where is my order",
"screen freezes", "password reset fails", "error on checkout",
]
labels = [
"shipping", "shipping", "shipping",
"technical", "technical", "technical",
"praise", "praise", "praise",
"shipping", "shipping", "shipping",
"technical", "technical", "technical",
]
x_train, x_test, y_train, y_test = train_test_split(
texts, labels, test_size=0.4, random_state=7, stratify=labels
)
baseline = DummyClassifier(strategy="most_frequent")
baseline.fit(x_train, y_train)
print("Baseline")
print(classification_report(y_test, baseline.predict(x_test), zero_division=0))
model = make_pipeline(
TfidfVectorizer(ngram_range=(1, 2)),
LogisticRegression(max_iter=1000),
)
model.fit(x_train, y_train)
print("TF-IDF model")
print(classification_report(y_test, model.predict(x_test), zero_division=0))The zero_division=0 argument makes undefined precision or recall explicit instead of producing warnings. This can happen when a model never predicts a class.
Match the metric to the product risk
Write down which mistakes matter. In toxic-content detection, missing harmful text may be worse than reviewing some harmless text, so recall can be the priority. In automatic email routing, sending a message to the wrong department may be worse than leaving it for manual review, so precision may matter more.
Keep secondary metrics too. A single primary metric supports ranking, while per-class precision, recall, support, and confusion patterns explain what the ranking hides.
Exercise
A three-class dataset has 90% “neutral” examples. A majority baseline scores 90% accuracy but has zero recall for “positive” and “negative.” Which metric should you inspect first if all classes matter equally?
Show answer
Macro F1, followed by per-class precision, recall, and support. Macro F1 gives the minority classes equal weight.