过滤2D numpy数组 [英] Filter a 2D numpy array

查看:130
本文介绍了过滤2D numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有一个numpy二维ndarray的子​​数组(在最小和最大之间)

I want to have a sub array (between min and max) of a numpy 2D ndarray

    xy_dat = get_xydata()
    x_displayed = xy_dat[((xy_dat > min) & (xy_dat < max))]

min和max为浮点数,以便与数组xy_dat的第一个值进行比较

min and max are float in order to be compare with the first value of the array xy_dat

xy_dat是2D numpy数组:

xy_dat is a 2D numpy array :

[[ 735964.            1020.        ]
 [ 735964.04166667    1020.        ]
 [ 735964.08333333    1020.        ]
 ..., 
 [ 736613.39722222    1095.        ]
 [ 736613.40416667    1100.        ]
 [ 736613.41111111    1105.        ]]

x_displayed已正确过滤,但是我丢失了第二个值(现在是一维数组):

x_displayed is correctly filtered but I have lost the second value (it is now a 1D array) :

[ 735964.04166667  735964.08333333  735964.125      
 ...,  
736613.39027778  736613.39722222  736613.40416667]

如何在第一个值上设置过滤器,并保持其他值不变?

How make the filter on the first value and keep the other ?

推荐答案

您应仅在 first 列上执行条件:

You should perform the condition only over the first column:

x_displayed = xy_dat[((xy_dat[:,0] > min) & (xy_dat[:,0] < max))]

我们在这里构建的视图仅考虑使用xy_dat[:,0]的第一列.现在,检查此1d是否在边界之间,我们构造一个应保留的行的 1D 布尔数组,然后通过将其用作xy_dat[..]参数中的项来选择这些行

What we do here is constructing a view where we only take into account the first column with xy_dat[:,0]. By now checking if this 1d is between bounds, we construct a 1D boolean array of the rows we should retain, and now we make a selection of these rows by using it as item in the xy_dat[..] parameter.

这篇关于过滤2D numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆