Creating a masked array from a condition

The numpy.ma module has a collection of functions geared towards common ways of constructing masked arrays. The following code illustrates how to create an array where all entries between 4 and 6 are masked:

x = np.array([
[i+j for j in range(i, i+5)]
for i in range(5)])
xm = ma.masked_inside(x, 4, 6)

In this code, we first generate the following array:

[[ 0  1  2  3  4]
[ 2 3 4 5 6]
[ 4 5 6 7 8]
[ 6 7 8 9 10]
[ 8 9 10 11 12]]

We then use the ma.masked_inside(x, 4, 6) function call to mask all elements of the array that are in the interval [4,6], resulting in the following masked array:

[[0 1 2 3 --]
[2 3 -- -- --]
[-- -- -- 7 8]
[-- 7 8 9 10]
[8 9 10 11 12]]
Notice that the endpoints are included in the interval specified by masked_inside.

The following table lists all functions available for the construction of specific masks and what they are used for:

Function Operation
masked_equal(x, v) Mask items in x equal to v
masked_greater(x, v) Mask items in x greater than v
masked_greater_equal(x, v) Mask items in x greater than or equal to v
masked_inside(x, v1, v2) Mask items in x inside the interval [v1, v2]
masked_invalid(x) Mask items in x that are NaN or infinite.
masked_less(x, v) Mask items in x less than v
masked_less_equal(x, v) Mask items in x less than or equal to v
masked_not_equal(x, v) Mask items in x not equal to v
masked_outside(x, v1, v2) Mask items in x outside interval [v1,v2]
masked_where(condition, v) Mask items in x at positions corresponding to entries where the array condition is true

 

The masked_where function is the most flexible way to generate a masked array. For example, the following code shows how to mask an array of two-digit integers at the positions where the sum of the digits is strictly between 5 and 9:

def digit_sum(n):
return n // 10 + n % 10
x = np.array(
[[10*i+j for j in range(1,7)]
for i in range(1,7)])
condition = (digit_sum(x) > 5) & (digit_sum(x) < 9)
xm = ma.masked_where(condition , x)

In this code block, we first define a digit_sum() function, which returns the sum of the digits of a two-digit integer. We then define the array x, which contains the following data:

[[11 12 13 14 15 16]
[21 22 23 24 25 26]
[31 32 33 34 35 36]
[41 42 43 44 45 46]
[51 52 53 54 55 56]
[61 62 63 64 65 66]]

We then define the condition array, as follows:

condition = (digit_sum(x) > 5) & (digit_sum(x) < 9)

This array will have a True entry only at positions where the digit sum of elements of x are greater than 5 but less than 9. Then, the masked array is created with the following call:

xm = ma.masked_where(condition , x)

The resulting array xm will contain the data indicated, as follows:

[[11 12 13 14 -- --]
[21 22 23 -- -- --]
[31 32 -- -- -- 36]
[41 -- -- -- 45 46]
[-- -- -- 54 55 56]
[-- -- 63 64 65 66]]
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset