Array indexing

We have already seen that one may index arrays by combinations of slices and integers, this is the basic slicing technique. There are, however, many more possibilities, which allow for a variety of ways to access and modify array elements.

Indexing with Boolean arrays

It is often useful to access and modify only parts of an array, depending on its value. For instance, one might want to access all the positive elements of an array. This turns out to be possible using Boolean arrays, which act like masks to select only some elements of an array. The result of such an indexing is always a vector. For instance, consider the following example:

B = array([[True, False],
           [False, True]])
M = array([[2, 3],
           [1, 4]])
M[B] # array([2,4]), a vector

In fact, the M[B] call is equivalent to M.flatten()[B]. One may then replace the resulting vector by another vector. For instance, one may replace all the elements by zero (refer to section Broadcasting for more information):

M[B] = 0
M # [[0, 3], [1, 0]]

Or one may replace all the selected values by others:

M[B] = 10, 20
M # [[10, 3], [1, 20]]

By combining the creation of Boolean arrays (M > 2), smart indexing (indexing with Boolean array), and broadcasting, one may use the following elegant syntax:

M[M>2] = 0    # all the elements > 2 are replaced by 0

The expression broadcasting here refers to the tacit conversion of the scalar 0 to a vector of an appropriate shape.

Using where

The command where gives a useful construct that can take a Boolean array as a condition and either return the indexes of the array elements satisfying the condition or return different values depending on the values in the Boolean array.

The basic structure is:

where(condition, a, b)

This will return values from a when the condition is True and values from b when it is False.

For instances consider, a Heaviside function:

Using where

The following code implements a Heaviside function:

def H(x):
    return where(x < 0, 0, 1)
x = linspace(-1,1,11)  # [-1. -0.8 -0.6 -0.4 -0.2 0. 0.2 0.4 0.6 0.8 1. ]
print(H(x))            # [0 0 0 0 0 1 1 1 1 1 1]

The second and third arguments can be either arrays of the same size as the condition (the Boolean array) or scalars. We give two more example to demonstrated how to manipulate elements from an array or a scalar depending on a condition:

x = linspace(-4,4,5)
# [-4. -2.  0.  2.  4.]

print(where(x > 0, sqrt(x), 0))
# [ 0.+0.j 0.+0.j 0.+0.j 1.41421356+0.j  2.+0.j ]
print(where(x > 0, 1, -1)) # [-1 -1 -1  1  1]

If the second and third arguments are omitted, then a tuple containing the indexes of the elements satisfying the condition is returned.

For example consider the use of where with only one argument in the following code:

a = arange(9)
b = a.reshape((3,3))

print(where(a > 5))   # (array([6, 7, 8]),)

print(where(b > 5))   # (array([2, 2, 2]), array([0, 1, 2]))
..................Content has been hidden....................

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