其他分享
首页 > 其他分享> > ML之SVM:随机产生100个点,建立SVM模型,找出超平面方程

ML之SVM:随机产生100个点,建立SVM模型,找出超平面方程

作者:互联网

ML之SVM:随机产生100个点,建立SVM模型,找出超平面方程

 

 

目录

实现结果

代码实例

 


 

 

 

实现结果

 

 

代码实例

import numpy as np
import pylab as pl 
from sklearn import svm

X = np.r_[np.random.randn(100, 2) - [2, 2], np.random.randn(100, 2) + [2, 2]]  

Y = [0]*100 +[1]*100  

clf = svm.SVC(kernel='linear')
clf.fit(X, Y)

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 ("w: ", w)
print ("a: ", a)

# print "xx: ", xx
# print "yy: ", yy
print ("support_vectors_: ", clf.support_vectors_)
print ("clf.coef_: ", clf.coef_)


# 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, facecolors='none')
pl.scatter(X[:, 0], X[:, 1], c=Y, cmap=pl.cm.Paired)

pl.axis('tight')
pl.show()  

 

 

 

相关文章
ML之SVM:随机产生100个点,建立SVM模型,找出超平面方程

 

标签:SVM,个点,clf,vectors,yy,xx,超平面,100,pl
来源: https://blog.51cto.com/u_14217737/2906451