def plot_svm(): num_points = 10 (X, y, f) = random_linearly_separable_data(num_points, bounds) (w_svm, vectors) = linsep_svm(X, y) x_plot = X[:,0] y_plot = X[:,1] s = 20*np.ones(num_points) s[vectors] = 60 pla = PLA(bounds=bounds) pla.fit(X, y) w_pla = pla.weights # test values for b print("Average b:", w_svm[0]) each_b = y[vectors] - np.dot(X[vectors], w_svm[1:]) print("Individual b's:", each_b) (x_f, y_f) = weights_to_mxb_2D(f, bounds) (x_w, y_w) = weights_to_mxb_2D(w_svm, bounds) (x_p, y_p) = weights_to_mxb_2D(w_pla, bounds) c = np.where(y==1, 'r', 'b') plt.scatter(x_plot,y_plot, s, c=c) plt.plot(x_f, y_f, 'k-.') plt.plot(x_w, y_w, 'b') plt.plot(x_p, y_p, 'r--') plt.xlim([-1, 1]) plt.ylim([-1, 1]) plt.grid() plt.show()
def simple_svm(): print("Solving problems 12 and 13") X = np.array([[1, 0], [0, 1], [0, -1], [-1, 0], [0, 2], [0, -2], [-2, 0]]) y = np.array([-1, -1, -1, 1, 1, 1, 1]) def svm_transform(X): return np.array([[x[1]**2-2*x[0]-1, x[0]**2-2*x[1]+1] for x in X]) X_t = svm_transform(X) clf = svm.SVC(kernel='poly', gamma=1, degree=2, coef0=1, C=10**10) clf.fit(X, y) print("num vectors: ", sum(clf.n_support_)) w_test = np.array([-0.5, 1, 0]) # Plot plt.scatter(X_t[:, 0], X_t[:, 1], c=np.where(y==1, 'r', 'b')) ax = plt.gca() bounds = np.concatenate([ax.get_xlim(), ax.get_ylim()]) (x_t, y_t) = weights_to_mxb_2D(w_test, bounds) plt.grid() plt.plot(x_t, y_t, 'r--') plt.show()