SVM 优化出来支持向量点的不等式约束不等于 1 是为什么?
import numpy as np
import random
import matplotlib.pyplot as plt
C=1
def selectj(i,n):
j = i
while(j==i):
j = random.randint(0,5)
return j
xt1 = np.array([[1,3],[3,5],[3,6]])
xt2 = np.array([[0,2],[-3,0],[-4,-3]])
plt.scatter(xt1[:,0], xt1[:,1], c='red', marker='o', s=30)
plt.scatter(xt2[:,0], xt2[:,1], c='blue', marker='*', s=30)
x = np.array([[1,3],[3,5],[3,6],[0,2],[-3,0],[-4,-3]])
y = np.array([[1],[1],[1],[-1],[-1],[-1]])
alpha = np.zeros((6, 1))
w = np.dot((alpha*y).T, x)
itera = 0
b=0
while (itera < 10):
itera = itera +1
for i in range(6):
w = np.dot((alpha * y).T, x)
print(w)
ev1 = np.dot(w, x[i]) + b - y[i]
disold = 0
if abs(ev1) > 0.001:
# for j in range(6):
# ev2 = np.dot(w, x[j]) + b - y[j]
# dis = abs(float(ev1) - float(ev2))
# if dis > disold:
# disold = dis
# p = j
p = selectj(i, 6)
ev2 = float(np.dot(w, x[p]) + b - y[p])
eta = -2 * np.dot(x[i, :], x[p, :]) + np.dot(x[i, :], x[i, :]) + np.dot(x[p, :], x[p, :])
if y[i] != y[p]:
L= max(0, alpha[p] - alpha[i])
H= min(C, C + alpha[p] - alpha[i])
else:
L=max(0, alpha[p] + alpha[i] - C)
H=min(C, alpha[p] + alpha[i])
alphapold = alpha[p].copy()
alphaiold = alpha[i].copy()
alpha[p] = alpha[p] + y[p] * (float(ev1) - float(ev2)) / eta
if alpha[p] < L:
alpha[p] = L
elif alpha[p] > C:
alpha[p] = C
alpha[i] += y[i] * y[p] * (alphapold-alpha[p])
# b1 = y[i] - np.dot(np.dot((alpha * y).T, x), x[i])
# b2 = y[p] - np.dot(np.dot((alpha * y).T, x), x[p])
b1 = y[i] - np.dot((alpha * y).T, np.dot(x, x[i]))
b2 = y[p] - np.dot((alpha * y).T, np.dot(x, x[p]))
if 0 < alpha[i] < C:
b = float(b1)
elif 0<alpha[p]<C:
b = float(b2)
else:
b = (float(b1)+float(b2))/2
for i in range(6):
if alpha[i] < 0.01:
alpha[i] = 0
w = np.dot((alpha * y).T, x)
b = -(y[0] * np.dot(w,x[0]) - y[3] * np.dot(w,x[3]))/2
x231 = np.linspace(-3,3,200)
y231 = -(w[0,0] * x231 + b)/w[0,1]
plt.plot(x231,y231)
print(y[0] * np.dot(w,x[0]))
plt.show()
大家帮忙看看为什么优化出来,wx+b不等于1,其中x是支持向量