1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| from cvxopt import matrix, solvers import numpy as np import matplotlib.pyplot as plt
def split_train_test_data(mean1, mean2, sdt, n): np.random.seed(529) x_p1 = np.random.normal(loc=mean1, scale=sdt,size=int(n/2)).reshape(-1,1) x_p2 = np.random.normal(loc=mean1, scale=sdt,size=int(n/2)).reshape(-1,1) y_p = np.ones(len(x_p1))[:,np.newaxis] X_p = np.hstack((x_p1, x_p2))
x_n1 = np.random.normal(loc=mean2, scale=sdt,size=int(n/2)).reshape(-1,1) x_n2 = np.random.normal(loc=mean2, scale=sdt,size=int(n/2)).reshape(-1,1) y_n = (np.ones(len(x_n1))*-1)[:,np.newaxis] X_n = np.hstack((x_n1, x_n2))
fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(x_p1, x_p2, color = "#ffb07c", s = 100, label = "1") ax.scatter(x_n1, x_n2, color = "#c94cbe", s = 100, label = "-1") plt.legend() plt.show()
F_train = np.vstack((X_n[:int(n/2*0.8)], X_p[:int(n/2*0.8)])) y_train = np.vstack((y_n[:int(n/2*0.8)], y_p[:int(n/2*0.8)])) F_test = np.vstack((X_n[int(n/2*0.8):], X_p[int(n/2*0.8):])) y_test = np.vstack((y_n[int(n/2*0.8):], y_p[int(n/2*0.8):]))
return F_train, F_test, y_train, y_test
F_train, F_test, y_train, y_test = split_train_test_data(6,1,2,50) print("训练集规模",F_train.shape) print("测试集规模",F_test.shape) print("训练集标签",y_train.shape) print("测试集标签",y_test.shape)
def train(x, y, C): k = [] for i in range(x.shape[0]): k.append([]) for j in range(x.shape[0]): k[i].append(np.inner(x[i], x[j])) k = np.array(k)
l = np.inner(y, y)
p = matrix(l * k) q = matrix(np.ones(40)*-1) A = matrix(y.reshape(1,-1)) b = matrix(0.) g = matrix(np.vstack((np.eye(40)*-1, np.eye(40)))) h = matrix(np.vstack((np.zeros(len(y)).reshape(-1,1), np.ones(len(y)).reshape(-1,1)*C))) solution = solvers.qp(p,q,g,h,A,b)
a = np.ravel(solution['x'])
w_best = np.sum(a.reshape(-1,1)*y*x, axis = 0) b_best = 0 for j in range(x.shape[0]): b_best += y[j] - np.sum(y * a.reshape(-1,1)* np.inner(x, x[j].T).reshape(-1,1)) b_best = b_best/x.shape[0]
return w_best, b_best
w, b = train(F_train,y_train,100) x = np.linspace(-6, 10 , 50) y = (-w[0]/w[1]*x - b/w[1]).ravel() [[plt.scatter(data[0], data[1], color = "#c94cbe", s = 100)]for data in F_train[:20]] [[plt.scatter(data[0], data[1], color = "#ffb07c", s = 100)]for data in F_train[20:]] plt.plot(x, y, color="#087804") def test(w, b, x): prediction=np.sign(np.dot(x, w)+b) return prediction prediction = test(w, b, F_test) num = 0 for i in range(y_test.shape[0]): if prediction[i] == y_test[i]: num += 1 acc = 100*num/(y_test.shape[0]) print("acc = %.2f %%"%acc) x = np.linspace(-6, 10 , 50) y = (-w[0]/w[1]*x - b/w[1]).ravel() y1 = (-w[0]/w[1]*x - (b+1)/w[1]).ravel() y2 = (-w[0]/w[1]*x - (b-1)/w[1]).ravel() [[plt.scatter(data[0], data[1], color = "#c94cbe", s=100)]for data in F_train[:20]] [[plt.scatter(data[0], data[1], color = "#ffb07c", s=100)]for data in F_train[20:]] [[plt.scatter(data[0], data[1], color = "#c94cbe", marker="*", s=100)]for data in F_test[:5]] [[plt.scatter(data[0], data[1], color = "#ffb07c", marker="*", s=100)]for data in F_test[5:]] plt.plot(x, y, color="#087804") plt.plot(x, y1, color="#048243", ls='--') plt.plot(x, y2, color="#048243", ls='--')
|