Ich habe einige Schwierigkeiten zu verstehen, warum matplotlib.scatter () die folgende Ausnahme auslöst, wenn Python 3.6.3 als Interpreter verwendet wird, funktioniert aber gut, wenn ich 2.7 verwendet, das in meinem MacBook integriert ist:
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
Ich versuche folgenden Code auszuführen:
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])
Ich rufe die Funktion als:
plot_boundary(p, X, T)
Wobei X ein [11x2] Numpy-Array ist.
Wenn ich meinen Interpreter unter MacOS auf den integrierten Python 2.7 stelle, läuft der Code einwandfrei. Wenn Sie ihn auf Python 3.6.2 oder 3.6.3 setzen, wird der obige Fehler angezeigt. Die Matplotlib-Version ist im ersten Fall 1.3.1 und im letzteren Fall 2.1.
Irgendwelche Ideen?
c erfordert ein eindimensionales Array.
T.ravel () sollte den Trick tun.
plt.scatter(X[:, 0], X[:, 1], s=40, c=T, cmap=plt.cm.Spectral)
In dieser Funktion c ist ein 1-D-Array erforderlich. Wie in der obigen Antwort erwähnt, verwenden Sie T.ravel oder T.reshape (400,).
Sie können auch c=np.squeeze(T)
verwenden.
Ich denke, dass das Problem hier tatsächlich Teil eines größeren Python/Numpy-Problems ist - was darin besteht, dass es nicht möglich ist, auf die korrekte Verwendung von 1D-Arrays zu schließen. Dies verschwendet eine Menge Zeit für das Codieren und Debuggen.
Verwenden Sie np.reshape :
import numpy as np
t1 = np.array([[1,2,3,4,5,6] , [7,8,9,10,11,12]])
t1_single = np.reshape(t1, -1)
print(t1.shape)
print(t1_single.shape)
Ausgabe :
(2, 6)
(12,)