Support Vector Machine 支持向量机散点分类

示例网址:
https://scikit-learn.org/stable/auto_examples/svm/plot_separating_hyperplane.html#sphx-glr-auto-examples-svm-plot-separating-hyperplane-py
支持向量机Scikit-learn官方文档:
https://scikit-learn.org/stable/modules/svm.html#svm-classification
支持向量机散点分类

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.datasets import make_blobs

X, y = make_blobs(n_samples=200, centers=2, random_state=6)

# fit the model, don't regularize for illustration purposes
clf = svm.SVC(kernel='linear', C=1000)
clf.fit(X, y)

plt.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap='Paired')

# plot the decision function
ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()

# create grid to evaluate model
xx = np.linspace(xlim[0], xlim[1], 30)
yy = np.linspace(ylim[0], ylim[1], 30)
YY, XX = np.meshgrid(yy, xx)
xy = np.vstack([XX.ravel(), YY.ravel()]).T
# xy = np.c_[XX.ravel(), YY.ravel()]相同
Z = clf.decision_function(xy).reshape(XX.shape)

# plot decision boundary and margins
ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,linestyles=['--', '-', '--'])
# plot support vectors
ax.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=50,linewidth=1, facecolors='red')
plt.show()

参考网址:

matplotlib contourf 画等高线图:
https://blog.csdn.net/Mr_Cat123/article/details/80677525

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-10,10,0.01)
y = np.arange(-10,10,0.01)
#也可以用x = np.linspace(-10,10,100)表示从-10到10,分100份

#将原始数据变成网格数据形式
X,Y = np.meshgrid(x,y)
Z = X**2+Y**2

#设置打开画布大小,长10,宽6
plt.figure(figsize=(10,6))
#填充颜色,f即filled
plt.contourf(X,Y,Z)
#画等高线
plt.contour(X,Y,Z)
plt.show()

等高线图

numpy.meshgrid(*xi, **kwargs):

Return coordinate matrices from two or more coordinate vectors.
Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, given one-dimensional coordinate arrays x1, x2,…, xn.
https://docs.scipy.org/doc/numpy-1.8.1/reference/generated/numpy.meshgrid.html#numpy.meshgrid

>>> x = np.linspace(0, 1, 3)
>>> y = np.linspace(0, 1, 2)
>>> xv, yv = meshgrid(x, y)
>>> xv
array([[ 0. ,  0.5,  1. ],
       [ 0. ,  0.5,  1. ]])
>>> yv
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.]])

绘制三维曲面图:
使用meshgrid函数以及Axes3D [plot_surface]
https://blog.csdn.net/Scc_hy/article/details/81455795

import mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig)

x = np.arange(-4, 4, 0.25)
y = np.arange(-4, 4, 0.25)
x, y = np.meshgrid(x, y)
r = np.sqrt(x**2 + y**2)
z = np.sin(r)

ax.plot_surface(x, y, z, rstride = 1,   # row 行步长
                 cstride = 2,           # colum 列步长
                 cmap='hot')     	   # 渐变颜色
ax.contourf(x, y, z, 
            zdir='z',  # 使用数据方向
            offset=-2, # 填充投影轮廓位置
            cmap='hot')
ax.set_zlim(-2, 2)
plt.show()

三维曲面图

注意事项:

kernel : string, optional (default=’rbf’)
Specifies the kernel type to be used in the algorithm. It must be one of ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’ or a callable. If none is given, ‘rbf’ will be used.

decision_function(X):
Distance of the samples X to the separating hyperplane.

plt.gcf()和plt.gca():
当前的图表和子图可以使用plt.gcf()和plt.gca()获得,分别表示"Get Current Figure"和"Get Current Axes"。

软化因子C:
我们迄今为止的讨论集中在非常干净的数据集,其中存在完美的决策边界。 但是如果你的数据有一定的重叠呢?为了处理这种情况,SVM 实现了软化因子,即“软化”边距:也就是说,如果允许更好的匹配,它允许某些点进入边距。 边缘的硬度由调整参数控制,通常称为C。 对于非常大的C,边距是硬的,点不能进入。 对于较小的C,边缘较软,可以扩展并包含一些点。

clf = svm.SVC(kernel='linear', C=0.1)

猜你喜欢

转载自blog.csdn.net/weixin_34275246/article/details/85007194
今日推荐