【问题标题】:IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices men2nIndexError:只有整数、切片 (`:`)、省略号 (`...`)、numpy.newaxis (`None`) 和整数或布尔数组是有效的索引 men2n
【发布时间】:2019-12-12 05:59:24
【问题描述】:

我收到这个错误我该如何解决这个问题

def fprop(self, input_data, target_data):
    tmp = [(t, i) for i, t in enumerate(target_data)]
    z = zip(*tmp)  # unzipping trick !
    cost = np.sum(np.log(input_data[z]))
    if self.size_average:
        cost /= input_data.shape[1]

错误

cost = np.sum(np.log(input_data[z]))
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

【问题讨论】:

  • 您到底想在这里做什么?你能先指定what你想做什么,而不是如何你想这样做吗?
  • 我只是想运行一堆代码然后得到这个错误并且错误行显示我的文件中的这段代码有问题
  • 错误表示您使用了错误的索引 - 这意味着 zinput_data[z] 中。所以首先使用print(z) 来查看变量中的内容,然后决定你尝试使用z 做什么,然后描述所有有问题的内容(不在评论中)。

标签: python numpy


【解决方案1】:

您应该告诉我们一些有关代码和数据的信息。是你写的还是别人的?它是新的,还是以前有效,可能在其他情况下?什么是:

input_data, target_data

但是让我们做一个合理的猜测,并逐步探索代码(这是你应该自己做的事情):

In [485]: alist = [1,3,0,2]                                                                                  
In [486]: tmp = [(t,i) for i,t in enumerate(alist)]                                                          
In [487]: tmp                                                                                                
Out[487]: [(1, 0), (3, 1), (0, 2), (2, 3)]
In [488]: z = zip(*tmp)                                                                                      
In [489]: z                                                                                                  
Out[489]: <zip at 0x7ff381d50188>

在 Python 3 中,zip 生成一个 zip 对象,一个生成器。这显然不能成为任何事物的索引。

我们可以把它变成一个列表,这是 Python2 会做的:

In [490]: list(z)                                                                                            
Out[490]: [(1, 3, 0, 2), (0, 1, 2, 3)]

但是元组列表有点好:

In [491]: x = np.arange(16).reshape(4,4)                                                                     
In [492]: x[_490]                                                                                            
/usr/local/bin/ipython3:1: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  #!/usr/bin/python3
Out[492]: array([ 4, 13,  2, 11])

如果我们把它变成一个元组的元组,numpy 可以在没有警告的情况下工作:

In [493]: x[tuple(_490)]                                                                                     
Out[493]: array([ 4, 13,  2, 11])

这与使用alist 为每列选择一个值相同:

In [494]: x[alist, np.arange(4)]                                                                             
Out[494]: array([ 4, 13,  2, 11])

In [495]: x                                                                                                  
Out[495]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

我最初应该用tuple 扩展zip

In [502]: x[tuple(zip(*tmp))]                                                                                
Out[502]: array([ 4, 13,  2, 11])

解决了这个问题,我现在怀疑这段代码最初适用于 Python2。

【讨论】:

    猜你喜欢
    • 2017-12-08
    • 2019-03-13
    • 2017-11-24
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多