MCPcopy Index your code
hub / github.com/rushter/MLAlgorithms / _train

Method _train

mla/svm/svm.py:49–105  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

47 return self._train()
48
49 def _train(self):
50 iters = 0
51 while iters < self.max_iter:
52 iters += 1
53 alpha_prev = np.copy(self.alpha)
54
55 for j in range(self.n_samples):
56 # Pick random i
57 i = self.random_index(j)
58
59 eta = 2.0 * self.K[i, j] - self.K[i, i] - self.K[j, j]
60 if eta >= 0:
61 continue
62 L, H = self._find_bounds(i, j)
63
64 # Error for current examples
65 e_i, e_j = self._error(i), self._error(j)
66
67 # Save old alphas
68 alpha_io, alpha_jo = self.alpha[i], self.alpha[j]
69
70 # Update alpha
71 self.alpha[j] -= (self.y[j] * (e_i - e_j)) / eta
72 self.alpha[j] = self.clip(self.alpha[j], H, L)
73
74 self.alpha[i] = self.alpha[i] + self.y[i] * self.y[j] * (
75 alpha_jo - self.alpha[j]
76 )
77
78 # Find intercept
79 b1 = (
80 self.b
81 - e_i
82 - self.y[i] * (self.alpha[i] - alpha_io) * self.K[i, i]
83 - self.y[j] * (self.alpha[j] - alpha_jo) * self.K[i, j]
84 )
85 b2 = (
86 self.b
87 - e_j
88 - self.y[j] * (self.alpha[j] - alpha_jo) * self.K[j, j]
89 - self.y[i] * (self.alpha[i] - alpha_io) * self.K[i, j]
90 )
91 if 0 < self.alpha[i] < self.C:
92 self.b = b1
93 elif 0 < self.alpha[j] < self.C:
94 self.b = b2
95 else:
96 self.b = 0.5 * (b1 + b2)
97
98 # Check convergence
99 diff = np.linalg.norm(self.alpha - alpha_prev)
100 if diff < self.tol:
101 break
102 logging.info("Convergence has reached after %s." % iters)
103
104 # Save support vectors index
105 self.sv_idx = np.where(self.alpha > 0)[0]
106

Callers 1

fitMethod · 0.95

Calls 4

random_indexMethod · 0.95
_find_boundsMethod · 0.95
_errorMethod · 0.95
clipMethod · 0.95

Tested by

no test coverage detected