Introduction
These strategies help us teach a computer to tell apart many groups by breaking the problem into simpler yes/no questions.
Jump into concepts and practice - no test required
One-vs-rest: Train one model per class, where that class is positive and all others are negative. One-vs-one: Train one model for every pair of classes, each distinguishing between just those two.
One-vs-rest example: Classify if an email is 'spam' or 'not spam' ignoring other categories. One-vs-one example: Classify if an animal is 'cat' or 'dog' ignoring other animals.
If you have 3 classes: A, B, C One-vs-rest trains 3 models: - A vs rest - B vs rest - C vs rest One-vs-one trains 3 models: - A vs B - A vs C - B vs C
from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.svm import SVC from sklearn.multiclass import OneVsRestClassifier, OneVsOneClassifier from sklearn.metrics import accuracy_score # Load iris dataset (3 classes) iris = datasets.load_iris() X, y = iris.data, iris.target # Split data X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42) # One-vs-rest classifier ovr = OneVsRestClassifier(SVC(kernel='linear', probability=True)) ovr.fit(X_train, y_train) y_pred_ovr = ovr.predict(X_test) acc_ovr = accuracy_score(y_test, y_pred_ovr) # One-vs-one classifier ovo = OneVsOneClassifier(SVC(kernel='linear', probability=True)) ovo.fit(X_train, y_train) y_pred_ovo = ovo.predict(X_test) acc_ovo = accuracy_score(y_test, y_pred_ovo) print(f"One-vs-rest accuracy: {acc_ovr:.2f}") print(f"One-vs-one accuracy: {acc_ovo:.2f}")
one-vs-rest strategy in multi-class classification?one-vs-one strategy for a problem with 4 classes?