Array masking

NumPy arrays can be used as filters on the larger original array. This process of using arrays as filters is called array masking. For example, see the following snippet:

    In [146]: np.random.seed(10)
              ar=np.random.random_integers(0,25,10); ar
    Out[146]: array([ 9,  4, 15,  0, 17, 25, 16, 17,  8,  9])
    In [147]: evenMask=(ar % 2==0); evenMask
    Out[147]: array([False,  True, False,  True, False, False,  True, False,  True, False], dtype=bool)
    In [148]: evenNums=ar[evenMask]; evenNums
    Out[148]: array([ 4,  0, 16,  8])

In the following example, we randomly generate an array of 10 integers between 0 and 25. Then, we create a boolean mask array that is used to filter out only the even numbers. This masking feature can be very useful, say, for example, if we wished to eliminate missing values by replacing them with a default value. Here, the missing value '' is replaced by 'USA' as the default country. Note that '' is also an empty string:

    In [149]: ar=np.array(['Hungary','Nigeria', 
                           'Guatemala','','Poland',
                           '','Japan']); ar
    Out[149]: array(['Hungary', 'Nigeria', 'Guatemala', 
                     '', 'Poland', '', 'Japan'], 
                     dtype='|S9')
    In [150]: ar[ar=='']='USA'; ar
    Out[150]: array(['Hungary', 'Nigeria', 'Guatemala', 
      'USA', 'Poland', 'USA', 'Japan'], dtype='|S9')

Arrays of integers can also be used to index an array to produce another array. Note that this produces multiple values; hence, the output must be an array of type ndarray. This is illustrated in the following snippet:

    In [173]: ar=11*np.arange(0,10); ar
    Out[173]: array([ 0, 11, 22, 33, 44, 55, 66, 77, 88, 99])
    In [174]: ar[[1,3,4,2,7]]
    Out[174]: array([11, 33, 44, 22, 77])

In the preceding code, the selection object is a list, and elements at indices 1, 3, 4, 2, and 7 are selected. Now, assume that we change it to the following:

    In [175]: ar[1,3,4,2,7]

We get an IndexError error since the array is 1D and we're specifying too many indices to access it:

    IndexError          Traceback (most recent call last)
    <ipython-input-175-adbcbe3b3cdc> in <module>()
    ----> 1 ar[1,3,4,2,7]
    
    IndexError: too many indices

This assignment is also possible with array indexing, as follows:

    In [176]: ar[[1,3]]=50; ar
    Out[176]: array([ 0, 50, 22, 50, 44, 55, 66, 77, 88, 99]) 

When a new array is created from another array by using a list of array indices, the new array has the same shape.

..................Content has been hidden....................

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