IndexError: too many indices for array
IndexError: too many indices for array: array is 0-dimensional, but 1 were i:
这个意思就是你的数组是0维的,但是你却取了1维的数据
出现这样的情况你因为你矩阵的维度出现了冗余情况,比如你把一组数放入矩阵,矩阵默认的维度是2,但是你其实只有一列数,或者你实际是2维的数据,你将其转为3维数据形式,也会报错。
因此可以先用np.shape()函数查看你的矩阵维度,是否出现了(n,)这样的情况。
然后对矩阵进行reshape重构,或者np.squeeze去除冗余自由度就可以避免这样的问题。
data = np.reshape(data,[-1,20,28,28])
or
Y_prediction_test = np.squeeze(d['Y_prediction_test'])
例如:
loss.data.cpu().numpy()[0]
总是报“IndexError: too many indices for array: array is 0-dimensional, but 1 were i:”这个错误,然后这里我用np.shape(loss.data.cpu().numpy()) 查看,发现输出为()
这里就发现“loss.data.cpu().numpy()”是0维的,但是你这里却取了0维的第一个数“loss.data.cpu().numpy()[0]”,所以是错误的
有两种解决方法,分别是:
(1)直接取该0维的数
loss.data.cpu().numpy()
(2)利用上述的reshape函数,将这个数组转为1维的
loss.data.cpu().numpy().reshape(1,-1)[0]
python:IndexError: too many indices for array_furuit的博客-CSDN博客