我可以使用Scikit learn和Matplotlib绘制3个特征(在3D空间中)的决策边界吗?

时间:2017-07-16 02:43:22

标签: python matplotlib scikit-learn python-3.6

我的问题与此问题相同:

Can I plot the SVM decision boundary for 3 features(in 3D space) using Scikit learn?

其中一个答案是:

  

举个例子   scikit-learn.org/stable/auto_examples/svm/plot_iris.html。如果你   有3个功能,你所要做的就是为你的z添加一个z坐标   meshgrid的定义与x和y类似,并将其传递给您的预测   方法和contourf方法。

所以我尝试这样做,将2D示例转换为3D:

print(__doc__)

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets

# import some data to play with
iris = datasets.load_iris()
X = iris.data[:, :3]  # we only take the first two features. We could
                      # avoid this ugly slicing by using a two-dim dataset
y = iris.target

h = .02  # step size in the mesh

# we create an instance of SVM and fit out data. We do not scale our
# data since we want to plot the support vectors
C = 1.0  # SVM regularization parameter
svc = svm.SVC(kernel='linear', C=C).fit(X, y)
rbf_svc = svm.SVC(kernel='rbf', gamma=0.7, C=C).fit(X, y)
poly_svc = svm.SVC(kernel='poly', degree=3, C=C).fit(X, y)
lin_svc = svm.LinearSVC(C=C).fit(X, y)

# create a mesh to plot in
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
z_min, z_max = X[:, 2].min() - 1, X[:, 2].max() + 1
xx, yy, zz = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h),
                     np.arange(z_min, z_max, h))

# title for the plots
titles = ['SVC with linear kernel',
          'LinearSVC (linear kernel)',
          'SVC with RBF kernel',
          'SVC with polynomial (degree 3) kernel']


for i, clf in enumerate((svc, lin_svc, rbf_svc, poly_svc)):
    # Plot the decision boundary. For that, we will assign a color to each
    # point in the mesh [x_min, x_max]x[y_min, y_max].
    plt.subplot(2, 2, i + 1, projection='3d')
    plt.subplots_adjust(wspace=0.4, hspace=0.4)

    pred = clf.predict(np.c_[xx.ravel(), yy.ravel(), zz.ravel()])

    # Put the result into a color plot
    pred = pred.reshape(xx.shape)
    plt.contourf(xx, yy, zz, pred, cmap=plt.cm.coolwarm, alpha=0.8)

    # Plot also the training points
    plt.scatter(X[:, 0], X[:, 1], X[:, 2], c=y, cmap=plt.cm.coolwarm)
    plt.xlabel('Sepal length')
    plt.ylabel('Sepal width')
    plt.xlim(xx.min(), xx.max())
    plt.ylim(yy.min(), yy.max())
    plt.zlim(zz.min(), zz.max())
    plt.xticks(())
    plt.yticks(())
    plt.zticks(())
    plt.title(titles[i])

plt.show()

但无论我如何调整它,我每次都会得到同样的错误:

=========================
3D surface (checkerboard)
=========================

Demonstrates plotting a 3D surface colored in a checkerboard pattern.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-54-b750fe83f0a6> in <module>()
     46     # Put the result into a color plot
     47     pred = pred.reshape(xx.shape)
---> 48     plt.contourf(xx, yy, zz, pred, cmap=plt.cm.coolwarm, alpha=0.8)
     49 
     50     # Plot also the training points

C:\Users\BNielson\Anaconda3\envs\PythonMachineLearning\lib\site-packages\matplotlib\pyplot.py in contourf(*args, **kwargs)
   2873                       mplDeprecation)
   2874     try:
-> 2875         ret = ax.contourf(*args, **kwargs)
   2876     finally:
   2877         ax._hold = washold

C:\Users\BNielson\Anaconda3\envs\PythonMachineLearning\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py in contourf(self, X, Y, Z, *args, **kwargs)
   2194 
   2195         jX, jY, jZ = art3d.rotate_axes(X, Y, Z, zdir)
-> 2196         cset = Axes.contourf(self, jX, jY, jZ, *args, **kwargs)
   2197         self.add_contourf_set(cset, zdir, offset)
   2198 

C:\Users\BNielson\Anaconda3\envs\PythonMachineLearning\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
   1889                     warnings.warn(msg % (label_namer, func.__name__),
   1890                                   RuntimeWarning, stacklevel=2)
-> 1891             return func(ax, *args, **kwargs)
   1892         pre_doc = inner.__doc__
   1893         if pre_doc is None:

C:\Users\BNielson\Anaconda3\envs\PythonMachineLearning\lib\site-packages\matplotlib\axes\_axes.py in contourf(self, *args, **kwargs)
   5827             self.cla()
   5828         kwargs['filled'] = True
-> 5829         contours = mcontour.QuadContourSet(self, *args, **kwargs)
   5830         self.autoscale_view()
   5831         return contours

C:\Users\BNielson\Anaconda3\envs\PythonMachineLearning\lib\site-packages\matplotlib\contour.py in __init__(self, ax, *args, **kwargs)
    862         self._transform = kwargs.get('transform', None)
    863 
--> 864         self._process_args(*args, **kwargs)
    865         self._process_levels()
    866 

C:\Users\BNielson\Anaconda3\envs\PythonMachineLearning\lib\site-packages\matplotlib\contour.py in _process_args(self, *args, **kwargs)
   1427                 self._corner_mask = mpl.rcParams['contour.corner_mask']
   1428 
-> 1429             x, y, z = self._contour_args(args, kwargs)
   1430 
   1431             _mask = ma.getmask(z)

C:\Users\BNielson\Anaconda3\envs\PythonMachineLearning\lib\site-packages\matplotlib\contour.py in _contour_args(self, args, kwargs)
   1506             args = args[1:]
   1507         elif Nargs <= 4:
-> 1508             x, y, z = self._check_xyz(args[:3], kwargs)
   1509             args = args[3:]
   1510         else:

C:\Users\BNielson\Anaconda3\envs\PythonMachineLearning\lib\site-packages\matplotlib\contour.py in _check_xyz(self, args, kwargs)
   1540 
   1541         if z.ndim != 2:
-> 1542             raise TypeError("Input z must be a 2D array.")
   1543         else:
   1544             Ny, Nx = z.shape

TypeError: Input z must be a 2D array.

0 个答案:

没有答案