J'ai du mal à comprendre pourquoi matplotlib.scatter () continue de générer l'exception suivante lors de l'utilisation de Python 3.6.3 en tant qu'interpréteur, mais fonctionne correctement lors de l'utilisation de la version 2.7 intégrée à mon MacBook:
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
J'essaye d'exécuter le code suivant:
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])
J'appelle la fonction comme:
plot_boundary(p, X, T)
X étant un tableau [11x2] de Numpy.
Si je configure mon interpréteur sur Python 2.7 intégré sur MacOS, le code fonctionne correctement, le réglage sur Python 3.6.2 ou 3.6.3 entraîne l'erreur ci-dessus. La version de Matplotlib est 1.3.1 dans le premier cas et 2.1 dans le dernier.
Des idées?
c nécessite un tableau à une dimension.
T.ravel () devrait faire l'affaire.
plt.scatter(X[:, 0], X[:, 1], s=40, c=T, cmap=plt.cm.Spectral)
Dans cette fonction c, nécessite un tableau 1-D. Comme mentionné dans la réponse ci-dessus, utilisez T.ravel ou T.reshape (400,)
Utilisez 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)
Sortie :
(2, 6)
(12)