Sub topic 2
Binary, Multiclass, Multilabel and Multioutput: Reading the Target Shape
This chapter assumes you know binary classification, logistic regression, and basic NumPy. The shape of the target array y tells you which problem you have. That choice then sets the output layer, the loss, and the metrics in later chapters.
The four problem types
Ask two questions about each example. How many target columns are there? How many values can each column take?
- Binary: one column, two possible values. Example: spam or not spam.
- Multiclass: one column, three or more possible values, and exactly one is true. Example: an iris species.
- Multilabel: several columns, each 0 or 1, and any subset can be true. Example: a photo tagged
beach,dogandsunsetat once. - Multioutput: several columns, each its own prediction task. Columns can be multiclass, and they can even be continuous. Example: predicting a product's colour (3 options) and size (4 options).
Multilabel is a special case of multioutput where every column is binary.
| Problem | Shape of `y` | Values | Exclusive? |
|---|---|---|---|
| Binary | `(n,)` | 0 or 1 | Yes |
| Multiclass | `(n,)` | 0 to K-1 | Yes |
| Multilabel | `(n, K)` | 0 or 1 per column | No |
| Multioutput | `(n, M)` | Any per column | Not applicable |
Checking the shape in code
scikit-learn provides type_of_target, which reports how it will interpret your labels.
import numpy as np
from sklearn.utils.multiclass import type_of_target
examples = {
"binary": np.array([0, 1, 1, 0]),
"multiclass": np.array([0, 2, 1, 2]),
"multilabel": np.array([[1, 0, 1], [0, 1, 0], [1, 1, 0]]),
"multioutput": np.array([[0, 3], [1, 2], [2, 0]]),
}
for name, y in examples.items():
print(f"{name:12s} -> {type_of_target(y)}")The output is:
binary -> binary
multiclass -> multiclass
multilabel -> multilabel-indicator
multioutput -> multiclass-multioutputCommon errors
The most frequent mistake is treating a multilabel problem as multiclass. If an example can carry two tags, a multiclass model is forced to pick one. It then cannot predict the full set, whatever its training accuracy. Check whether any row has more than one positive column. Also check whether any row has none, since an all-zero row is valid in multilabel but impossible in multiclass.
The reverse mistake is encoding a multiclass target as several 0/1 columns and calling it multilabel. One-hot columns are mutually exclusive, so each row has exactly one 1. That is multiclass in a different encoding, and the next chapter covers it.
Another trap is class labels that happen to be integers. A column with values 0, 1, 2 is categorical. Do not pass it to a regressor, because the order carries no meaning.
Exercise
A support-ticket system assigns each ticket a priority (low, medium, high) and also a set of topic tags (billing, login, bug, and others), where a ticket can have several tags. What problem type is the priority alone, the tags alone, and the two together?
Show answer
The priority alone is multiclass, because exactly one of three values is true. The tags alone are multilabel, because any subset can apply. Together they form a multioutput problem: one multiclass column plus several binary columns.