我目前正在使用Python的scikit库进行多类线性核SVM。我的训练数据和测试数据样例如下所示:
模型数据:
x = [[20,32,45,33,32,44,0],[23,32,45,12,32,66,11],[16,32,45,12,32,44,23],[120,2,55,62,82,14,81],[30,222,115,12,42,64,91],[220,12,55,222,82,14,181],[30,222,315,12,222,64,111]]y = [0,0,0,1,1,2,2]
我想绘制决策边界并可视化数据集。有人可以帮助绘制这种类型的数据吗?
以上数据仅为模拟数据,因此您可以自由更改数值。如果至少能建议需要遵循的步骤,将会非常有帮助。提前感谢
回答:
你必须选择两个特征来进行此操作。原因是你无法绘制7D图。选择两个特征后,仅使用这些来可视化决策表面。
现在,你接下来会问的问题是:我如何选择这两个特征?。嗯,有很多方法。你可以进行单变量F值(特征排序)测试,看看哪些特征/变量是最重要的。然后你可以用这些来绘图。另外,我们也可以使用例如PCA将维度从7降到2。
使用鸢尾花数据集的2D图,绘制2个特征
from sklearn.svm import SVCimport numpy as npimport matplotlib.pyplot as pltfrom sklearn import svm, datasetsiris = datasets.load_iris()# 选择2个特征/变量来创建我们将要绘制的2D图。X = iris.data[:, :2] # 我们只取前两个特征。y = iris.targetdef make_meshgrid(x, y, h=.02): x_min, x_max = x.min() - 1, x.max() + 1 y_min, y_max = y.min() - 1, y.max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) return xx, yydef plot_contours(ax, clf, xx, yy, **params): Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) out = ax.contourf(xx, yy, Z, **params) return outmodel = svm.SVC(kernel='linear')clf = model.fit(X, y)fig, ax = plt.subplots()# 绘图的标题title = ('Decision surface of linear SVC ')# 设置绘图的网格X0, X1 = X[:, 0], X[:, 1]xx, yy = make_meshgrid(X0, X1)plot_contours(ax, clf, xx, yy, cmap=plt.cm.coolwarm, alpha=0.8)ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')ax.set_ylabel('y label here')ax.set_xlabel('x label here')ax.set_xticks(())ax.set_yticks(())ax.set_title(title)ax.legend()plt.show()
编辑:应用PCA以降低维度。
from sklearn.svm import SVCimport numpy as npimport matplotlib.pyplot as pltfrom sklearn import svm, datasetsfrom sklearn.decomposition import PCAiris = datasets.load_iris()X = iris.data y = iris.targetpca = PCA(n_components=2)Xreduced = pca.fit_transform(X)def make_meshgrid(x, y, h=.02): x_min, x_max = x.min() - 1, x.max() + 1 y_min, y_max = y.min() - 1, y.max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) return xx, yydef plot_contours(ax, clf, xx, yy, **params): Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) out = ax.contourf(xx, yy, Z, **params) return outmodel = svm.SVC(kernel='linear')clf = model.fit(Xreduced, y)fig, ax = plt.subplots()# 绘图的标题title = ('Decision surface of linear SVC ')# 设置绘图的网格X0, X1 = Xreduced[:, 0], Xreduced[:, 1]xx, yy = make_meshgrid(X0, X1)plot_contours(ax, clf, xx, yy, cmap=plt.cm.coolwarm, alpha=0.8)ax.scatter(X0, X1, c=y, cmap=plt.cm.coolwarm, s=20, edgecolors='k')ax.set_ylabel('PC2')ax.set_xlabel('PC1')ax.set_xticks(())ax.set_yticks(())ax.set_title('Decison surface using the PCA transformed/projected features')ax.legend()plt.show()
编辑1(2020年4月15日):
案例:使用鸢尾花数据集的3D图,绘制3个特征
from sklearn.svm import SVCimport numpy as npimport matplotlib.pyplot as pltfrom sklearn import svm, datasetsfrom mpl_toolkits.mplot3d import Axes3Diris = datasets.load_iris()X = iris.data[:, :3] # 我们只取前三个特征。Y = iris.target#使其成为二分类问题X = X[np.logical_or(Y==0,Y==1)]Y = Y[np.logical_or(Y==0,Y==1)]model = svm.SVC(kernel='linear')clf = model.fit(X, Y)# 分割平面的方程由所有x给出,使得np.dot(svc.coef_[0], x) + b = 0。# 求解w3 (z)z = lambda x,y: (-clf.intercept_[0]-clf.coef_[0][0]*x -clf.coef_[0][1]*y) / clf.coef_[0][2]tmp = np.linspace(-5,5,30)x,y = np.meshgrid(tmp,tmp)fig = plt.figure()ax = fig.add_subplot(111, projection='3d')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')ax.plot_surface(x, y, z(x,y))ax.view_init(30, 60)plt.show()