Get the matrix for quadratic programming (applying the kernel trick):
1 2 3 4 5 6
defget_P_Mat(x, y, n, kernel, _gamma): _P = np.zeros([n, n], dtype=np.float) for i in range(n): for j in range(n): _P[i][j] = y[i][0] * y[j][0] * kernel(x[i].T, x[j].T, _gamma) return _P
Solve the quadratic programming with cvxopt:
1 2 3 4 5 6 7 8 9 10 11 12
# optimizaton P = matrix(P_Mat) q = matrix(-np.ones([n*2, 1])) # 2*n inequality constraints G = matrix(np.vstack([-np.eye(n*2), np.eye(n*2)])) h = matrix(np.vstack([np.zeros([n*2, 1]), C*np.ones([n*2, 1])])) # 1 equality constraint A = matrix(y_train.T) b = matrix(0.)
sol = solvers.qp(P, q, G, h, A, b) alpha = np.array(sol['x'])
Select indices of SVs (support vectors) and SOs (support vector and outliers):
1 2 3 4 5 6 7
SV, SO = [], [] for i in range(n*2): if alpha[i][0] < 0.001: continue if abs(alpha[i][0]-C) > 0.001: SV.append(i) SO.append(i)
Calculate the offset w0:
1 2 3 4 5 6
w0 = 0. for i in SV: w0 += y_train[i][0] for j in SO: w0 -= alpha[j][0] * y_train[j][0] * P_Mat[j][i] w0 /= len(SV)
Result:
When gamma is set too large, it tends to overfitting.