Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

Let an array:

 a =np.array([[1,2],[3,-5],[6,-15],[10,7]])

to get lines with elements of the second column above -6 it' s possible to do

>>> a[a[:,1]>-6]
array([[ 1,  2],
   [ 3, -5],
   [10,  7]])

but how to get lines with the second element between -6;3? I tried:

>>> a[3>a[:,1]>-6]

and also (which raises an error):

>>> np.ma.masked_inside(a,-6,3)

which gives:

 masked_array(data =
 [[-- --]
  [-- --]
  [6 -15]
  [10 7]],
         mask =
  [[ True  True]
  [ True  True]
  [False False]
  [False False]],
   fill_value = 999999)

but the result is not too clear

Thanks jp

like image 549
Jean-Pat Avatar asked Oct 25 '12 10:10

Jean-Pat


People also ask

How we can filter values evaluate values in NumPy array?

In NumPy, you filter an array using a boolean index list. A boolean index list is a list of booleans corresponding to indexes in the array. If the value at an index is True that element is contained in the filtered array, if the value at that index is False that element is excluded from the filtered array.

What is NumPy masked array?

A masked array is the combination of a standard numpy. ndarray and a mask. A mask is either nomask , indicating that no value of the associated array is invalid, or an array of booleans that determines for each element of the associated array whether the value is valid or not.


2 Answers

>>> a[ (-6<a[:,1]) & (a[:,1]<3) ]

array([[ 1,  2],
      [ 3, -5]])
like image 186
Antony Hatchkins Avatar answered Sep 25 '22 23:09

Antony Hatchkins


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.

like image 34
Pierre GM Avatar answered Sep 23 '22 23:09

Pierre GM