MCPcopy Create free account
hub / github.com/AssemblyAI-Community/Machine-Learning-From-Scratch / fit

Method fit

09 SVM/svm.py:12–28  ·  view source on GitHub ↗
(self, X, y)

Source from the content-addressed store, hash-verified

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

Callers 1

svm.pyFile · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected