📘
One-vs-Rest Classifier
HardClassification
Implement a One-vs-Rest (OvR) multi-class classifier using multiple binary logistic models.
Given K 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 k computes: P(y=k∣x)=σ(wk⋅x+bk)
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.5; w2=[−0.2,0.8],b2=0.1; w3=[0.1,−0.4],b3=0.3.
- For the input x=[1,2], we compute the sigmoid outputs for each classifier:
- P(y=1∣x)=σ(w1⋅x+b1)=σ(0.5∗1+0.3∗2−0.5)=σ(0.3)
- P(y=2∣x)=σ(w2⋅x+b2)=σ(−0.2∗1+0.8∗2+0.1)=σ(0.7)
- P(y=3∣x)=σ(w3⋅x+b3)=σ(0.1∗1−0.4∗2+0.3)=σ(−0.5)
- Calculating the sigmoid values: σ(0.3)≈0.5744, σ(0.7)≈0.6684, σ(−0.5)≈0.3773, and σ(0.7)≈0.6684 is not the highest, but σ(0.7) is actually the second highest, the actual highest is σ(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 is not the value, but is close, the actual value is 0.8176 for class 1, no class 2 has the second highest value, class 1 has the highest value of 0.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
Python 3.13.1
Test Results
0/0Run code to see test results.