def svm(x, y):
"""
classification SVM
Minimize
1/2 * w^T w
subject to
y_n (w^T x_n + b) >= 1
"""
weights_total = len(x[0])
I_n = np.identity(weights_total-1)
P_int = np.vstack(([np.zeros(weights_total-1)], I_n))
zeros = np.array([np.zeros(weights_total)]).T
P = np.hstack((zeros, P_int))
q = np.zeros(weights_total)
G = -1 * vec_to_dia(y).dot(x)
h = -1 * np.ones(len(y))
matrix_arg = [ matrix(x) for x in [P,q,G,h] ]
sol = solvers.qp(*matrix_arg)
return np.array(sol['x']).flatten()
评论列表
文章目录