-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear-SVM.py
39 lines (32 loc) · 1.21 KB
/
linear-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
import numpy as np
class LinearSVM:
def __init__(self, learning_rate=0.0001, lambda_param=0.01, n_iters=1000):
self.lr = learning_rate
self.lambda_param = lambda_param
self.n_iters = n_iters
self.w = None
self.b = None
def fit(self, X, y):
n_samples, n_features = X.shape
y_ = np.where(y <= 0, -1, 1)
self.w = np.zeros(n_features)
self.b = 0
for _ in range(self.n_iters):
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.lambda_param * self.w)
else:
self.w -= self.lr * (2 * self.lambda_param * self.w - np.dot(x_i, y_[idx]))
self.b -= self.lr * y_[idx]
def predict(self, X):
approx = np.dot(X, self.w) - self.b
return np.sign(approx)
# Example usage:
# X, y would be predefined datasets. Note that y should contain only -1 or 1.
# For example:
# X = np.array([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]])
# y = np.array([-1, -1, 1, 1, 1])
# model = LinearSVM()
# model.fit(X, y)
# predictions = model.predict(X)