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

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

本文介绍了如何提取与满足条件的原始数组相同维度的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The question sounds very basic. But when I try to use where or boolean conditions on numpy arrays, it always returns a flattened array.

我有 NumPy 数组

I have the NumPy array

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]])

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

I want to extract the array of only negative values, but when I try

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])

我得到一个扁平的数组.如何提取表单的数组

I get a flattened array. How can I extract the array of the form

array([[ 0,  0,          -0.19012371],
       [ 0 , 0,          -0.20315014],
       [ 0,  0,          -0.56598029],
       [ 0, -0.21166188, -0.08773241]])

我不想创建一个临时数组,然后使用类似 Temp[Temp>=0] = 0

I do not wish to create a temp array and then use something like Temp[Temp>=0] = 0

推荐答案

既然您的需求是:

我想提取"只有负值的数组

您可以使用 numpy.where() 使用您的 condition(检查负值),它可以保留数组的维度,如下例所示:

You can use numpy.where() with your condition (checking for negative values), which can preserve the dimension of the array, as in the below example:

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 是您的输入数组.

where P is your input array.

另一个想法可能是使用 numpy.zeros_like() 用于初始化相同的形状数组和 numpy.where() 收集满足我们条件的索引.

Another idea could be to use numpy.zeros_like() for initializing a same shape array and numpy.where() to gather the indices at which our condition satisfies.

# 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,它将返回一个新数组.


Yet another idea would be to simply use the barebones numpy.clip() API, which would return a new array, if we omit the out= kwarg.

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]])

这篇关于如何提取与满足条件的原始数组相同维度的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 11:53