在下面的plot_decision函数中,与matplotlib一起使用时,我以前在Jupyter Notebook中收到“内存错误”,因此我将分辨率从0.2降低到了“ 2”。现在我收到一个
“ TypeError:contourf的参数过多;请参阅help(contourf)”
在尝试绘图时。.我做了一些微不足道的调整尝试,希望能对您有所帮助。
###绘图决策功能from matplotlib.colors import ListedColormap
def plot_decision(X, y, classifier, test_idx=None, resolution=2, figsize=(8,8)):
# setup marker generator and color map
markers = ('s', 'x', 'o', '^', 'v')
colors = ('#cc0000', '#003399', '#00cc00', '#999999', '#66ffff')
cmap = ListedColormap(colors[:len(np.unique(y))])
# get dimensions
x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
x3_min, x3_max = X[:, 1].min() - 1, X[:, 1].max() + 1
x4_min, x4_max = X[:, 1].min() - 1, X[:, 1].max() + 1
x5_min, x5_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx1, xx2, xx3, xx4, xx5 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max, resolution), np.arange(x3_min, x3_max, resolution), np.arange(x4_min, x4_max, resolution), np.arange(x5_min, x5_max, resolution))
xmin = xx1.min()
xmax = xx1.max()
ymin = xx2.min()
ymax = xx2.max()
ymin = xx3.min()
ymax = xx3.max()
ymin = xx4.min()
ymax = xx4.max()
ymin = xx5.min()
ymax = xx5.max()
# create the figure
fig, ax = plt.subplots(figsize=figsize)
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)
# plot the decision surface
Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel(), xx3.ravel(), xx4.ravel(), xx5.ravel()]).T)
Z = Z.reshape(xx1.shape)
ax.contourf(xx1, xx2, xx3, xx4, xx5, Z, alpha=0.4, cmap=cmap, zorder=1)
#ax.contourf(xx1, xx2, xx3, xx4, xx5, VWXYZ, alpha=0.4, cmap=cmap, zorder=1)
# plot all samples
for idx, cl in enumerate(np.unique(y)):
ax.scatter(x=X[y == cl, 0],
y=X[y == cl, 1],
alpha=0.6,
c=cmap(idx),
edgecolor='black',
marker='o',#markers[idx],
s=50,
label=cl,
zorder=3)
# highlight test samples
if test_idx:
X_test, y_test = X[test_idx, :], y[test_idx]
ax.scatter(X_test[:, 0],
X_test[:, 1],
c='w',
alpha=1.0,
edgecolor='black',
linewidths=1,
marker='o',
s=150,
label='test set',
zorder=2)
##### END PLOT决策功能
#####开始图功能
# visualize the model's decision regions to see how it separates the samples
X_combined = np.vstack((X_train, X_test))
y_combined = np.hstack((y_train, y_test))
plot_decision(X=X_combined, y=y_combined, classifier=dtree)
plt.xlabel('Parameters')
plt.ylabel('Survivors')
plt.legend(loc='upper left')
plt.show()
感谢任何愿意提供帮助的人:)