Bird
Raised Fist0
ML Pythonml~8 mins

Multi-label classification in ML Python - Model Metrics & Evaluation

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Metrics & Evaluation - Multi-label classification
Which metric matters for Multi-label classification and WHY

In multi-label classification, each example can have many correct labels at once. So, we need metrics that check how well the model predicts all these labels together.

Common metrics are:

  • Hamming Loss: Measures how many labels are wrong on average. Lower is better.
  • Subset Accuracy: Checks if all predicted labels exactly match the true labels. Very strict.
  • Precision, Recall, and F1-score (micro and macro averaged): These show how well the model finds correct labels (Recall), avoids wrong labels (Precision), and balances both (F1).

We use these because simple accuracy doesn't work well when multiple labels can be true or false independently.

Confusion matrix or equivalent visualization

For multi-label, confusion matrices are made per label. For example, for label A:

      | Predicted Yes | Predicted No |
      |--------------|--------------|
      | True Pos (TP) | False Neg (FN)|
      | False Pos (FP)| True Neg (TN) |
    

We calculate TP, FP, TN, FN for each label separately, then combine results for overall metrics.

Example for 3 labels (A, B, C) with 4 samples:

    Samples:       Label A  Label B  Label C
    True labels:    1        0        1
                   0        1        0
                   1        1        0
                   0        0        1
    Predicted:     1        0        0
                   0        1        1
                   1        0        0
                   0        0        1
    

We count TP, FP, TN, FN for each label and then compute metrics.

Precision vs Recall tradeoff with concrete examples

In multi-label tasks, precision means how many predicted labels are actually correct. Recall means how many true labels the model found.

Example: A music app tags songs with genres. If the model predicts many genres per song (high recall), it may include wrong ones (low precision). If it predicts fewer genres (high precision), it might miss some true genres (low recall).

Depending on the goal, you choose:

  • High precision: Avoid wrong tags, good for user trust.
  • High recall: Find all possible tags, good for discovery.

F1-score balances both.

What "good" vs "bad" metric values look like for multi-label classification

Good:

  • Hamming Loss close to 0 (few wrong labels)
  • Subset Accuracy above 0.7 (exact matches often)
  • Precision, Recall, F1 above 0.8 (balanced and strong)

Bad:

  • Hamming Loss near 0.5 or higher (many wrong labels)
  • Subset Accuracy near 0 (rare exact matches)
  • Precision or Recall below 0.5 (poor label prediction)

Remember, subset accuracy is strict and often low, so focus on F1 and Hamming Loss for practical insight.

Common pitfalls in multi-label classification metrics
  • Ignoring label imbalance: Some labels appear rarely. Macro averaging treats all labels equally, micro averaging weights by frequency.
  • Using accuracy alone: It can be misleading because predicting no labels can give high accuracy if most labels are negative.
  • Data leakage: If test data leaks into training, metrics look falsely good.
  • Overfitting: Very high training metrics but low test metrics mean the model memorizes instead of generalizing.
Self-check question

Your multi-label model has 98% accuracy but only 12% recall averaged over labels. Is it good for production? Why or why not?

Answer: No, it is not good. The high accuracy likely comes from predicting mostly negative labels correctly (many labels are absent). The very low recall means the model misses most true labels, so it fails to find what it should. This hurts usefulness in real tasks.

Key Result
In multi-label classification, balanced metrics like F1-score and Hamming Loss best show model quality because simple accuracy can be misleading.

Practice

(1/5)
1. What is the main difference between multi-label classification and multi-class classification?
easy
A. Multi-label classification uses regression, multi-class uses classification.
B. Multi-label classification assigns only one label, multi-class assigns multiple labels.
C. Multi-label classification is used only for images, multi-class for text.
D. Multi-label classification assigns multiple labels to one example, multi-class assigns only one.

Solution

  1. Step 1: Understand multi-label classification

    Multi-label classification means each example can have more than one correct label at the same time.
  2. Step 2: Compare with multi-class classification

    Multi-class classification means each example can have only one label from many possible classes.
  3. Final Answer:

    Multi-label classification assigns multiple labels to one example, multi-class assigns only one. -> Option D
  4. Quick Check:

    Multi-label = multiple labels, multi-class = single label [OK]
Hint: Remember: multi-label means many labels per example [OK]
Common Mistakes:
  • Confusing multi-label with multi-class
  • Thinking multi-label assigns only one label
  • Mixing up classification with regression
  • Assuming multi-label is only for images
2. Which of the following is a correct way to represent labels for multi-label classification in Python?
easy
A. labels = [0, 1, 2]
B. labels = [[1, 0, 1], [0, 1, 0]]
C. labels = 'cat,dog,bird'
D. labels = 3

Solution

  1. Step 1: Understand label representation for multi-label

    Multi-label classification uses a list or array where each position represents a label, with 1 or 0 indicating presence or absence.
  2. Step 2: Check options for correct format

    labels = [[1, 0, 1], [0, 1, 0]] shows a list of lists with 1s and 0s, correctly representing multiple labels per example.
  3. Final Answer:

    labels = [[1, 0, 1], [0, 1, 0]] -> Option B
  4. Quick Check:

    Multi-label uses binary vectors per example [OK]
Hint: Use binary lists to show multiple labels [OK]
Common Mistakes:
  • Using a single integer for labels
  • Using a string instead of list
  • Using a flat list without nested structure
  • Confusing multi-class label format with multi-label
3. Given this Python code snippet for multi-label classification predictions:
import numpy as np
preds = np.array([[0.8, 0.1, 0.6], [0.3, 0.7, 0.2]])
threshold = 0.5
binary_preds = (preds > threshold).astype(int)
print(binary_preds)

What is the output?
medium
A. [[1 1 1] [0 0 0]]
B. [[0 1 0] [1 0 1]]
C. [[1 0 1] [0 1 0]]
D. [[0 0 0] [1 1 1]]

Solution

  1. Step 1: Apply threshold to predictions

    Compare each value in preds with 0.5: values > 0.5 become 1, else 0.
  2. Step 2: Convert boolean to int and print

    First row: 0.8>0.5=1, 0.1>0.5=0, 0.6>0.5=1; Second row: 0.3>0.5=0, 0.7>0.5=1, 0.2>0.5=0.
  3. Final Answer:

    [[1 0 1] [0 1 0]] -> Option C
  4. Quick Check:

    Thresholding preds > 0.5 = binary labels [OK]
Hint: Compare each prediction to threshold for binary output [OK]
Common Mistakes:
  • Confusing > with >=
  • Not converting boolean to int
  • Mixing rows and columns in output
  • Using wrong threshold value
4. You trained a multi-label model but it always predicts only one label per example. What is the most likely cause?
medium
A. Using softmax activation instead of sigmoid in the output layer
B. Using sigmoid activation instead of softmax in the output layer
C. Using binary cross-entropy loss
D. Using a threshold of 0.1 for predictions

Solution

  1. Step 1: Understand output activations for multi-label

    Multi-label models use sigmoid activation to allow independent probabilities per label.
  2. Step 2: Identify problem with softmax

    Softmax forces probabilities to sum to 1, so only one label gets high probability, limiting multi-label predictions.
  3. Final Answer:

    Using softmax activation instead of sigmoid in the output layer -> Option A
  4. Quick Check:

    Softmax limits to one label, sigmoid allows many [OK]
Hint: Use sigmoid for multi-label, softmax for single-label [OK]
Common Mistakes:
  • Confusing softmax and sigmoid activations
  • Ignoring loss function compatibility
  • Setting threshold too low or high
  • Assuming threshold fixes activation issues
5. You have a dataset where each image can have multiple tags like 'beach', 'sunset', and 'people'. You want to build a multi-label classifier. Which metric is best to evaluate your model's performance?
hard
A. Precision, Recall, and F1-score calculated per label and averaged
B. Accuracy (percentage of exact matches of all labels)
C. Mean Squared Error
D. Confusion matrix for single-label classification

Solution

  1. Step 1: Understand evaluation needs for multi-label

    Exact match accuracy is too strict because all labels must match perfectly, which is rare.
  2. Step 2: Choose suitable metrics

    Precision, Recall, and F1-score per label, then averaged, give a balanced view of performance on each label.
  3. Final Answer:

    Precision, Recall, and F1-score calculated per label and averaged -> Option A
  4. Quick Check:

    Use per-label metrics averaged for multi-label evaluation [OK]
Hint: Use per-label precision/recall for multi-label metrics [OK]
Common Mistakes:
  • Using strict accuracy that ignores partial matches
  • Using regression metrics like MSE
  • Using single-label confusion matrix
  • Ignoring label imbalance in metrics