Implement Multi-Label Classification Metrics: Hamming Loss, Subset Accuracy and F1

Quick Overview

Implement multi-label classification metrics on binary indicator matrices in NumPy: Hamming loss, subset accuracy, per-label F1, and macro and micro F1. Tests vectorized reductions over the right axis, input validation, the zero-denominator rule and how the averaging choices differ.

Implement Multi-Label Classification Metrics: Hamming Loss, Subset Accuracy and F1

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Given binary indicator matrices `y_true` and `y_pred` of shape `(n_samples, n_labels)`, implement `hamming_loss`, `subset_accuracy`, `per_label_f1`, `macro_f1` and `micro_f1`. Scalars return a Python `float` and `per_label_f1` returns a float array of shape `(n_labels,)`. - `hamming_loss`: fraction of wrong (sample, label) entries. - `subset_accuracy`: fraction of rows predicted exactly. - F1 = `2TP / (2TP + FP + FN)`, and `0.0` when the denominator is 0. - `macro_f1`: unweighted mean over all labels (zero-score labels included). `micro_f1`: F1 from TP, FP and FN pooled over all labels. For example: ```python y_true = [[1, 0, 1], [0, 1, 0], [1, 1, 0], [0, 0, 1]] y_pred = [[1, 0, 0], [0, 1, 0], [1, 0, 1], [0, 0, 1]] hamming_loss -> 0.25 subset_accuracy -> 0.5 per_label_f1 -> [1.0, 0.6667, 0.5] # 0.6667 is 2/3 macro_f1 -> 13/18 micro_f1 -> 8/11 ``` ```hint One grid, several axes Every F1 variant needs the same three counts. Think about which axis of the matrix each variant sums them over. ``` ```hint An empty label Picture a label column that is all zeros in both matrices. Check that your F1 returns 0.0 for it without a warning, and that it still counts toward the macro mean. ``` ### Constraints & Assumptions - Entries are 0/1 ints or booleans. - Non-2-D inputs, mismatched shapes, or empty inputs raise `ValueError`. ### What a Strong Answer Covers - Validation of dimensionality, shape and emptiness before any metric is computed - Hamming loss as an entry-level measure and subset accuracy as a row-level one, and why the two can disagree - Per-label TP, FP and FN from boolean masks summed over the right axis - The zero-denominator rule, with all-zero labels kept in the macro mean - Pooled counts for micro F1, and the return types (Python float for scalars, a float array per label) ### Follow-up Questions - When would you prefer micro over macro F1 in a multi-label setting? - How would you add sample-averaged and support-weighted F1? - How would you pick per-label thresholds if the model outputs probabilities?

Overview: Implement multi-label classification metrics on binary indicator matrices in NumPy: Hamming loss, subset accuracy, per-label F1, and macro and micro F1. Tests vectorized reductions over the right axis, input validation, the zero-denominator rule and how the averaging choices differ.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Given binary indicator matrices y_true and y_pred of shape (n_samples, n_labels), implement hamming_loss, subset_accuracy, per_label_f1, macro_f1 and micro_f1. Scalars return a Python float and per_label_f1 returns a float array of shape (n_labels,).

  • hamming_loss : fraction of wrong (sample, label) entries.
  • subset_accuracy : fraction of rows predicted exactly.
  • F1 = 2TP / (2TP + FP + FN) , and 0.0 when the denominator is 0.
  • macro_f1 : unweighted mean over all labels (zero-score labels included). micro_f1 : F1 from TP, FP and FN pooled over all labels.

For example:

y_true = [[1, 0, 1], [0, 1, 0], [1, 1, 0], [0, 0, 1]]
y_pred = [[1, 0, 0], [0, 1, 0], [1, 0, 1], [0, 0, 1]]

hamming_loss    -> 0.25
subset_accuracy -> 0.5
per_label_f1    -> [1.0, 0.6667, 0.5]   # 0.6667 is 2/3
macro_f1        -> 13/18
micro_f1        -> 8/11

Constraints & Assumptions

  • Entries are 0/1 ints or booleans.
  • Non-2-D inputs, mismatched shapes, or empty inputs raise ValueError .

What a Strong Answer Covers Guidance

  • Validation of dimensionality, shape and emptiness before any metric is computed
  • Hamming loss as an entry-level measure and subset accuracy as a row-level one, and why the two can disagree
  • Per-label TP, FP and FN from boolean masks summed over the right axis
  • The zero-denominator rule, with all-zero labels kept in the macro mean
  • Pooled counts for micro F1, and the return types (Python float for scalars, a float array per label)

Follow-up Questions Guidance

  • When would you prefer micro over macro F1 in a multi-label setting?
  • How would you add sample-averaged and support-weighted F1?
  • How would you pick per-label thresholds if the model outputs probabilities?