import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
# This is used for out dataset
# dataset at : https://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_breast_cancer.html#sklearn.datasets.load_breast_cancer
from sklearn.datasets import load_breast_cancer
dataCancer = load_breast_cancer()
data = dataCancer.data[:,0:2] # 原数据集有30个特征,我们这里只取2个特征target = dataCancer.target
# 获得‘标签’
model = svm.SVC(kernel = 'linear', C=10000)model.fit(data,target)
# plots the pointsplt.scatter(data[:,0],data[:,1],c=target,s=30,cmap=plt.cm.prism)
# Creates the axis bounds for the gridaxis = plt.gca()x_limit = axis.get_xlim()y_limit = axis.get_ylim()
# Creates a grid to evaluate modelx = np.linspace(x_limit[0],x_limit[1],50) # 生成等差数列y = np.linspace(y_limit[0],y_limit[1],50)X,Y = np.meshgrid(x,y) # 生成坐标矩阵xy = np.c_[X.ravel(),Y.ravel()]# .c_左右 矩阵相加 / .r_ 上下矩阵相加# .ravel() //将多维数组转换为一维数组# 这里xy 就相当于 [ [x1,y1],[x2,y2],[x3,y3]...]print(x)
# Creates the decision line for the data points,# use model.predict if you are classifying more than two.decision_line = model.decision_function(xy).reshape(Y.shape)
# decision_function(): 计算样本点到分割超平面的 函数距离# ?????????????? xy???
# Plot the decision line and the marginsaxis.contour(X,Y,decision_line,colors = 'k',levels=[0],linestyles=['-'])
# Shows the support vectors that determine the desision line.axis.scatter(model.support_vectors_[:,0],model.support_vectors_[:,1],s=100, linewidth=1,facecolors='none',edgecolors='k')plt.show()
SVC
最新推荐文章于 2024-04-26 23:20:04 发布