gpt4 book ai didi

matplotlib - 从线性 SVM 绘制 3D 决策边界

转载 作者:行者123 更新时间:2023-12-04 11:03:13 32 4
gpt4 key购买 nike

我已经使用 sklearn.svm.svc() 拟合了 3 个特征数据集。我可以使用 matplotlib 和 Axes3D 为每个观察点绘制点。我想绘制决策边界以查看拟合。我尝试调整 2D 示例来绘制决策边界,但无济于事。我知道 clf.coef_ 是决策边界的法线向量。我如何绘制它以查看它划分点的位置?

最佳答案

这是一个关于玩具数据集的示例。请注意,使用 matplotlib 在 3D 中绘图很时髦。 .有时,飞机后面的点可能看起来好像在飞机前面,因此您可能需要摆弄旋转图来确定发生了什么。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.svm import SVC

rs = np.random.RandomState(1234)

# Generate some fake data.
n_samples = 200
# X is the input features by row.
X = np.zeros((200,3))
X[:n_samples/2] = rs.multivariate_normal( np.ones(3), np.eye(3), size=n_samples/2)
X[n_samples/2:] = rs.multivariate_normal(-np.ones(3), np.eye(3), size=n_samples/2)
# Y is the class labels for each row of X.
Y = np.zeros(n_samples); Y[n_samples/2:] = 1

# Fit the data with an svm
svc = SVC(kernel='linear')
svc.fit(X,Y)

# The equation of the separating plane is given by all x in R^3 such that:
# np.dot(svc.coef_[0], x) + b = 0. We should solve for the last coordinate
# to plot the plane in terms of x and y.

z = lambda x,y: (-svc.intercept_[0]-svc.coef_[0][0]*x-svc.coef_[0][1]*y) / svc.coef_[0][2]

tmp = np.linspace(-2,2,51)
x,y = np.meshgrid(tmp,tmp)

# Plot stuff.
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z(x,y))
ax.plot3D(X[Y==0,0], X[Y==0,1], X[Y==0,2],'ob')
ax.plot3D(X[Y==1,0], X[Y==1,1], X[Y==1,2],'sr')
plt.show()
输出:

编辑(上面评论中的关键数学线性代数语句):
# The equation of the separating plane is given by all x in R^3 such that:
# np.dot(coefficients, x_vector) + intercept_value = 0.
# We should solve for the last coordinate: x_vector[2] == z
# to plot the plane in terms of x and y.

关于matplotlib - 从线性 SVM 绘制 3D 决策边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36232334/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com