gpt4 book ai didi

python - matplotlib.scatter() 不能在 Python 3.6 上使用 Numpy

转载 作者:太空狗 更新时间:2023-10-29 20:35:05 25 4
gpt4 key购买 nike

我很难理解为什么 matplotlib.scatter() 在使用 Python 3.6.3 作为解释器时不断抛出以下异常,但在使用我的 MacBook 内置的 2.7 时工作正常:

Traceback (most recent call last):
File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/colors.py", line 132, in to_rgba
rgba = _colors_full_map.cache[c, alpha]
TypeError: unhashable type: 'numpy.ndarray'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 4050, in scatter
colors = mcolors.to_rgba_array(c)
File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/colors.py", line 233, in to_rgba_array
result[i] = to_rgba(cc, alpha)
File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/colors.py", line 134, in to_rgba
rgba = _to_rgba_no_colorcycle(c, alpha)
File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/colors.py", line 189, in _to_rgba_no_colorcycle
raise ValueError("RGBA sequence should have length 3 or 4")
ValueError: RGBA sequence should have length 3 or 4

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/thomastiotto/Documents/USI/1 semester/Machine Learning/Assignments/Assignment 1/skeleton.py", line 458, in <module>
main()
File "/Users/thomastiotto/Documents/USI/1 semester/Machine Learning/Assignments/Assignment 1/skeleton.py", line 455, in main
run_part1()
File "/Users/thomastiotto/Documents/USI/1 semester/Machine Learning/Assignments/Assignment 1/skeleton.py", line 156, in run_part1
plot_boundary(p, X, T)
File "/Users/thomastiotto/Documents/USI/1 semester/Machine Learning/Assignments/Assignment 1/skeleton.py", line 142, in plot_boundary
plot_data(X, targets)
File "/Users/thomastiotto/Documents/USI/1 semester/Machine Learning/Assignments/Assignment 1/skeleton.py", line 129, in plot_data
plt.scatter(X[:, 0], X[:, 1], s=40, c=T, cmap=plt.cm.Spectral)
File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3357, in scatter
edgecolors=edgecolors, data=data, **kwargs)
File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/__init__.py", line 1710, in inner
return func(ax, *args, **kwargs)
File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 4055, in scatter
raise ValueError(msg.format(c.shape, x.size, y.size))
ValueError: c of shape (11, 1) not acceptable as a color sequence for x with size 11, y with size 11

我正在尝试执行以下代码:

def plot_data(X, T):
"""
Plots the 2D data as a scatterplot
"""
plt.scatter(X[:, 0], X[:, 1], s=40, c=T, cmap=plt.cm.Spectral)


def plot_boundary(model, X, targets, threshold=0.0):
"""
Plots the data and the boundary lane which separates the input space into two classes.
"""
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 200), np.linspace(y_min, y_max, 200))
X_grid = np.c_[xx.ravel(), yy.ravel()]
y = model.forward(X_grid)
plt.contourf(xx, yy, y.reshape(*xx.shape) < threshold, alpha=0.5)
plot_data(X, targets)
plt.ylim([y_min, y_max])
plt.xlim([x_min, x_max])

我将函数称为:

plot_boundary(p, X, T)

X 是一个 [11x2] Numpy 数组。

如果我将解释器设置为 MacOS 上的内置 Python 2.7,代码运行正常,将其设置为 Python 3.6.2 或 3.6.3 会导致上述错误。 Matplotlib 版本前者为 1.3.1,后者为 2.1。

有什么想法吗?

最佳答案

c 需要一维数组。

T.ravel() 应该可以解决问题。

关于python - matplotlib.scatter() 不能在 Python 3.6 上使用 Numpy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46746839/

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