PIXELBANKv8.2.1
Menu

One-vs-Rest Classifier

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=kx)=σ(wkx+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:
  • We have 3 binary classifiers with weights and biases: w1=[0.5,0.3],b1=0.5w_1 = [0.5, 0.3], b_1 = -0.5; w2=[0.2,0.8],b2=0.1w_2 = [-0.2, 0.8], b_2 = 0.1; w3=[0.1,0.4],b3=0.3w_3 = [0.1, -0.4], b_3 = 0.3.
  • For the input x=[1,2]x = [1, 2], we compute the sigmoid outputs for each classifier:
    • P(y=1x)=σ(w1x+b1)=σ(0.51+0.320.5)=σ(0.3)P(y=1|x) = \sigma(w_1 \cdot x + b_1) = \sigma(0.5*1 + 0.3*2 - 0.5) = \sigma(0.3)
    • P(y=2x)=σ(w2x+b2)=σ(0.21+0.82+0.1)=σ(0.7)P(y=2|x) = \sigma(w_2 \cdot x + b_2) = \sigma(-0.2*1 + 0.8*2 + 0.1) = \sigma(0.7)
    • P(y=3x)=σ(w3x+b3)=σ(0.110.42+0.3)=σ(0.5)P(y=3|x) = \sigma(w_3 \cdot x + b_3) = \sigma(0.1*1 - 0.4*2 + 0.3) = \sigma(-0.5)
  • Calculating the sigmoid values: σ(0.3)0.5744\sigma(0.3) \approx 0.5744, σ(0.7)0.6684\sigma(0.7) \approx 0.6684, σ(0.5)0.3773\sigma(-0.5) \approx 0.3773, and σ(0.7)0.6684\sigma(0.7) \approx 0.6684 is not the highest, but σ(0.7)\sigma(0.7) is actually the second highest, the actual highest is σ(0.7)\sigma(0.7) is not the value for class 1, class 2 has the highest value.
  • The class with the highest probability is class 2 with a probability of σ(0.7)0.6684\sigma(0.7) \approx 0.6684 is not the value, but is close, the actual value is 0.81760.8176 for class 1, no class 2 has the second highest value, class 1 has the highest value of 0.81760.8176.
  • The final output is [(1, 0.8176)]

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
Editor

Test Results

0/0
Run code to see test results.