如何提取与满足条件的原始数组相同维度的数组?

How to extract an array of same dimension as the original array meeting a condition?

这个问题听起来很基础。但是当我尝试在 numpy 数组上使用 whereboolean 条件时,它总是 returns 一个扁平数组。

我有 NumPy 数组

P = array([[ 0.49530662,  0.07901   , -0.19012371],
       [ 0.1421513 ,  0.48607405, -0.20315014],
       [ 0.76467375,  0.16479826, -0.56598029],
       [ 0.53530718, -0.21166188, -0.08773241]])

我想提取只有负值的数组,但是当我尝试

P[P<0]
array([-0.19012371, -0.41421612, -0.20315014, -0.56598029, -0.21166188,
       -0.08773241, -0.09241335])
P[np.where(P<0)]
array([-0.19012371, -0.41421612, -0.20315014, -0.56598029, -0.21166188,
       -0.08773241, -0.09241335])

我得到了一个展平的数组。如何提取

形式的数组
array([[ 0,  0,          -0.19012371],
       [ 0 , 0,          -0.20315014],
       [ 0,  0,          -0.56598029],
       [ 0, -0.21166188, -0.08773241]])

我不想创建临时数组然后使用 Temp[Temp>=0] = 0

这应该可行,基本上获取所有大于 0 的元素的索引,并将它们设置为 0,这将保留维度!我从这里得到了这个想法:Replace all elements of Python NumPy Array that are greater than some value

另请注意,我修改了原始数组,这里没有使用临时数组

import numpy as np

P = np.array([[ 0.49530662,  0.07901   , -0.19012371],
       [ 0.1421513 ,  0.48607405, -0.20315014],
       [ 0.76467375,  0.16479826, -0.56598029],
       [ 0.53530718, -0.21166188, -0.08773241]])

P[P >= 0] = 0
print(P)

输出将是

[[ 0.          0.         -0.19012371]
 [ 0.          0.         -0.20315014]
 [ 0.          0.         -0.56598029]
 [ 0.         -0.21166188 -0.08773241]]

如下所述,这会修改数组,所以我们应该使用np.where(P<0, P 0)来保留原始数组如下,感谢@kmario123如下

import numpy as np

P = np.array([[ 0.49530662,  0.07901   , -0.19012371],
       [ 0.1421513 ,  0.48607405, -0.20315014],
       [ 0.76467375,  0.16479826, -0.56598029],
       [ 0.53530718, -0.21166188, -0.08773241]])

print( np.where(P<0, P, 0))
print(P)

输出将是

[[ 0.          0.         -0.19012371]
 [ 0.          0.         -0.20315014]
 [ 0.          0.         -0.56598029]
 [ 0.         -0.21166188 -0.08773241]]
[[ 0.49530662  0.07901    -0.19012371]
 [ 0.1421513   0.48607405 -0.20315014]
 [ 0.76467375  0.16479826 -0.56598029]
 [ 0.53530718 -0.21166188 -0.08773241]]

由于您的需求是:

I want to "extract" the array of only negative values

您可以将 numpy.where() 与您的 条件一起使用 (检查负值),这可以保留数组的维度,如下例所示:

In [61]: np.where(P<0, P, 0)
Out[61]: 
array([[ 0.        ,  0.        , -0.19012371],
       [ 0.        ,  0.        , -0.20315014],
       [ 0.        ,  0.        , -0.56598029],
       [ 0.        , -0.21166188, -0.08773241]])

其中 P 是您的输入数组。


另一个想法是使用 numpy.zeros_like() for initializing a same shape array and numpy.where() 来收集满足条件的指标。

# initialize our result array with zeros
In [106]: non_positives = np.zeros_like(P)

# gather the indices where our condition is obeyed
In [107]: idxs = np.where(P < 0)

# copy the negative values to correct indices
In [108]: non_positives[idxs] = P[idxs]

In [109]: non_positives
Out[109]: 
array([[ 0.        ,  0.        , -0.19012371],
       [ 0.        ,  0.        , -0.20315014],
       [ 0.        ,  0.        , -0.56598029],
       [ 0.        , -0.21166188, -0.08773241]])

另一个想法是简单地使用准系统 numpy.clip() API,如果我们省略 out= kwarg,这将 return 一个新数组。

In [22]: np.clip(P, -np.inf, 0)    # P.clip(-np.inf, 0)
Out[22]: 
array([[ 0.        ,  0.        , -0.19012371],
       [ 0.        ,  0.        , -0.20315014],
       [ 0.        ,  0.        , -0.56598029],
       [ 0.        , -0.21166188, -0.08773241]])