Text Classification
The discipline behind 'this email → that bucket'
Tickets, emails, reviews and support chats all become classifiers. From TF-IDF + linear models to embeddings to transformers, text classification pipelines share evaluation discipline that decides who wins.
What you will learn
- Build an end-to-end text classification pipeline
- Compare TF-IDF-linear vs embedding vs transformer approaches
- Evaluate multi-class text fairly (macro metrics, per-class confusion)
What is this?
Text classification assigns a label to a document: spam/not, ticket category, language, sentiment polarity. The pipeline is: clean → vectorize (TF-IDF, embeddings, or a pretrained transformer) → fit a classifier (logistic, SVM, GBDT, or fine-tuned transformer) → evaluate with class-aware metrics. Complexity escalates with data budget — linear TF-IDF models often beat fancier nets on small or noisy datasets, while transformers dominate when corpus and labels are large.
Why do you need it?
- It automates ticketing, moderation, triage and discovery — high-business-value, low-ceremony ML.
- A correct classification pipeline transfers to spam, intent, language and sentiment domains.
- Comparing approaches honestly (baseline-first) stops teams from overspending on models.
- Per-class errors are operational gold: misrouted support tickets cost dollars, tracked as confusion.
Mental model
A mailroom clerk sorting envelopes by department
Text classifier
The clerk uses cues — dept, header, sender, keywords — to bucket each envelope. A classifier learns which cues matter and (crucially) which sorts it gets wrong, so the mailroom can fix the miss-routing where money leaks.
How it works
- Build the label-quality check first: duplicate texts, mislabeled classes, tiny minority classes = the real problem.
- Vectorize: TF-IDF baseline → optional embeddings → transformer embeddings (frozen or fine-tuned).
- Fit on stratified train, tune with stratified CV; optimize macro-F1 (each class equal) unless frequency-weighted matters.
- Evaluate the confusion matrix per class and by slice (short/long text, language, channel).
- Ship the simplest model that clears the bar; revisit as data grows.
Classification stack from tokens to labels.
In production
- A support platform routes 80k tickets/day with a TF-IDF logistic model — explainable, <50ms, retrained nightly.
- A moderation system fine-tunes a small transformer for 'offensive' detection with macro-recall gates.
- An email triage product classifies intent (inquiry/complaint/order) and feeds downstream automation with its confidence.
Best practices
- Always start at the linear TF-IDF baseline — it sets the bar transformers must genuinely beat.
- Class-stratified splits + macro-averaged scores unless the cost truly is frequency-weighted.
- Log prediction confidence; a threshold on confidence is often better than a fixed label for routing.
- Recheck label quality before blaming the model — 5% label noise caps any accuracy.
Common mistakes
- Judging on accuracy when classes are imbalanced (a routing model with 90% 'general' class).
- Using an unbalanced test and trusting '91%' reported on the majority class.
- Fine-tuning the transformer on 200 labeled examples and calling it superior to TF-IDF.
- Deploying a model whose confidence says 'unsure' but rendering it as a hard label anyway.
Common issues
Model robust on trains, spams on short live messagesLength/tone distribution differs between train and live
Stratify by length/channel at train time and monitor score-per-slice online.
Confidence says 0.99 but routing gets it wrong dailyMiscalibrated model or the 'confident' atom pattern never seen in train
Calibrate (Platt isotonic) and add a known-unknown audit bucket for low-coverage patterns.
Debugging tips
- Print top-coefficient words per class for the linear model — a suspicious word means a labeled-noise leak.
- Slice confusion by text length; short texts may under-represent signal entirely.
- If a rare class is never predicted, its training share is too small — hard-sample or rebalance.
- Run everything on a 300-sample subset first; iterate on speed before burning the full corpus.
Interview checkpoint
Hands-on lab
- Build a TF-IDF + logistic ticket classifier
- Evaluate with macro-F1 and per-class confusion
- Raise it to an embedding feature model and compare honestly
Route Tickets With a Baseline That Matters
30 min · intermediate
Quick quiz
For an imbalanced routing problem, the primary metric should be:
A TF-IDF linear model is never a reasonable baseline against transformers.