SVM应用
上图的例子(使用线性核函数):
from sklearn import svm X = [[2,0],[1,1],[2,3]] Y = [0,0,1] clf = svm.SVC(kernel='linear') clf.fit(X,Y) print(clf) print(clf.support_vectors_) print(clf.support_) print(clf.n_support_) | 运行有点慢,结果贴出来: SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, decision_function_shape='ovr', degree=3, gamma='auto', kernel='linear', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False) [[1. 1.] [2. 3.]] [1 2] [1 1] |
线性SVM和软间隔SVMhttps://www.jianshu.com/p/bfcf645bd56a
import numpy as np import pylab as pl from sklearn import svm np.random.seed(0) X = np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]] Y = [0] * 20 + [1] * 20 clf = svm.SVC(kernel='linear', C=0.01) clf.fit(X, Y) # w0x + w1y + w2=0 所以: y=-(w0/w1)x-w2/w1 w = clf.coef_[0] a = -w[0] / w[1] xx = np.linspace(-5, 5) yy = a * xx - (clf.intercept_[0]) / w[1] b = clf.support_vectors_[0] yy_down = a * xx + (b[1] - a * b[0]) b = clf.support_vectors_[-1] yy_up = a * xx + (b[1] - a * b[0]) print(clf.coef_) print(clf.support_vectors_) pl.plot(xx, yy, 'k-') pl.plot(xx, yy_down, 'k--') pl.plot(xx, yy_up, 'k--') pl.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=80, facecolor='none') pl.scatter(X[:, 0], X[:, 1], c=Y, cmap=pl.cm.Paired) pl.axis('tight') pl.show() |
注意代码最后红色地方在IDE中有错误提示,但是可以正常运行,包名的问题?
鸢尾花SVM二特征分类:http://www.cnblogs.com/luyaoblog/p/6775342.html
from sklearn import svm import numpy as np import matplotlib.pyplot as plt import matplotlib as mpl from sklearn.model_selection import train_test_split def iris_type(s): it = {b'setosa': 0, b'versicolor': 1, b'virginica': 2} return it[s] def show_accuracy(y_hat, y_test, param): pass path = r'D:\OneDrive\data\[Python]\iris.txt' # 数据文件路径 data = np.loadtxt(path, dtype=float, delimiter=',', converters={4: iris_type}) x, y = np.split(data, (4,), axis=1) x = x[:, :2] x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=1, train_size=0.6) # clf = svm.SVC(C=0.1, kernel='linear', decision_function_shape='ovr') clf = svm.SVC(C=0.8, kernel='rbf', gamma=20, decision_function_shape='ovr') clf.fit(x_train, y_train.ravel()) print(clf.score(x_train, y_train)) # 精度 y_hat = clf.predict(x_train) show_accuracy(y_hat, y_train, '训练集') print(clf.score(x_test, y_test)) y_hat = clf.predict(x_test) show_accuracy(y_hat, y_test, '测试集') print('decision_function:\n', clf.decision_function(x_train)) print('\npredict:\n', clf.predict(x_train)) x1_min, x1_max = x[:, 0].min(), x[:, 0].max() # 第0列的范围 x2_min, x2_max = x[:, 1].min(), x[:, 1].max() # 第1列的范围 x1, x2 = np.mgrid[x1_min:x1_max:200j, x2_min:x2_max:200j] # 生成网格采样点 grid_test = np.stack((x1.flat, x2.flat), axis=1) # 测试点 mpl.rcParams['font.sans-serif'] = [u'SimHei'] mpl.rcParams['axes.unicode_minus'] = False cm_light = mpl.colors.ListedColormap(['#A0FFA0', '#FFA0A0', '#A0A0FF']) cm_dark = mpl.colors.ListedColormap(['g', 'r', 'b']) # print 'grid_test = \n', grid_test grid_hat = clf.predict(grid_test) # 预测分类值 grid_hat = grid_hat.reshape(x1.shape) # 使之与输入的形状相同 alpha = 0.5 plt.pcolormesh(x1, x2, grid_hat, cmap=cm_light) # 预测值的显示 # plt.scatter(x[:, 0], x[:, 1], c=y, edgecolors='k', s=50, cmap=cm_dark) # 样本 plt.plot(x[:, 0], x[:, 1], 'o', alpha=alpha, color='blue', markeredgecolor='k') plt.scatter(x_test[:, 0], x_test[:, 1], s=120, facecolors='none', zorder=10) # 圈中测试集样本 plt.xlabel(u'花萼长度', fontsize=13) plt.ylabel(u'花萼宽度', fontsize=13) plt.xlim(x1_min, x1_max) plt.ylim(x2_min, x2_max) plt.title(u'鸢尾花SVM二特征分类', fontsize=15) # plt.grid() plt.show() |