Spaces:
Sleeping
Sleeping
File size: 1,968 Bytes
ffbf3ef a413a9a ffbf3ef a413a9a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import numpy as np
# We keep this because your Multiclass model uses the binary model inside it!
try:
from linear_svm import LinearSVMScartch
except ModuleNotFoundError:
from SVM.linear_svm import LinearSVMScartch
# ---------------------------------------------------------------------------
# One-vs-Rest multiclass wrapper around LinearSVMScartch
# ---------------------------------------------------------------------------
class MultiClassSVMScratch:
def __init__(self, C=1.0, learning_rate=0.001, n_epochs=50,
batch_size=64, random_state=42):
self.C = C
self.learning_rate = learning_rate
self.n_epochs = n_epochs
self.batch_size = batch_size
self.random_state = random_state
self.classifiers = {} # maps class label -> trained LinearSVMScartch
def fit(self, X, y):
self.classes_ = np.unique(y)
for k in self.classes_:
# Relabel: +1 if sample belongs to class k, -1 otherwise
y_binary = np.where(y == k, 1, -1)
# use_class_weights=True handles the ~10% / ~90% OvR imbalance
clf = LinearSVMScartch(
C=self.C,
learning_rate=self.learning_rate,
n_epochs=self.n_epochs,
batch_size=self.batch_size,
use_class_weights=True,
random_state=self.random_state,
)
clf.fit(X, y_binary)
self.classifiers[k] = clf
return self
def decision_scores(self, X):
# Returns (n_samples, n_classes) matrix of raw w·x+b scores
scores = np.column_stack([
self.classifiers[k].decision_function(X)
for k in self.classes_
])
return scores
def predict(self, X):
# Pick the class whose binary SVM is most confident (+highest score)
scores = self.decision_scores(X)
return self.classes_[np.argmax(scores, axis=1)] |