MCPcopy Index your code
hub / github.com/aladdinpersson/Machine-Learning-Collection / fit

Method fit

ML/algorithms/svm/svm.py:40–60  ·  view source on GitHub ↗
(self, X, y)

Source from the content-addressed store, hash-verified

38 self.C = C
39
40 def fit(self, X, y):
41 self.y = y
42 self.X = X
43 m, n = X.shape
44
45 # Calculate Kernel
46 self.K = np.zeros((m, m))
47 for i in range(m):
48 self.K[i, :] = self.kernel(X[i, np.newaxis], self.X)
49
50 # Solve with cvxopt final QP needs to be reformulated
51 # to match the input form for cvxopt.solvers.qp
52 P = cvxopt.matrix(np.outer(y, y) * self.K)
53 q = cvxopt.matrix(-np.ones((m, 1)))
54 G = cvxopt.matrix(np.vstack((np.eye(m) * -1, np.eye(m))))
55 h = cvxopt.matrix(np.hstack((np.zeros(m), np.ones(m) * self.C)))
56 A = cvxopt.matrix(y, (1, m), "d")
57 b = cvxopt.matrix(np.zeros(1))
58 cvxopt.solvers.options["show_progress"] = False
59 sol = cvxopt.solvers.qp(P, q, G, h, A, b)
60 self.alphas = np.array(sol["x"])
61
62 def predict(self, X):
63 y_predict = np.zeros((X.shape[0]))

Calls

no outgoing calls

Tested by

no test coverage detected