简体   繁体   English

TypeError:不可散列的类型:Python 程序中的“numpy.ndarray”

[英]TypeError: unhashable type: 'numpy.ndarray' in Python program

Here is my code:这是我的代码:

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]])

Error:错误:

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'

answer[0] is a numpy array and you are trying to use it as a key to the dictionary idc . answer[0]是一个 numpy 数组,您正在尝试将其用作字典idc的键。 Dictionary keys should be hashable, hence the error.字典键应该是可散列的,因此是错误的。 Also please provide an example next time so other people can replicate your problem.下次还请提供一个示例,以便其他人可以复制您的问题。

Use this:用这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM