-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_svm.py
More file actions
52 lines (40 loc) · 1.5 KB
/
Copy pathlinear_svm.py
File metadata and controls
52 lines (40 loc) · 1.5 KB
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
import numpy as np
def batch_norm(x, eps=1e-8):
x_mean = np.mean(x, axis=0)
x_std = np.std(x, axis=0)
return (x - x_mean) / (x_std + eps)
class LinearSVM:
def __init__(self, n_features, lr=1e-2, alpha=1e-3):
self.lr = lr
self.alpha = alpha
self.W = np.random.rand(1, n_features)
self.b = np.random.rand(1)
def fit(self, X, y, epochs=100):
y = np.sign(y)
for _ in range(epochs):
for i, x in enumerate(X):
x = x.reshape(-1, 1)
h = (self.W @ x + self.b).item()
condition = (y[ i ] * h) >= 0
if condition:
self.W -= self.lr * self.W
else:
self.W -= self.lr * (self.W - self.alpha * y[ i ] * x.T)
self.b -= self.lr * self.alpha * y[ i ]
def predict(self, X):
h = self.W @ X.T + self.b
return np.sign(h)
def score(self, X, y):
y = np.sign(y).reshape(1, -1)
pred = self.predict(X)
return np.sum(y == pred) / y.shape[ -1 ]
if __name__ == '__main__':
from sklearn.datasets import make_blobs
X, y = make_blobs(n_samples=100, centers=2, cluster_std=0.2)
n_train = int(X.shape[ 0 ] * 0.8)
train_x, test_x = X[ :n_train ], X[ n_train: ]
train_y, test_y = y[ :n_train ], y[ n_train: ]
train_x, test_x = batch_norm(train_x), batch_norm(test_x)
ls = LinearSVM(2)
ls.fit(train_x, train_y)
print(f"Test Accuracy: {ls.score(test_x, test_y)}")