filtering lines in a numpy array according to values in a range

不羁的心 提交于 2019-12-02 18:22:32
>>> a[ (3>a[:,1]) & (a[:,1]>-6) ]

array([[ 1,  2],
      [ 3, -5]])

The np.ma.masked_inside(a, -6, 3) will create a MaskedArray object, where the values between -6 and 3 are masked (that is, flagged as invalid). In other terms, you're filtering out the values between -6 and 3. Instead, you should use np.ma.masked_outside(a, -6, 3):

>>> a = np.array([[1,2],[3,-5],[6,-15],[10,2]])
>>> np.ma.masked_outside(a,-6,3)
>>> masked_array(data =
 [[1 2]
 [3 -5]
 [-- --]
 [-- 2]],
             mask =
 [[False False]
 [False False]
 [ True  True]
 [ True False]],
       fill_value = 999999)

Note that with this function, you are filtering out the whole array, element by element, which is not what you want.

The indexing approach given in another solution is by far the most straightforward and understandable.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!