最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

【环球聚看点】线性SVM决策过程的可视化

来源:博客园

线性 SVM 决策过程的可视化

导入模块

from sklearn.datasets import make_blobsfrom sklearn.svm import SVCimport matplotlib.pyplot as pltimport numpy as np

实例化数据集,可视化数据集

x, y = make_blobs(n_samples=50, centers=2, random_state=0, cluster_std=0.5)# plt.scatter(x[:, 0], x[:, 1], c=y, cmap="rainbow")# plt.xticks([])# plt.yticks([])

画决策边界

# 首先要有散点图plt.scatter(x[:, 0], x[:, 1], c=y, s=50, cmap="rainbow")# 获取当前的子图,如果不存在,则创建新的子图ax = plt.gca()# 获取平面上两条坐标轴的最大值和最小值xlim = ax.get_xlim()  # (-0.46507821433712176, 3.1616962549275827)ylim = ax.get_ylim()  # (-0.22771027454251097, 5.541407658378895)# 在最大值和最小值之间形成30个规律的数据axisx = np.linspace(xlim[0], xlim[1], 30)  # shape(30,)axisy = np.linspace(ylim[0], ylim[1], 30)  # shape(30,)# 将axis(x, y)转换成二维数组axisx, axisy = np.meshgrid(axisx, axisy)  # axisx, axisy = shape((30, 30)# 将axisx, axisy 组成 900 * 2 的数组xy = np.vstack([axisx.ravel(), axisy.ravel()]).T  # shape(900, 2)# 展示xy画出的网格图# plt.scatter(xy[:, 0], xy[:, 1], s=1, c="grey", alpha=0.3)


(资料图片仅供参考)

建模,计算决策边界并找出网格上每个点到决策边界的距离

# 建模,通过fit计算出对应的决策边界clf = SVC(kernel="linear").fit(x, y)# 重要接口decision_function,返回每个输入的样本所对应的到决策边界的距离z = clf.decision_function(xy).reshape(axisx.shape)  # shape(30, 30)plt.scatter(x[:, 0], x[:, 1], c=y, s=50, cmap="rainbow")ax = plt.gca()# 画决策边界和平行于决策边界的超平面ax.contour(    axisx,    axisy,    z,    colors="k",    levels=[-1, 0, 1],    linestyles=["--", "-", "--"],    alpha=0.5,)# 设置xyk刻度ax.set_xlim(xlim)ax.set_ylim(ylim)
(-0.22771027454251097, 5.541407658378895)

将绘图过程包装成函数

# 将上述过程包装成函数:def plot_svc_decision_function(model: SVC, ax=None):    """画出线性数据中策边界和平行于决策边界的超平面    Args:        model (SVC): 模型        ax (_type_, optional): 子图. Defaults to None.    """    if ax is None:        ax = plt.gca()  # 获取子图或新建子图    xlim = ax.get_xlim()  # 获取子图x轴最大值和最小值    ylim = ax.get_ylim()  # 获取子图y轴最大值和最小值    # 在最大值和最小值之间形成30个规律的数据    x = np.linspace(xlim[0], xlim[1], 30)    y = np.linspace(ylim[0], ylim[1], 30)    # 将x,y转换成x^2, y^2的二维数组    Y, X = np.meshgrid(y, x)    # 组成 xy * 2 的数组    xy = np.vstack([X.ravel(), Y.ravel()]).T    # 获取每个输入的样本所对应的到决策边界的距离    P = model.decision_function(xy).reshape(X.shape)    # 画图    ax.contour(        X, Y, P, colors="k", levels=[-1, 0, 1], alpha=0.5, linestyles=["--", "-", "--"]    )    ax.set_xlim(xlim)    ax.set_ylim(ylim)
clf = SVC(kernel="linear").fit(x, y)plt.scatter(x[:, 0], x[:, 1], s=50, cmap="rainbow", c=y)plot_svc_decision_function(clf)

# 测试x1, y1 = make_blobs(n_samples=500, centers=2, cluster_std=0.9)clf1 = SVC(kernel="linear").fit(x1, y1)plt.scatter(x1[:, 0], x1[:, 1], c=y1, s=25, cmap="rainbow")plot_svc_decision_function(clf1)

探索模型

# 根据决策边界,对X中的样本进行分类,返回的结构为n_samplesclf.predict(x)
array([1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1,       1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1,       0, 1, 1, 0, 1, 0])
# 返回给定测试数据和标签的平均准确度clf.score(x, y)
1.0
# 返回支持向量clf.support_vectors_
array([[0.5323772 , 3.31338909],       [2.11114739, 3.57660449],       [2.06051753, 1.79059891]])
# 返回每个类中支持向量的个数clf.n_support_
array([2, 1], dtype=int32)

推广到非线性情况

from sklearn.datasets import make_circlesx, y = make_circles(n_samples=300, factor=0.1, noise=0.1)# x.shape(300, 2)# y.shape(300,)plt.scatter(x[:, 0], x[:, 1], c=y, s=50, cmap="rainbow")plt.show()

# 测试plot_svc_decision_functionclf = SVC(kernel="linear").fit(x, y)plt.scatter(x[:, 0], x[:, 1], s=50, cmap="rainbow", c=y)plot_svc_decision_function(clf)clf.score(x, y)
0.6733333333333333

为非线性数据增加维度并绘制 3D 图像

r = np.exp(-(x**2).sum(1))  # r.shape(300,)rlim = np.linspace(min(r), max(r), 100)
from mpl_toolkits import mplot3d# 定义一个绘制三维图像的函数# elev表示上下旋转的角度# azim表示平行旋转的角度def plot_3D(elev=30, azim=30, x=x, y=y):    ax = plt.subplot(projection="3d")    ax.scatter3D(x[:, 0], x[:, 1], r, c=y, s=50, cmap="rainbow")    ax.view_init(elev=elev, azim=azim)    ax.set_xlabel("x")    ax.set_ylabel("y")    ax.set_zlabel("r")    plt.show()plot_3D()

将上述过程放到 Jupyter Notebook 中运行

# 如果放到jupyter notebook中运行from sklearn.svm import SVCimport matplotlib.pyplot as pltimport numpy as npfrom sklearn.datasets import make_circlesX, y = make_circles(100, factor=0.1, noise=0.1)plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap="rainbow")def plot_svc_decision_function(model, ax=None):    if ax is None:        ax = plt.gca()    xlim = ax.get_xlim()    ylim = ax.get_ylim()    x = np.linspace(xlim[0], xlim[1], 30)    y = np.linspace(ylim[0], ylim[1], 30)    Y, X = np.meshgrid(y, x)    xy = np.vstack([X.ravel(), Y.ravel()]).T    P = model.decision_function(xy).reshape(X.shape)    ax.contour(        X, Y, P, colors="k", levels=[-1, 0, 1], alpha=0.5, linestyles=["--", "-", "--"]    )    ax.set_xlim(xlim)    ax.set_ylim(ylim)clf = SVC(kernel="linear").fit(X, y)plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap="rainbow")plot_svc_decision_function(clf)r = np.exp(-(X**2).sum(1))rlim = np.linspace(min(r), max(r), 100)from mpl_toolkits import mplot3ddef plot_3D(elev=30, azim=30, X=X, y=y):    ax = plt.subplot(projection="3d")    ax.scatter3D(X[:, 0], X[:, 1], r, c=y, s=50, cmap="rainbow")    ax.view_init(elev=elev, azim=azim)    ax.set_xlabel("x")    ax.set_ylabel("y")    ax.set_zlabel("r")    plt.show()from ipywidgets import interact, fixedinteract(plot_3D, elev=[0, 30], azip=(-180, 180), X=fixed(X), y=fixed(y))plt.show()

interactive(children=(Dropdown(description="elev", index=1, options=(0, 30), value=30), IntSlider(value=30, de…

关键词: