【发布时间】:2020-07-28 04:56:13
【问题描述】:
我正在使用 scikit-learn 的乳腺癌数据集,该数据集包含 30 个特征。 在 this tutorial 之后,对于不那么令人沮丧的 iris 数据集,我想出了如何绘制区分“良性”和“恶性”类别的决策表面,在考虑数据集的前两个特征时(平均半径和平均纹理)。
这是我得到的:
但是当使用数据集中的所有特征时如何表示计算的超平面呢?
我知道我无法绘制 30 维的图形,但我想将运行 svm.SVC(kernel='linear', C=1).fit(X_train, y_train) 时创建的超平面“投影”到显示平均纹理与平均半径的二维散点图上。
我读到了using PCA to reduce dimensionality,但我怀疑拟合“简化”数据集与将计算出的所有 30 个特征的超平面投影到 2D 图上是不同的。
到目前为止,这是我的代码:
from sklearn import datasets
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn import svm
import numpy as np
#Load dataset
cancer = datasets.load_breast_cancer()
# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, test_size=0.3,random_state=109) # 70% training and 30% test
h = .02 # mesh step
C = 1.0 # Regularisation
clf = svm.SVC(kernel='linear', C=C).fit(X_train[:,:2], y_train) # Linear Kernel
x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1
y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.8)
scat=plt.scatter(X_train[:, 0], X_train[:, 1], c=y_train)
legend1 = plt.legend(*scat.legend_elements(),
loc="upper right", title="diagnostic")
plt.xlabel('mean_radius')
plt.ylabel('mean_texture')
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.show()
【问题讨论】:
-
我提供了 2 个示例。一个 2D 使用 3 个特征,一个 3D 使用 3 个特征。希望这会有所帮助
标签: python machine-learning scikit-learn svm