机器学习----SVM实现(一)

调用sklearn 库实现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_)
print(clf.predict([2,0]))
print(clf.predict([1,1]))
print(clf.predict([2,3]))

实战二:

%matplotlib inline
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]]
#前20个点为0类,后20个点类为1
Y = [0] * 20 +[1] *20
# print(X)
# print(Y)
clf = svm.SVC(kernel = 'linear')
clf.fit(X,Y)

w = clf.coef_[0]
#a为斜率
a = -w[0]/w[1]
xx = np.linspace(-5,5)
#(clf.intercept_[0]/w[1])截距
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("support_vectors_:\n",clf.support_vectors_)
print("clf.coef_:\n",clf.coef_)

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

猜你喜欢

转载自blog.csdn.net/zhourunan123/article/details/80068033
今日推荐