1

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'
halfer
  • 19,824
  • 17
  • 99
  • 186
Sridara Pavan
  • 11
  • 1
  • 2
  • Can you share a minimal working example. We can't run your code when we don't have access to things stored on your computer. – defladamouse Dec 01 '21 at 19:36
  • Have a look [at similar cases on this site](https://stackoverflow.com/search?q=TypeError%3A+unhashable+type%3A+%27numpy.ndarray%27). – halfer Dec 01 '21 at 21:26
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Dec 06 '21 at 22:51

2 Answers2

1

Use this:

 idc[np.argmax(answer[0])]
ouflak
  • 2,458
  • 10
  • 44
  • 49
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 13 '21 at 12:58
0

answer[0] is a numpy array and you are trying to use it as a key to the dictionary idc. Dictionary keys should be hashable, hence the error. Also please provide an example next time so other people can replicate your problem.

Z Li
  • 4,133
  • 1
  • 4
  • 19