1、概念及参数调整
1、构造数据集
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
import seaborn as sns
sns.set()
from sklearn.datasets.samples_generator import make_blobs
X, y = make_blobs(n_samples=50, centers=2, random_state=0, cluster_std=0.60)
# print(X, y)
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
plt.show()
输出结果:
可以通过改变cluster_std从而改变生成数据的离散程度
2、随便画出三条线
xfit = np.linspace(-1, 3.5) # 默认num=50,生成50个点
# print(len(xfit))
plt.plot([0.6], [2.1], 'x', color='red', markeredgewidth=2, markersize=10) # 设置一个异常点
for m, b in [(1, 0.65), (0.5, 1.6), (-0.2, 2.9)]:
plt.plot(xfit, m*xfit + b, '-k')
plt.xlim(-1, 3.5)
plt.show()
输出结果:
3、最小化雷区
# 最小化雷区
xfit = np.linspace(-1, 3.5) # 生成50个点
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap='autumn')
# yfit + d 表示雷区的宽度
for m, b, d in [(1, 0.65, 0.33), (0.5, 1.6, 0.55), (-0.2, 2.9, 0.2)]:
yfit = m * xfit + b
plt.plot(xfit, yfit, '-k')
plt.fill_between(xfit, yfit - d, yfit + d, edgecolor='none', color='#AAAAAA', alpha=0.4)
plt.xlim(-1, 3.5)
plt.show()
输出:
4、训练模型并且图形化显示
# 训练模型
from sklearn.svm import SVC
model = SVC(kernel='linear')
model.fit(X, y)
# 绘图函数
def plot_svc_decision_function(model, ax=None, plot_support=True):
"""Plot the decision function for a 2D SVC"""
if ax is None:
ax = plt.gca()
xlim = ax.get_xlim()
ylim = ax.get_ylim()
# creat grid to evaluate model
x = np.linspace(xlim[0], xlim[1], 30)
y = np.linsp