只能将整数标量数组转换为标量索引

时间:2019-06-19 02:58:55

标签: python-3.x numpy

我正在查看的代码是:

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数组。那么到底出了什么问题。

1 个答案:

答案 0 :(得分: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])