【问题标题】:only integer scalar arrays can be converted to a scalar index只有整数标量数组可以转换为标量索引
【发布时间】:2019-06-19 02:58:55
【问题描述】:

我正在查看的代码是:

ids = np.delete(ids, np.concatenate([ids[-1]], np.where(ious > thresh)[0]))

不同变量的值是:

ID:[3 2 0 1]

ious:[0. 0.65972222 0.65972222]

阈值:0.5

np.where(ious > [thresh])[0]) 的输出是[1 2]

我似乎得到的错误是:

    np.where(ious > [thresh])[0]))
TypeError: only integer scalar arrays can be converted to a scalar index

我确信除了thresh 之外的每个变量都是numpy 数组。那么究竟出了什么问题。

【问题讨论】:

  • concatenate 的第二个参数必须是标量,即轴。
  • @hpaulj where 的输出是索引列表,我将对问题进行更改以表明这一点。

标签: python-3.x numpy


【解决方案1】:
In [187]: ids=np.array([3,2,0,1])                                                         
In [188]: ious=np.array([0.  ,       0.65972222, 0.65972222])                             
In [189]: thresh=0.5                                                                      

测试where

In [190]: np.where(ious>thresh)                                                           
Out[190]: (array([1, 2]),)
In [191]: np.where(ious>thresh)[0]                                                        
Out[191]: array([1, 2])
In [192]: np.where(ious>[thresh])[0]                                                      
Out[192]: array([1, 2])

现在是concatenate

In [193]: np.concatenate([ids[-1]], np.where(ious > thresh)[0])                           
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-193-c71c05bfaf15> in <module>
----> 1 np.concatenate([ids[-1]], np.where(ious > thresh)[0])

TypeError: only integer scalar arrays can be converted to a scalar index
In [194]: np.concatenate([ids[-1], np.where(ious > thresh)[0]])                           
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-194-91ed414d6d7c> in <module>
----> 1 np.concatenate([ids[-1], np.where(ious > thresh)[0]])

ValueError: zero-dimensional arrays cannot be concatenated
In [195]: np.concatenate([[ids[-1]], np.where(ious > thresh)[0]])                         
Out[195]: array([1, 1, 2])

现在是delete

In [196]: np.delete(ids,np.concatenate([[ids[-1]], np.where(ious > thresh)[0]]))          
Out[196]: array([3, 1])

【讨论】:

  • 这行得通,但你能解释一下为什么需要[[ids[-1]]。为什么这里需要嵌套数组?
  • ids[-1]ids 的一个元素,类型是 np.int64,形状是 (),错误称为“零维”。 concatenate(无轴)在它们的第一个轴上加入参数。 0d 数组没有第一个轴。 concatenate 的所有输入必须具有相同的维数。 np.array([ids[-1]]) 的形状为 (1,)。
  • 但是 [ids[-1]] 会导致一维数组对吗?我使用了一个解决方案,我做了一个ids = np.delete(ids, np.concatenate(([len(ids) - 1], np.where(ious &gt; thresh)[0])))
猜你喜欢
  • 1970-01-01
  • 2021-07-15
  • 2018-05-29
  • 2021-01-27
  • 2019-04-26
  • 2017-12-15
  • 2021-10-31
  • 1970-01-01
  • 2018-04-04
相关资源
最近更新 更多