Stop 08 · Supervised Learning: ClassificationTopics · 5/9

Multiclass & Multilabel Classification

Part of the Machine Learning Roadmap

In short

Real problems are rarely just yes or no. A support ticket goes to one of six teams. A movie can be comedy, romance and drama at once. Knowing which kind of problem you have decides your model's output layer, loss and metrics.

Multiclass problems pick one label from three or more classes, while multilabel problems can assign several labels at once, like tagging a movie as both comedy and romance. Models handle them with softmax, one-vs-rest or separate binary outputs.

The jaadu sorts clothes into three separate baskets at a riverside dhobi ghat, while one shirt nearby hangs with several colourful clips attached to it.

Think of it like this

Sorting laundry vs tagging clothes

At the dhobi ghat, each shirt goes into exactly one pile: whites, colours or delicates. That's multiclass: one label per item. Now a shop is tagging the same shirts for its website: 'cotton', 'formal', 'summer'. A shirt can get several tags or none. That's multilabel.

Watch it happen

One label or many?

Multiclass

  • Exactly one class per example, from three or more.
  • Ticket goes to Billing, Delivery or Technical.
  • Softmax output: probabilities sum to 1. Pick the biggest.

Multilabel

  • Zero, one or many labels per example.
  • A movie tagged Comedy and Romance.
  • One sigmoid per label: each is an independent yes/no above its own threshold.

Ask: can two labels be true at the same time? If yes, it's multilabel. If exactly one is always true, it's multiclass.

The big ideas

What you need to know

One-vs-rest

Train one binary classifier per class ('Billing or not?'), then pick the most confident. Works with any binary model.

Softmax for multiclass

Logistic regression and neural networks can output one probability per class that sums to 1.

Labels as a 0/1 matrix

For multilabel, y becomes one column per label. MultiLabelBinarizer builds it from lists of tags.

Average your metrics

Macro-average treats every class equally, micro-average counts every prediction equally. Report per-class scores too.

See it in code

Walk through the code

python
1from sklearn.feature_extraction.text import TfidfVectorizer2from sklearn.linear_model import LogisticRegression3from sklearn.multiclass import OneVsRestClassifier4from sklearn.pipeline import make_pipeline5from sklearn.preprocessing import MultiLabelBinarizer6 7# Multiclass: each ticket goes to exactly one team8tickets = ["refund not received", "parcel is late", "app keeps crashing", "charged twice"]9team = ["billing", "delivery", "technical", "billing"]10mc = make_pipeline(TfidfVectorizer(), LogisticRegression(max_iter=1000))11mc.fit(tickets, team)12print(mc.predict(["payment deducted but order failed"]))13 14# Multilabel: each movie can have several genres15plots = ["two friends fall in love on a road trip", "a heist goes hilariously wrong",16         "a family drama with a love story", "a funny wedding goes wrong"]17genres = [["romance"], ["comedy", "crime"], ["drama", "romance"], ["comedy", "romance"]]18mlb = MultiLabelBinarizer()19Y = mlb.fit_transform(genres)              # shape (4, 4): one 0/1 column per genre20 21ml = make_pipeline(TfidfVectorizer(), OneVsRestClassifier(LogisticRegression()))22ml.fit(plots, Y)23print(mlb.inverse_transform(ml.predict(["a funny love story"])))

1/4Multiclass: y is a single column of team names. LogisticRegression handles several classes with softmax.

The same tools handle both, but the shape of y and the meaning of the output differ.

Save yourself the pain

Common mistakes

Oops

Using softmax for a multilabel problem.

Do this instead

Softmax forces probabilities to compete and sum to 1. Use independent sigmoids, one per label.

Oops

Reporting only overall accuracy on imbalanced classes.

Do this instead

Show a confusion matrix and per-class precision and recall. A rare class can be completely ignored while accuracy looks fine.

Your mission

Classify news headlines

~30 min

Quick check

An app tags each photo with any of: food, people, outdoors, pets. What kind of problem is this?

Go deeper

  1. 1Train a multiclass classifier on the Iris or digits dataset and read the per-class report.
  2. 2Use MultiLabelBinarizer and OneVsRestClassifier on a small tagging dataset.
  3. 3Compare macro and weighted F1 for an imbalanced multiclass problem.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge