首页 > 解决方案 > TypeError: unhashable type: 'numpy.ndarray' mnist

问题描述

有人可以在这里帮助我,我无法解决这个问题。

import matplotlib.pyplot as plt
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=False)

dataWithLabels = zip(mnist.train.labels, mnist.train.images)

digitDict = {}

for i in range(0,10):
    digitDict[i] = []
    
for i in dataWithLabels:
    digitDict[i[0]].append(i[1])

for i in range(0,10):
    digitDict[i] = np.matrix(digitDict[i])
    print("Digit {0} matrix shape: {1}".format(i,digitDict[i].shape))




---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-05052c24d917> in <module>()
 15 # Assign a list of image vectors to each corresponding digit class index.
 16 for i in dataWithLabels:
---> 17     digitDict[i[0]].append(i[1])
 18 
 19 # Convert the lists into numpy matricies. (could be done above, but I claim ignorace)

TypeError: unhashable type: 'numpy.ndarray'

标签: python

解决方案


我不确定标签的形状。尝试以下选项:

  • i[0](就像你已经拥有的一样)
  • i[0][0](如果它们是长度为 1 的数组)<== 这是我最好的猜测
  • float(i[0])
  • float(i[0][0])

如果这些都不起作用,请提供给我们mnist.train.labels.shape


推荐阅读