支持向量SVM-py实现

总共三个点


Paste_Image.png
#sklearn简单例子
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)

# get support vectors,支持向量点

print(clf.support_vectors_)

# get indices of support vectors
#支持向量的下表
print(clf.support_ )

# get number of support vectors for each class
#各有多少点属于支持向量
print(clf.n_support_ )

print(clf.predict([2, .0]))

2.python代码

#支持向量的计算
import numpy as np
#pl画图的功能
import pylab as pl
#导入svm
from sklearn import svm

# 随机创建40个点
np.random.seed(0)
#训练实例的集合,-[2,2]和+[2,2] 进行分类成可直线分开的实例
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]
#从-5,5产生以下值
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_)

# In scikit-learn coef_ attribute holds the vectors of the separating hyperplanes for linear models. It has shape (n_classes, n_features) if n_classes > 1 (multi-class one-vs-all) and (1, n_features) for binary classification.
# 
# In this toy binary classification example, n_features == 2, hence w = coef_[0] is the vector orthogonal to the hyperplane (the hyperplane is fully defined by it + the intercept).
# 
# To plot this hyperplane in the 2D case (any hyperplane of a 2D plane is a 1D line), we want to find a f as in y = f(x) = a.x + b. In this case a is the slope of the line and can be computed by a = -w[0] / 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, facecolors='none')
pl.scatter(X[:, 0], X[:, 1], c=Y, cmap=pl.cm.Paired)

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

你可能感兴趣的:(支持向量SVM-py实现)