gpt4 book ai didi

python - 检查两个 numpy 数组行是否同时满足一个命题

转载 作者:太空宇宙 更新时间:2023-11-04 04:48:09 25 4
gpt4 key购买 nike

这是我之前一个问题的后续帖子:

Check whether numpy array row is smaller than the next

假设我有以下 numpy 数组:

a=np.reshape(np.array([[79,np.nan,87,77,92,133,99,121,103,118,126, 
133,131,67]]),(7,2))

In [1]: a
Out[1]:
array([[ 79., nan],
[ 87., 77.],
[ 92., 133.],
[ 99., 121.],
[ 103., 118.],
[ 126., 133.],
[ 131., 67.]])

我想创建一个新的列或数组,这将是一个 True/False 指标,用于测试以下命题:

a[-1, 0] < a[1:, 0] and a[-1, 1] > a[1:, 1]

我期望的结果如下:

False (because the first value of column 1 is nan)
False
True
True
False
True
False

我已经尝试了上一篇文章中描述的解决方案的不同变体,但到目前为止我都没有成功。

编辑:

想法是测试是否 87<92 同时 77>133 是否为 False。然后 92<99 和 133>121 为真等等。

最佳答案

您将使用与 Tai's answer 完全相同的策略在您之前的帖子中:

b = np.diff(a, axis=0)

[[ 8. nan]
[ 5. 56.]
[ 7. -12.]
[ 4. -3.]
[ 23. 15.]
[ 5. -66.]]

现在您使用逻辑函数 np.logical_and在数组 b 上。因为你总是想要一个 trailing False,我们可以直接附加它:

result = np.logical_and(b[:,0] > 0, b[:,1] < 0)
result = np.append(result, np.array([False]))

[False False True True False True False]

(根据您的意见编辑了我的帖子)

注意:将值与nan 进行比较将始终返回False。因此,如果 nan 出现在中间的某行中,它总是会生成 行,这些行的计算结果为 False

关于python - 检查两个 numpy 数组行是否同时满足一个命题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49018980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com