| 1 | import numpy as np |
| 2 | |
| 3 | class SVM: |
| 4 | |
| 5 | def __init__(self, learning_rate=0.001, lambda_param=0.01, n_iters=1000): |
| 6 | self.lr = learning_rate |
| 7 | self.lambda_param = lambda_param |
| 8 | self.n_iters = n_iters |
| 9 | self.w = None |
| 10 | self.b = None |
| 11 | |
| 12 | def fit(self, X, y): |
| 13 | n_samples, n_features = X.shape |
| 14 | |
| 15 | y_ = np.where(y <= 0, -1, 1) |
| 16 | |
| 17 | # init weights |
| 18 | self.w = np.zeros(n_features) |
| 19 | self.b = 0 |
| 20 | |
| 21 | for _ in range(self.n_iters): |
| 22 | for idx, x_i in enumerate(X): |
| 23 | condition = y_[idx] * (np.dot(x_i, self.w) - self.b) >= 1 |
| 24 | if condition: |
| 25 | self.w -= self.lr * (2 * self.lambda_param * self.w) |
| 26 | else: |
| 27 | self.w -= self.lr * (2 * self.lambda_param * self.w - np.dot(x_i, y_[idx])) |
| 28 | self.b -= self.lr * y_[idx] |
| 29 | |
| 30 | |
| 31 | def predict(self, X): |
| 32 | approx = np.dot(X, self.w) - self.b |
| 33 | return np.sign(approx) |
| 34 | |
| 35 | |
| 36 | # Testing |