python - 如何根据模糊条件从Numpy数组中选择值?

标签 python arrays numpy

假设我有以下数组,我只想保留第一列值为 39 (±1)、第二列值为 -1 (±1) 的行:

[[39.26783752, -0.84259111],
[24.44051933,  2.33262944],
[28.28138924,  6.79832602],
[37.54968262,  7.43038321]]

我知道如果我有更简单的条件,我可以这样做:

np.where((next_points_arr[:, 0] > 39) & (next_points_arr[:, 1] < 1))

我也知道我可以使用 math.isclose(39, 39.2, abs_tol = 1),但我不确定如何有效地将两者结合起来?

我的方法是再创建两列带有 bool 值的列,这将表示条件是否为真,然后使用 np.where() 查找 True 在每一列中,但这似乎并不明智。

最佳答案

您可以使用 np.isclose 检查这两个条件

a = np.array(
    [[39.26783752, -0.84259111],
     [24.44051933,  2.33262944],
     [28.28138924,  6.79832602],
     [37.54968262,  7.43038321]])

a[np.isclose(a, [39, -1], atol=[1.,1.]).all(1)]

输出:

array([[39.26783752, -0.84259111]])

关于python - 如何根据模糊条件从Numpy数组中选择值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64445525/

相关文章:

python - 当指定面色时,边缘线在 mplot3d 冲浪中消失

PHP 将每个元素追加到数组中

python - Numpy:从点列表中获取最大值的正确方法

python - 生成列表,允许重复并符合条件

python - 具有特殊字符的Powershell/c(PsExec/EncodedCommand)

将 8 字节 char 数组转换为 long

php - simpleXml 到数组的行为对我来说很奇怪,并且在 9941 项处中断

arrays - 在数组的不同部分执行不同的函数。 python 3

Python Pyinstaller 3.1 英特尔 MKL fatal error : Cannot load mkl_intel_thread. dll

python - 如何使用scrapy抓取各种标签之间包含的文本