gpt4 book ai didi

python-2.7 - 属性错误 : 'module' object has no attribute 'cm'

转载 作者:行者123 更新时间:2023-12-04 02:19:26 25 4
gpt4 key购买 nike

我正在使用 Python2.7。这是 Udacity 的 Intro to Machine Learning 类(class)中的一个功能。调用该函数时,会显示一个图。但是,假设也显示了彩色区域,但未显示。

当我运行调用此函数的脚本时,图形会打开。当我关闭图形时,我看到以下消息:

Traceback (most recent call last):
File "your_algorithm.py", line 45, in <module>
prettyPicture(clf, features_test, labels_test)
File "e:\Projects\Udacity\Intro to Machine Learning\ud120-projects\choose_your_own\class_vis.py", line 22, in prettyPicture
plt.pcolormesh(xx, yy, Z, cmap=pl.cm.seismic)
AttributeError: 'module' object has no attribute 'cm'

在我看来就像 cmmatplotlib 的一个属性来自 matplotlib cm .于是,我改了 pl 'plt'。这消除了错误消息,但彩色区域仍然没有出现在绘图中。因此,我不太相信这是正确的。

为什么彩色区域不显示?

这是函数prettyPicture的代码:
import numpy as np
import matplotlib.pyplot as plt
import pylab as pl

def prettyPicture(clf, X_test, y_test):
x_min = 0.0; x_max = 1.0
y_min = 0.0; y_max = 1.0

# Plot the decision boundary. For that, we will assign a color to each
# point in the mesh [x_min, m_max]x[y_min, y_max].
h = .01 # step size in the mesh
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()])

# Put the result into a color plot
Z = Z.reshape(xx.shape)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())

plt.pcolormesh(xx, yy, Z, cmap=pl.cm.seismic)

# Plot also the test points
grade_sig = [X_test[ii][0] for ii in range(0, len(X_test)) if y_test[ii]==0]
bumpy_sig = [X_test[ii][1] for ii in range(0, len(X_test)) if y_test[ii]==0]
grade_bkg = [X_test[ii][0] for ii in range(0, len(X_test)) if y_test[ii]==1]
bumpy_bkg = [X_test[ii][1] for ii in range(0, len(X_test)) if y_test[ii]==1]

plt.scatter(grade_sig, bumpy_sig, color = "b", label="fast")
plt.scatter(grade_bkg, bumpy_bkg, color = "r", label="slow")
plt.legend()
plt.xlabel("bumpiness")
plt.ylabel("grade")

plt.savefig("test.png")

最佳答案

替换 cmap=pl.cm.seismiccmap=plt.cm.seismic
替换 plt.savefig("your_image.png")

figure = plt.gcf()
plt.show()
plt.draw()
figure.savefig("your_image.png", dpi=100)

关于python-2.7 - 属性错误 : 'module' object has no attribute 'cm' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35050062/

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