支持向量机(SVM)线性可分解决实例--参考麦子学院机器学习基础5.1
2018-01-29 21:02
573 查看
#首先产生数据集 #训练数据集 #画图展示超平面 import numpy as np import pylab as pl from sklearn import svm # we create 40 separable points #正态分布产生,均值分别为-2 2,可以线性可分 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 #fit the model clf = svm.SVC(kernel='linear') clf.fit(X, Y) # get the separating hyperplane w = clf.coef_[0]#权值 a = -w[0]/w[1]#直线斜率 xx = np.linspace(-5, 5) yy = a*xx - (clf.intercept_[0])/w[1] # plot the parallels to the separating hyperplane that pass through the support vectors 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("w: ", w) print("a: ", a) # print "xx: ", xx # print "yy: ", yy print("support_vectors_: ", clf.support_vectors_) print("clf.coef_: ", clf.coef_) # switching to the generic n-dimensional parameterization of the hyperplan to the 2D-specific equation # of a line y=a.x +b: the generic w_0x + w_1y +w_3=0 can be rewritten y = -(w_0/w_1) x + (w_3/w_1) # plot the line, the points, and the nearest vectors to the plane 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, edgecolors='red') pl.scatter(X[:, 0], X[:, 1], c=Y, cmap=pl.cm.Paired) pl.axis('tight') pl.show()
相关文章推荐
- 人脸识别SVM算法实现--参考麦子学院彭亮机器学习基础5.2
- 机器学习中的算法(2)-支持向量机(SVM)基础
- 机器学习中的算法(2)-支持向量机(SVM)基础
- 机器学习中的算法(2)-支持向量机(SVM)基础
- 支持向量机 SVM 机器学习基础(6)
- 机器学习中的算法(2)-支持向量机(SVM)基础
- 机器学习中的算法(2)-支持向量机(SVM)基础
- 机器学习中的算法-支持向量机(SVM)基础
- 机器学习中的算法(2)-支持向量机(SVM)基础
- 机器学习中的算法(2)-支持向量机(SVM)基础
- 机器学习中的算法(2)-支持向量机(SVM)基础
- 机器学习中的算法-支持向量机(SVM)基础
- 机器学习中的算法(2)-支持向量机(SVM)基础
- 机器学习中的算法(2)-支持向量机(SVM)基础
- 机器学习中的算法(2)-支持向量机(SVM)基础
- 机器学习第十课:支持向量机SVM(一)线性可分(硬间隔)SVM
- 机器学习20-线性支持向量机svm公式推导(二)
- 机器学习中的算法(2)-支持向量机(SVM)基础
- 机器学习中的算法(2)-支持向量机(SVM)基础
- OpenCV之ml 模块. 机器学习:支持向量机(SVM)介绍 支持向量机对线性不可分数据的处理