Accessing sub arrays using slices

A slice is a reference to a subset of the elements in an array. Slices allow access to subranges of equally spaced items across each axis of the array. For example, the following code accesses a subset of elements on contiguous rows and columns of the array x:

y = x[2:4, 3:6]
dumpArray(y)

This will produce the following output:

23 24 25
33 34 35

Notice that, as is the case with ranges, the slice does not include its rightmost index. Thus, for instance, the slice 3:6 refers items at positions 3, 4, and 5

NumPy follows the standard interface for Python slices, where i:j:k denotes a slice from i to j with step k. See the following link for the full documentation:
https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range.

A slice can optionally include a step size, as exemplified by the following code:

y = x[1:5:2, 2:10:3]
dumpArray(y)

This code produces the output, shown as follows:

12 15 18
32 35 38

Using a negative increment, we can revert the order of the elements, as shown in the following example:

y = x[4:1:-1, 2]

This code will produce the following one-dimensional array:

array([42, 32, 22])

It is also valid to assign an array to a slice. For example, let's look at the following code, which demonstrates how to set a region of an array to zero:

xx = x.copy()
xx[2:-2, 2:-2] = np.zeros((6, 6), dtype=np.int64)
dumpArray(xx)

In this code, we first make a copy of the array x into xx using the copy() method. Then, we assign an array of zeros to the slice xx[2:-2, 2:-2], which has the effect setting a central rectangle of the array xx to zero, as follows:

00 01 02 03 04 05 06 07 08 09
10 11 12 13 14 15 16 17 18 19
20 21 00 00 00 00 00 00 28 29
30 31 00 00 00 00 00 00 38 39
40 41 00 00 00 00 00 00 48 49
50 51 00 00 00 00 00 00 58 59
60 61 00 00 00 00 00 00 68 69
70 71 00 00 00 00 00 00 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99
The array being assigned to a slice must exactly match the dimensions of the slice.

In a slice i:j:k, it is possible to omit one or more of the indices i, j, and k. The slice is then interpreted as follows:

  • If i is omitted, the start of the slice is assumed to be the beginning of the range
  • If j is omitted, the end of the slice is assumed to be the end of the range
  • If k is omitted, the increment in the slice is assumed to be one

It is actually possible to omit all indices in a slice expression, in which case the slice represents all items along the corresponding axis of the array. For example, the following code illustrates a typical idiom for selecting a row or column of an array:

y = x[:, 5]
y

This produces the following array:

array([ 5, 15, 25, 35, 45, 55, 65, 75, 85, 95])
Notice that, apparently, NumPy returned a row vector, even though we requested a column from the original array. In fact, NumPy does not have a notion of row or column vectors. The output of this example is a vector with shape (10,), that is, a tuple with the single element 10. A one-dimensional NumPy array can be interpreted as either a row vector or a column vector, depending on the context. If we need to convert y to a two-dimensional array with shape (1,10), we can use the code y.shape=(1,10).

Finally, it is possible to refer to the whole array x using the x[:] slice expression. A word of caution is necessary, however. In pure Python, if lst is a list, the lst[:] expression causes the whole list to be copied. The same is not true with NumPy arrays. The following code illustrates the point:

xx = x.copy()
y = xx[:]
y[1,1] = 0
dumpArray(xx)

This will produce the following output:

00 01 02 03 04 05 06 07 08 09
10 00 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
...

Notice that entry (1,1) of the array xx was modified. This happens because the y=xx[:] statement creates a reference to the array xx, and no copying is effected. Thus, changes to the array y will actually change the array xx. NumPy adopts the policy of avoiding copying arrays unless a copy is absolutely necessary, or the user explicitly requests a copy. This is a wise decision, since arrays can be quite large, in which case copying becomes an expensive operation.

Having said that, let's compare the previous example with the following code:

xx = x.copy()
y[:] = xx
y[1,1] = 0
dumpArray(xx)

Running this code, the reader can verify that the array xx is not modified. This means that the y[:] = xx statement does indeed copy the array xx into y.

Whenever an assignment to a slice is made, NumPy assumes that a copy is requested.
..................Content has been hidden....................

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