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)]