matplotlib分散:TypeError:无法散列的类型:'numpy.ndarray' [英] matplotlib scatter: TypeError: unhashable type: 'numpy.ndarray'

查看:49
本文介绍了matplotlib分散:TypeError:无法散列的类型:'numpy.ndarray'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下错误:

  TypeError Traceback(最近一次通话)〜/.local/share/miniconda3/lib/python3.6/site-packages/matplotlib/colors.py在to_rgba(c,alpha)中154 尝试:->155 rgba = _colors_full_map.cache [c, alpha]156 except (KeyError, TypeError): # 不在缓存中,或不可散列.TypeError:无法散列的类型:'numpy.ndarray'

有问题的代码来自从 Coursera 下载的 .ipynb.

它在他们的系统上运行良好,但似乎我在本地有一个库版本控制问题.

代码是:

  plt.scatter(X [0,:],X [1,:],c = y,cmap = plt.cm.Spectral)

解决方案

更改:

plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)

收件人:

plt.scatter(X[0, :], X[1, :], c=y.ravel().tolist(), cmap=plt.cm.Spectral)

这会将数组 y 展平为一维,然后将其转换为列表, to_rgba 很高兴将其消化为可以散列的内容.<小时>

Coursera深度学习学生:

您可能会在 *util*.py 文件之一中找到有问题的代码行.在回溯中查找 scatter 以获取文件名.

我在论坛上看到这个问题被提出了大约 8 次.如果问题和答案有用,请点赞.

I'm getting the following error:

TypeError                                 Traceback (most recent call last)
~/.local/share/miniconda3/lib/python3.6/site-packages/matplotlib/colors.py in to_rgba(c, alpha)
    154     try:
--> 155         rgba = _colors_full_map.cache[c, alpha]
    156     except (KeyError, TypeError):  # Not in cache, or unhashable.

TypeError: unhashable type: 'numpy.ndarray'

The code in question is from a .ipynb downloaded from Coursera.

It works fine on their system, but it seems that I have a library versioning problem locally.

The code is:

plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)

解决方案

Change:

plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)

to:

plt.scatter(X[0, :], X[1, :], c=y.ravel().tolist(), cmap=plt.cm.Spectral)

This flattens the array y to be one-dimensional, and then turns it into a list, which to_rgba is happy to digest as something it can hash.


Coursera Deep Learning students:

You'll likely find the offending line(s) of code in one of the *util*.py files. Look for scatter in the traceback to get the filename.

I saw this question raised about 8 times on the forum. Please upvote both question and answer if they've been useful.

这篇关于matplotlib分散:TypeError:无法散列的类型:'numpy.ndarray'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆