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

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

# we create 40 separable points
#np.random.seed(0)   
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_)

equation



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()   

  

 

posted @ 2018-01-07 11:45  一个处女座的程序猿  阅读(797)  评论(0编辑  收藏  举报