Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在numpy数组中逐行查找大于阈值的值的索引_Python_Numpy_Threshold_Numpy Ndarray - Fatal编程技术网

Python 在numpy数组中逐行查找大于阈值的值的索引

Python 在numpy数组中逐行查找大于阈值的值的索引,python,numpy,threshold,numpy-ndarray,Python,Numpy,Threshold,Numpy Ndarray,我有一个数组,如下所示。我想通过数组中的每一行查找高于阈值(例如,0.7)的值的索引 items= np.array([[1. , 0.40824829, 0.03210806, 0.29488391, 0. , 0.5 , 0.32444284, 0.57735027, 0. , 0.5 ], [0.40824829, 1. , 0.57675476, 0.48154341, 0.

我有一个数组,如下所示。我想通过数组中的每一行查找高于阈值(例如,0.7)的值的索引

items= np.array([[1.        , 0.40824829, 0.03210806, 0.29488391, 0.        ,
        0.5       , 0.32444284, 0.57735027, 0.        , 0.5       ],
       [0.40824829, 1.        , 0.57675476, 0.48154341, 0.        ,
        0.81649658, 0.79471941, 0.70710678, 0.57735027, 0.40824829],
       [0.03210806, 0.57675476, 1.        , 0.42606683, 0.        ,
        0.        , 0.92713363, 0.834192  , 0.        , 0.73848549],
       [0.29488391, 0.48154341, 0.42606683, 1.        , 0.        ,
        0.29488391, 0.52620136, 0.51075392, 0.20851441, 0.44232587],
       [0.        , 0.        , 0.        , 0.        , 0.        ,
        0.        , 0.        , 0.        , 0.        , 0.        ],
       [0.5       , 0.81649658, 0.        , 0.29488391, 0.        ,
        1.        , 0.32444284, 0.28867513, 0.70710678, 0.        ],
       [0.32444284, 0.79471941, 0.92713363, 0.52620136, 0.        ,
        0.32444284, 1.        , 0.93658581, 0.22941573, 0.81110711],
       [0.57735027, 0.70710678, 0.834192  , 0.51075392, 0.        ,
        0.28867513, 0.93658581, 1.        , 0.        , 0.8660254 ],
       [0.        , 0.57735027, 0.        , 0.20851441, 0.        ,
        0.70710678, 0.22941573, 0.        , 1.        , 0.        ],
       [0.5       , 0.40824829, 0.73848549, 0.44232587, 0.        ,
        0.        , 0.81110711, 0.8660254 , 0.        , 1.        ]])

indices_items = np.argwhere(items>= 0.7)
此(索引\u项)返回

如何按以下行获取索引? 行0->[0]行1->[0,1,5,6,7]行2->[2,6,7,9]行3->[3]行4->[]
#这应该是空列表,因为没有任何值高于阈值…

获取行,使用
np列。其中
,然后使用
np.searchsorted
获取行数组上的区间索引,并使用这些索引拆分列数组-

In [38]: r,c = np.where(items>= 0.7)

In [39]: np.split(c,np.searchsorted(r,range(1,items.shape[0])))
Out[39]: 
[array([0], dtype=int64),
 array([1, 5, 6, 7], dtype=int64),
 array([2, 6, 7, 9], dtype=int64),
 array([3], dtype=int64),
 array([], dtype=int64),
 array([1, 5, 8], dtype=int64),
 array([1, 2, 6, 7, 9], dtype=int64),
 array([1, 2, 6, 7, 9], dtype=int64),
 array([5, 8], dtype=int64),
 array([2, 6, 7, 9], dtype=int64)]

就性能而言,这可能不是最优的,但如果您不真正关心它,则应该可以

indices_items = []
for l in items:
    indices_items.append(np.argwhere(l >= 0.7).flatten().tolist())

indices_items
Out[5]: 
[[0],
[1, 5, 6, 7],
[2, 6, 7, 9],
[3],
[],
[1, 5, 8],
[1, 2, 6, 7, 9],
[1, 2, 6, 7, 9],
[5, 8],
[2, 6, 7, 9]]

您将使用该输出做什么?您将无法将其保留为
ndarray
,因为它将不再是矩形。所以它实际上不是行-列形式,而是一个链表。
indices_items = []
for l in items:
    indices_items.append(np.argwhere(l >= 0.7).flatten().tolist())

indices_items
Out[5]: 
[[0],
[1, 5, 6, 7],
[2, 6, 7, 9],
[3],
[],
[1, 5, 8],
[1, 2, 6, 7, 9],
[1, 2, 6, 7, 9],
[5, 8],
[2, 6, 7, 9]]