-
Notifications
You must be signed in to change notification settings - Fork 205
/
svm.py
71 lines (59 loc) · 2.27 KB
/
svm.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import numpy as np
class SVM:
def __init__(self, lr=0.01, lamda=0.01, epochs=100, patience=5):
"""
Params:
lr : Learning rate
lamda : Regularization parameter
epochs: Maximum number of epochs
patience: Number of epochs with no improvement before stopping early
"""
self.lr = lr
self.lamda = lamda
self.epochs = epochs
self.patience = patience
self.w = None # Weights
self.b = None # Bias
def fit(self, X, y):
n_samples, n_features = X.shape
# Initialize weights and bias
self.w = np.zeros(n_features)
self.b = 0
# Convert labels to {-1, 1}
y_ = np.where(y <= 0, -1, 1)
best_loss = float('inf')
patience_counter = 0
# Gradient descent with early stopping
for epoch in range(self.epochs):
loss = 0
for idx, x_i in enumerate(X):
condition = y_[idx] * (np.dot(x_i, self.w) + self.b) >= 1
if condition:
self.w -= self.lr * (2 * self.lamda * self.w)
else:
self.w -= self.lr * (2 * self.lamda * self.w - np.dot(x_i, y_[idx]))
self.b -= self.lr * y_[idx]
loss += max(0, 1 - y_[idx] * (np.dot(x_i, self.w) + self.b))
# Display weights and bias every 10 epochs
if epoch % 10 == 0:
print(f"Epoch {epoch}: Loss = {loss}")
print(f"Weights: {self.w}, Bias: {self.b}")
# Early stopping logic
if loss < best_loss:
best_loss = loss
patience_counter = 0
else:
patience_counter += 1
if patience_counter >= self.patience:
print(f"Early stopping at epoch {epoch}")
break
def predict(self, X):
linear_output = np.dot(X, self.w) + self.b
# Convert -1 back to 0 for label compatibility
return np.where(np.sign(linear_output) == -1, 0, 1)
def save_model(self, filename='svm_model.npz'):
np.savez(filename, w=self.w, b=self.b)
def load_model(self, filename='svm_model.npz'):
data = np.load(filename)
self.w = data['w']
self.b = data['b']