NLP
Intermediate 15 min read NLP

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.

text classificationtopic modelmulticlasssentimentpipelinebaselines
Before we start

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)
Introduction

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.

Motivation

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.
Think about it

Mental model

Imagine

A mailroom clerk sorting envelopes by department

Think of

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.

Mechanics

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.

Text
Features
Model
Metrics
Route
Real world

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.
Guidelines

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.
Watch out

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.
Root cause analysis

Common issues

Error
Model robust on trains, spams on short live messages
Root cause

Length/tone distribution differs between train and live

Fix

Stratify by length/channel at train time and monitor score-per-slice online.

Error
Confidence says 0.99 but routing gets it wrong daily
Root cause

Miscalibrated model or the 'confident' atom pattern never seen in train

Fix

Calibrate (Platt isotonic) and add a known-unknown audit bucket for low-coverage patterns.

When things break

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.
Interviews

Interview checkpoint

Practice

Hands-on lab

Route Tickets With a Baseline That Matters
30 min · intermediate
In progress
Objectives
  • 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

0/4 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

For an imbalanced routing problem, the primary metric should be:

2

A TF-IDF linear model is never a reasonable baseline against transformers.

Go deeper

Official documentation