v9.1.1
Menu

One-vs-Rest Classifier

hardFREEClassificationMachine Learning 1

Implement a One-vs-Rest (OvR) multi-class classifier using multiple binary logistic models.

Given KK binary classifiers (each represented as a weight vector and bias), classify data points by running all classifiers and selecting the class with the highest sigmoid output.

Each classifier kk computes: P(y=k∣x)=σ(wk⋅x+bk)P(y=k|x) = \sigma(w_k \cdot x + b_k)

For each data point, return the class (0-indexed) with the highest probability, along with that probability.

Return a list of tuples [(class_index, probability), ...].

Example

Input
X = [[1, 2]]
classifiers = [([0.5, 0.3], -0.5), ([-0.2, 0.8], 0.1), ([0.1, -0.4], 0.3)]
Output
[(1, 0.8176)]
Reasoning
  • X holds one data point, x=[1,2]x = [1, 2] (two features). classifiers holds three (weights, bias) pairs, one binary "is it class kk?" model per class: w0=[0.5,0.3],b0=−0.5w_0 = [0.5, 0.3], b_0 = -0.5; w1=[−0.2,0.8],b1=0.1w_1 = [-0.2, 0.8], b_1 = 0.1; w2=[0.1,−0.4],b2=0.3w_2 = [0.1, -0.4], b_2 = 0.3.
  • Each model scores the point with zk=wk⋅x+bkz_k = w_k \cdot x + b_k, then squashes it with σ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}}:
    • Class 0: z0=0.5+0.6−0.5=0.6z_0 = 0.5 + 0.6 - 0.5 = 0.6, σ(0.6)≈0.6457\sigma(0.6) \approx 0.6457
    • Class 1: z1=−0.2+1.6+0.1=1.5z_1 = -0.2 + 1.6 + 0.1 = 1.5, σ(1.5)≈0.8176\sigma(1.5) \approx 0.8176
    • Class 2: z2=0.1−0.8+0.3=−0.4z_2 = 0.1 - 0.8 + 0.3 = -0.4, σ(−0.4)≈0.4013\sigma(-0.4) \approx 0.4013
  • Class 1 is the most confident, so this point becomes the pair (class 1, probability 0.81760.8176 rounded to 4 decimal places), giving [(1, 0.8176)]: one tuple per row of X.
  • Notice the three probabilities sum to 1.86461.8646, not 1: each model is an independent yes/no judge. Since σ\sigma is increasing, the largest zkz_k always wins.

Constraints

  • X: 2D list (n_samples x n_features)
  • classifiers: list of (weights, bias) tuples, one per class
  • Return list of (class_index, max_probability) tuples
  • Probabilities rounded to 4 decimal places

Learn this in

More from Machine Learning 1

solution.py

Test results

Sign in to run your code against the test cases.