def fit(self, X, T, max_iter=int(1e3), tol=1e-5):
"""Use training data ``X`` and ``T`` to fit a SVC models."""
n_samples = X.shape[0]
n_dual_vars = 2 * n_samples
# Compute the Gram matrix of training data
K = self.kernel.inner(X, X)
# The equality constraints: H(x) = 0
ones = np.ones(n_samples)
A = np.concatenate((ones, -ones))
cons = ({'type': 'eq', 'fun': lambda x: A.dot(x), 'jac': lambda x: A})
# The inequality constaints: 0 <= G(x) <= C
bnds = [(0, self.C) for i in range(n_dual_vars)]
# The target function: (1/2)*x'*Q*x + p'*x
Q = np.array(np.bmat([[K, -K], [-K, K]]))
p = self.eps - A * np.concatenate((T, T))
lagrange = lambda x: (0.5 * x.dot(Q).dot(x) + p.dot(x), Q.dot(x) + p)
# Solve the quadratic program
opt_solution = minimize(lagrange,
np.zeros(n_dual_vars),
method='SLSQP',
constraints=cons,
bounds=bnds,
tol=tol,
jac=True,
options={'maxiter': max_iter,
'disp': True})
self.dual_var = np.array([None, None], dtype=np.object)
self.dual_var[0] = opt_solution.x[:n_samples]
self.dual_var[1] = opt_solution.x[n_samples:]
self.sv_indices = np.array([None, None], dtype=np.object)
self.sv_indices[0] = np.nonzero((1 - np.isclose(self.dual_var[0], 0)))[
0]
self.sv_indices[1] = np.nonzero((1 - np.isclose(self.dual_var[1], 0)))[
0]
self.union_sv_inices = np.union1d(*self.sv_indices)
self.inner_sv_indices = np.array([None, None], dtype=np.object)
self.inner_sv_indices[0] = np.nonzero(
(1 - np.isclose(self.dual_var[0], 0)) *
(1 - np.isclose(self.dual_var[0], self.C)))[0]
self.inner_sv_indices[1] = np.nonzero(
(1 - np.isclose(self.dual_var[1], 0)) *
(1 - np.isclose(self.dual_var[1], self.C)))[0]
return self
评论列表
文章目录