Implement an Error-Correcting Output Codes (ECOC) Classifier

Quick Overview

Implement a multiclass classifier built from error-correcting output codes, training one binary estimator per column of a ternary code matrix. It tests per-column training sets, input validation, and vectorized Hamming, linear and exponential decoding with deterministic tie-breaking.

Implement an Error-Correcting Output Codes (ECOC) Classifier

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: hard

Interview Round: Other

Implement `ECOCClassifier(code_matrix, estimator_factory, decoding="hamming")` over a ternary `code_matrix` (shape `(n_classes, n_columns)`, entries in {-1, 0, 1}). Row `i` is the codeword of `classes_[i]`, the i-th sorted unique label. `estimator_factory` is a zero-argument callable returning a fresh binary estimator with `fit`, `predict` and `decision_function`. The class must behave as follows: - `fit(X, y)` trains, for each column, a new estimator only on the samples whose class entry is nonzero (target `1` for +1, `0` for -1). It sets `classes_` and `estimators_` and returns `self`. - `fit` raises `ValueError` if the row count differs from the number of classes, an entry is outside {-1, 0, 1}, a column lacks a +1 or a -1, or `decoding` is unknown. - `decision_function(X)` stacks each estimator's scores column-wise. `predict_bits(X)` maps `est.predict(X) == 1` to +1 and anything else to -1. - `distances(X)` returns one distance per sample and class, shape `(n_samples, n_classes)`. With codeword $m$, bits $b$ and scores $s$, summed over columns $l$, `"hamming"` is $\sum_l (1 - m_l b_l)/2$, `"linear"` is $\sum_l -m_l s_l$, and `"exponential"` is $\sum_l \exp(-m_l s_l)$. - `predict(X)` returns `classes_[argmin distance]`, with ties going to the lowest class index. Example: ```python code = [[1, 1, 0], [-1, 1, 1], [-1, -1, -1]] y = [7, 3, 9, 7, 3, 9] # classes_ = [3, 7, 9] # column 2 trains only on classes 7 and 9 (class 3 has entry 0) # suppose the three estimators predict bits [-1, -1, 1] for a sample x # hamming distances: [2.5, 1.0, 1.0] -> predict 7 (tie, lowest index) ``` Class 3 pays 1 for each of its first two columns and 0.5 for its zero entry. Classes 7 and 9 each disagree on one bit, and the tie goes to 7, the lower index. ```hint Labels to rows Row `i` belongs to the i-th sorted label, so each training sample needs its row index in the code matrix. A sorted search over `classes_` gives all of them in one call. ``` ```hint Decoding without loops Every distance is a sum over columns of a function of the code entry and the column output. Broadcasting the codewords against the per-sample outputs gives all sample and class pairs at once. ``` ```hint Why the column check matters Think about what a column with no -1 entry would hand its estimator at training time. ``` ### Constraints & Assumptions - Labels can be any sortable values (the example uses 7, 3 and 9), and `classes_` comes from the sorted unique labels in `y`. - The code matrix has one row per class in `y`. - Estimators follow the scikit-learn `fit` / `predict` / `decision_function` API and are trained on 0/1 targets. ### What a Strong Answer Covers - Per-column training sets that drop zero-coded classes, with labels mapped to code rows through the sorted classes - The full set of `fit` checks, and why every column needs both a +1 and a -1 - Vectorized Hamming, linear and exponential decoding, with the right treatment of zero entries - Deterministic tie-breaking toward the lowest class index - Time and memory cost, including the size of the distance tensor ### Follow-up Questions - How would you design a good code matrix for 20 classes? - Why does loss-based decoding often beat Hamming decoding? - How would you produce class probabilities instead of hard labels?

Overview: Implement a multiclass classifier built from error-correcting output codes, training one binary estimator per column of a ternary code matrix. It tests per-column training sets, input validation, and vectorized Hamming, linear and exponential decoding with deterministic tie-breaking.

PracHub logo
General
Nov 25, 2026
hardMachine Learning EngineerOtherMachine Learning
0
0

Implement ECOCClassifier(code_matrix, estimator_factory, decoding="hamming") over a ternary code_matrix (shape (n_classes, n_columns), entries in {-1, 0, 1}). Row i is the codeword of classes_[i], the i-th sorted unique label. estimator_factory is a zero-argument callable returning a fresh binary estimator with fit, predict and decision_function.

The class must behave as follows:

  • fit(X, y) trains, for each column, a new estimator only on the samples whose class entry is nonzero (target 1 for +1, 0 for -1). It sets classes_ and estimators_ and returns self .
  • fit raises ValueError if the row count differs from the number of classes, an entry is outside {-1, 0, 1}, a column lacks a +1 or a -1, or decoding is unknown.
  • decision_function(X) stacks each estimator's scores column-wise. predict_bits(X) maps est.predict(X) == 1 to +1 and anything else to -1.
  • distances(X) returns one distance per sample and class, shape (n_samples, n_classes) . With codeword mm , bits bb and scores ss , summed over columns ll , "hamming" is ∑l(1−mlbl)/2\sum_l (1 - m_l b_l)/2 , "linear" is ∑l−mlsl\sum_l -m_l s_l , and "exponential" is ∑lexp⁡(−mlsl)\sum_l \exp(-m_l s_l) .
  • predict(X) returns classes_[argmin distance] , with ties going to the lowest class index.

Example:

code = [[1, 1, 0], [-1, 1, 1], [-1, -1, -1]]
y = [7, 3, 9, 7, 3, 9]          # classes_ = [3, 7, 9]
# column 2 trains only on classes 7 and 9 (class 3 has entry 0)
# suppose the three estimators predict bits [-1, -1, 1] for a sample x
# hamming distances: [2.5, 1.0, 1.0]  -> predict 7 (tie, lowest index)

Class 3 pays 1 for each of its first two columns and 0.5 for its zero entry. Classes 7 and 9 each disagree on one bit, and the tie goes to 7, the lower index.

Constraints & Assumptions

  • Labels can be any sortable values (the example uses 7, 3 and 9), and classes_ comes from the sorted unique labels in y .
  • The code matrix has one row per class in y .
  • Estimators follow the scikit-learn fit / predict / decision_function API and are trained on 0/1 targets.

What a Strong Answer Covers Guidance

  • Per-column training sets that drop zero-coded classes, with labels mapped to code rows through the sorted classes
  • The full set of fit checks, and why every column needs both a +1 and a -1
  • Vectorized Hamming, linear and exponential decoding, with the right treatment of zero entries
  • Deterministic tie-breaking toward the lowest class index
  • Time and memory cost, including the size of the distance tensor

Follow-up Questions Guidance

  • How would you design a good code matrix for 20 classes?
  • Why does loss-based decoding often beat Hamming decoding?
  • How would you produce class probabilities instead of hard labels?