Implement the Frank and Hall ordinal classifier from K-1 cumulative binary models

Quick Overview

Implement the Frank and Hall approach to ordinal classification: fit K-1 binary models for P(y > threshold), convert the cumulative probabilities into per-class probabilities, and predict with a lowest-class tie rule. It tests NumPy vectorization, scikit-learn style estimator code, and handling non-monotone outputs from independently trained models.

Implement the Frank and Hall ordinal classifier from K-1 cumulative binary models

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `cumulative_to_class_proba(cum)` and a `FrankHallClassifier` class. For K sorted ordinal classes, train K-1 binary models where model `m` predicts P(y > `classes_[m]`). Convert these cumulative probabilities into per-class probabilities and predictions. Requirements: - `cumulative_to_class_proba`: input shape (n, K-1). Take differences of `[1, c_0, ..., c_{K-2}, 0]`, clip negatives to 0, and renormalise each row to sum to 1. Non-2-D input raises `ValueError`. - `fit`: set `classes_` (sorted unique labels) and `estimators_` (K-1 models, model `m` fit on `(y > classes_[m]).astype(int)`). Fewer than 2 classes raises `ValueError`. Returns `self`. - Each model comes from `base_estimator_factory()`. When the factory is `None`, use `LogisticRegression(random_state=0)`. - `cumulative_proba(X)`: shape (n, K-1), where column `m` is `estimators_[m].predict_proba(X)[:, 1]`. `predict_proba` applies the conversion above. - `predict` returns the label in `classes_` with the highest probability. Ties go to the lowest class. Example: ```python cumulative_to_class_proba([[0.8, 0.3]]) # -> [[0.2, 0.5, 0.3]] cumulative_to_class_proba([[0.2, 0.9, 0.4]]) # raw diffs [0.8, -0.7, 0.5, 0.4] -> clip -> [0.8, 0, 0.5, 0.4] # -> [[0.8, 0.0, 0.5, 0.4]] / 1.7 ``` ```hint Vectorize the conversion Build the padded matrix once per call and subtract shifted column slices of it, so there is no Python loop over rows or classes. ``` ```hint Check the edge behavior Look up which index `np.argmax` returns when two entries tie, and work out whether a clipped row can ever sum to zero before you divide by it. ``` ### Constraints & Assumptions - `X` has shape (n, d) and `y` holds n ordinal labels whose sorted order is their ordinal order. - `cum` must be 2-D with shape (n, K-1). Its values are probabilities, but they are not guaranteed to be non-increasing across columns. - Factory estimators only need `fit(X, y)` and `predict_proba(X)` returning (n, 2) columns ordered [P(0), P(1)]. - Use NumPy and scikit-learn. ### What a Strong Answer Covers - A vectorized `cumulative_to_class_proba` that pads, differences, clips and renormalises, and rejects non-2-D input. - A `fit` that validates the class count and builds a fresh estimator per threshold with the correct binary target. - Prediction that uses the positive-class column, maps indices back through `classes_`, and resolves ties toward the lowest class. - Why independently trained threshold models can produce non-monotone cumulative probabilities, and what clipping does to them. - Training and prediction cost in terms of K, n and the base model. ### Follow-up Questions - How would you avoid non-monotone cumulative probabilities instead of patching them afterwards? - What happens if a threshold's binary target contains only one class? - Is argmax the right decision rule for ordinal targets?

Overview: Implement the Frank and Hall approach to ordinal classification: fit K-1 binary models for P(y > threshold), convert the cumulative probabilities into per-class probabilities, and predict with a lowest-class tie rule. It tests NumPy vectorization, scikit-learn style estimator code, and handling non-monotone outputs from independently trained models.

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

Implement cumulative_to_class_proba(cum) and a FrankHallClassifier class. For K sorted ordinal classes, train K-1 binary models where model m predicts P(y > classes_[m]). Convert these cumulative probabilities into per-class probabilities and predictions.

Requirements:

  • cumulative_to_class_proba : input shape (n, K-1). Take differences of [1, c_0, ..., c_{K-2}, 0] , clip negatives to 0, and renormalise each row to sum to 1. Non-2-D input raises ValueError .
  • fit : set classes_ (sorted unique labels) and estimators_ (K-1 models, model m fit on (y > classes_[m]).astype(int) ). Fewer than 2 classes raises ValueError . Returns self .
  • Each model comes from base_estimator_factory() . When the factory is None , use LogisticRegression(random_state=0) .
  • cumulative_proba(X) : shape (n, K-1), where column m is estimators_[m].predict_proba(X)[:, 1] . predict_proba applies the conversion above.
  • predict returns the label in classes_ with the highest probability. Ties go to the lowest class.

Example:

cumulative_to_class_proba([[0.8, 0.3]])
# -> [[0.2, 0.5, 0.3]]

cumulative_to_class_proba([[0.2, 0.9, 0.4]])
# raw diffs [0.8, -0.7, 0.5, 0.4] -> clip -> [0.8, 0, 0.5, 0.4]
# -> [[0.8, 0.0, 0.5, 0.4]] / 1.7

Constraints & Assumptions

  • X has shape (n, d) and y holds n ordinal labels whose sorted order is their ordinal order.
  • cum must be 2-D with shape (n, K-1). Its values are probabilities, but they are not guaranteed to be non-increasing across columns.
  • Factory estimators only need fit(X, y) and predict_proba(X) returning (n, 2) columns ordered [P(0), P(1)].
  • Use NumPy and scikit-learn.

What a Strong Answer Covers Guidance

  • A vectorized cumulative_to_class_proba that pads, differences, clips and renormalises, and rejects non-2-D input.
  • A fit that validates the class count and builds a fresh estimator per threshold with the correct binary target.
  • Prediction that uses the positive-class column, maps indices back through classes_ , and resolves ties toward the lowest class.
  • Why independently trained threshold models can produce non-monotone cumulative probabilities, and what clipping does to them.
  • Training and prediction cost in terms of K, n and the base model.

Follow-up Questions Guidance

  • How would you avoid non-monotone cumulative probabilities instead of patching them afterwards?
  • What happens if a threshold's binary target contains only one class?
  • Is argmax the right decision rule for ordinal targets?