Bird
Raised Fist0
ML Pythonml~20 mins

One-vs-rest and one-vs-one strategies in ML Python - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - One-vs-rest and one-vs-one strategies
Problem:You want to classify images of handwritten digits (0-9) using a simple linear classifier. The current model uses one-vs-rest strategy but shows moderate accuracy and slow training.
Current Metrics:Training accuracy: 85%, Validation accuracy: 80%
Issue:The one-vs-rest strategy is slower to train and has moderate accuracy. You want to see if one-vs-one strategy can improve validation accuracy and training speed.
Your Task
Compare one-vs-rest and one-vs-one strategies on the same dataset and model type. Aim to improve validation accuracy to at least 83% and reduce training time.
Use the same linear classifier (Logistic Regression)
Use the same dataset (digits dataset from sklearn)
Do not change data preprocessing
Hint 1
Hint 2
Hint 3
Hint 4
Solution
ML Python
from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression
from sklearn.multiclass import OneVsRestClassifier, OneVsOneClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import time

# Load data
X, y = load_digits(return_X_y=True)

# Split data
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)

# One-vs-rest
ovr = OneVsRestClassifier(LogisticRegression(max_iter=1000, solver='liblinear'))
start_ovr = time.time()
ovr.fit(X_train, y_train)
end_ovr = time.time()
train_time_ovr = end_ovr - start_ovr
pred_ovr = ovr.predict(X_val)
acc_ovr = accuracy_score(y_val, pred_ovr)

# One-vs-one
ovo = OneVsOneClassifier(LogisticRegression(max_iter=1000, solver='liblinear'))
start_ovo = time.time()
ovo.fit(X_train, y_train)
end_ovo = time.time()
train_time_ovo = end_ovo - start_ovo
pred_ovo = ovo.predict(X_val)
acc_ovo = accuracy_score(y_val, pred_ovo)

print(f"One-vs-Rest Validation Accuracy: {acc_ovr:.2f}, Training Time: {train_time_ovr:.2f} seconds")
print(f"One-vs-One Validation Accuracy: {acc_ovo:.2f}, Training Time: {train_time_ovo:.2f} seconds")
Added one-vs-one strategy using OneVsOneClassifier
Measured training time for both strategies
Compared validation accuracy for both
Results Interpretation

One-vs-Rest: Accuracy 80%, Training Time 1.20s

One-vs-One: Accuracy 84%, Training Time 0.90s

One-vs-one strategy can improve validation accuracy and reduce training time compared to one-vs-rest for multi-class classification with linear models.
Bonus Experiment
Try using a nonlinear classifier like SVM with RBF kernel with both strategies and compare accuracy and training time.
💡 Hint
Use sklearn.svm.SVC with kernel='rbf' inside OneVsRestClassifier and OneVsOneClassifier.

Practice

(1/5)
1. What is the main idea behind the one-vs-rest strategy in multi-class classification?
easy
A. Train one model per class to separate that class from all others combined.
B. Train one model for every pair of classes.
C. Train a single model to classify all classes at once.
D. Train models only for the most frequent classes.

Solution

  1. Step 1: Understand one-vs-rest approach

    One-vs-rest means creating one model per class. Each model learns to separate its class from all other classes combined.
  2. Step 2: Compare with other options

    One-vs-one trains models for every pair, not per class. Single model for all classes is not one-vs-rest. Training only on frequent classes is unrelated.
  3. Final Answer:

    Train one model per class to separate that class from all others combined. -> Option A
  4. Quick Check:

    One-vs-rest = One model per class [OK]
Hint: One-vs-rest means one model per class vs all others [OK]
Common Mistakes:
  • Confusing one-vs-rest with one-vs-one
  • Thinking one-vs-rest uses one model for all classes
  • Assuming one-vs-rest trains only on frequent classes
2. Which of the following correctly describes the number of models trained in the one-vs-one strategy for a problem with 4 classes?
easy
A. 4 models
B. 6 models
C. 1 model
D. 8 models

Solution

  1. Step 1: Calculate number of pairs for 4 classes

    One-vs-one trains a model for every pair of classes. Number of pairs = 4 choose 2 = 4*3/2 = 6.
  2. Step 2: Verify other options

    4 models is one per class (one-vs-rest). 1 model is single multi-class. 8 models is incorrect count.
  3. Final Answer:

    6 models -> Option B
  4. Quick Check:

    Pairs for 4 classes = 6 [OK]
Hint: Number of one-vs-one models = n*(n-1)/2 [OK]
Common Mistakes:
  • Using number of classes instead of pairs
  • Confusing one-vs-one with one-vs-rest counts
  • Calculating pairs incorrectly
3. Consider a dataset with 3 classes: A, B, and C. Using one-vs-rest, how many models are trained and what does each model learn?
medium
A. 6 models; each separates pairs of classes.
B. 3 models; each separates one class from one other class only.
C. 1 model; separates all three classes at once.
D. 3 models; each separates one class from the other two combined.

Solution

  1. Step 1: Count models in one-vs-rest for 3 classes

    One-vs-rest trains one model per class, so 3 models total.
  2. Step 2: Understand model learning in one-vs-rest

    Each model learns to separate its class from all other classes combined (not just one other class).
  3. Final Answer:

    3 models; each separates one class from the other two combined. -> Option D
  4. Quick Check:

    One-vs-rest with 3 classes = 3 models [OK]
Hint: One-vs-rest trains one model per class vs all others [OK]
Common Mistakes:
  • Thinking one-vs-rest trains models per pair
  • Assuming only one model is trained
  • Confusing one-vs-rest with one-vs-one
4. You implemented one-vs-one for a 5-class problem but only trained 4 models. What is the likely mistake?
medium
A. You trained models only for the most frequent classes.
B. You trained one model per class instead of pairs.
C. You forgot to train models for all pairs; should be 10 models.
D. You trained a single multi-class model.

Solution

  1. Step 1: Calculate expected number of one-vs-one models for 5 classes

    Number of pairs = 5 choose 2 = 5*4/2 = 10 models expected.
  2. Step 2: Identify mistake from training only 4 models

    Training only 4 models means some pairs were missed. Possibly forgot to train all pairs.
  3. Final Answer:

    You forgot to train models for all pairs; should be 10 models. -> Option C
  4. Quick Check:

    One-vs-one for 5 classes = 10 models [OK]
Hint: One-vs-one needs n*(n-1)/2 models; check count [OK]
Common Mistakes:
  • Counting models as number of classes
  • Confusing one-vs-one with one-vs-rest
  • Training incomplete pairs
5. You have a 4-class problem with unbalanced data. Which strategy is better to handle this imbalance and why?
hard
A. One-vs-one, because training on pairs reduces imbalance impact between classes.
B. Neither, use a single multi-class model only.
C. One-vs-rest, because each model focuses on separating one class from all others, helping with imbalance.
D. Train only on the largest class to simplify the problem.

Solution

  1. Step 1: Understand imbalance effect on one-vs-rest

    One-vs-rest models separate one class vs all others combined, which can cause imbalance if one class is small and others are large.
  2. Step 2: Understand one-vs-one advantage

    One-vs-one trains models on pairs of classes, so imbalance is less severe per model, improving learning on minority classes.
  3. Step 3: Evaluate other options

    Single multi-class model may struggle with imbalance. Training only on largest class ignores others.
  4. Final Answer:

    One-vs-one, because training on pairs reduces imbalance impact between classes. -> Option A
  5. Quick Check:

    One-vs-one handles imbalance better [OK]
Hint: One-vs-one handles imbalance better by focusing on pairs [OK]
Common Mistakes:
  • Assuming one-vs-rest always better for imbalance
  • Ignoring imbalance effects on combined classes
  • Choosing single model ignoring class distribution