【问题标题】:TypeError: unhashable type: 'numpy.ndarray' in Python programTypeError:不可散列的类型:Python程序中的'numpy.ndarray'
【发布时间】:2022-01-08 09:27:20
【问题描述】:

这是我的代码:

dic = test_dataset.class_indices
idc = {k:v for v, k in dic.items()}

img = load_img( r'C:\Users\sreep\Downloads\Alzheimer_s Dataset\test\NonDemented\26 (62).jpg', target_size = (224,224,3))
img = img_to_array(img)
img = img/255
imshow(img)
plt.axis('off')
img = np.expand_dims(img,axis=0)
answer = model.predict(img)
probability = round(np.max(model.predict(img)*100),2)

print(probability, '% chances are there that the image is',idc[answer[0]])

错误:

TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_17688/2364562646.py in <module>
     13 probability = round(np.max(model.predict(img)*100),2)
     14 
---> 15 print(probability, '% chances are there that the image is',idc[answer[0]])

TypeError: unhashable type: 'numpy.ndarray'

【问题讨论】:

  • 你能分享一个最小的工作示例吗?当我们无法访问存储在您计算机上的内容时,我们无法运行您的代码。
  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: python anaconda junit-jupiter


【解决方案1】:

answer[0] 是一个 numpy 数组,您正试图将其用作字典 idc 的键。字典键应该是可散列的,因此是错误的。另外请下次提供一个示例,以便其他人可以复制您的问题。

【讨论】:

    【解决方案2】:

    使用这个:

     idc[np.argmax(answer[0])]
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2020-05-22
    • 2012-02-19
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多