NumPy arrays support a feature called slicing. Slicing retrieves zero or more items from an array, and the items also don't need to be sequential, whereas the normal array element operator []
can only retrieve one value. This is very convenient as it provides an ability to efficiently select multiple items from an array without the need to implement Python loops.
Slicing overloads the normal array []
operator to accept what is referred to as a slice object. A slice object is created using a syntax of start:end:step
. Each component of the slice is optional and, as we will see, this provides convenient means to select entire rows or columns by omitting the component of the slice.
To begin with the demonstrations, the following code creates a ten-element array and selects items in zero-based positions from 3
up to, but not including, position 8
:
In [36]: # get all items in the array from position 3 # up to position 8 (but not inclusive) a1 = np.arange(1, 10) a1[3:8] Out[36]: array([4, 5, 6, 7, 8])
This example has omitted specifying the step
value, which uses the default value of 1
. To demonstrate using other values for step
, the following code selects every other element in the array:
In [37]: # every other item a1[::2] Out[37]: array([1, 3, 5, 7, 9])
By omitting the start
and end
, NumPy chooses 0
through the length of the array as those values and then retrieves every other item. Changing this slightly, a negative step
value of -1
will conveniently reverse the array:
In [38]: # in reverse order a1[::-1] Out[38]: array([9, 8, 7, 6, 5, 4, 3, 2, 1])
When using a negative step
value, it is important that the start
value is greater than the end
value. Also, note that the following example is not equivalent to the preceding example:
In [39]: # note that when in reverse, this does not include # the element specified in the second component of the slice # that is, there is no 1 printed in this a1[9:0:-1] Out[39]: array([9, 8, 7, 6, 5, 4, 3, 2])
In this scenario, the 0
value in the array was not retrieved. This is because the end
value is not inclusive, so when iterating by -1
from 9
, NumPy stops at 0
before returning the value at that position in the array.
To select all the items starting at a position until the end of the array, simply specify the start
position and leave end
unspecified. The following code selects items from position 5
through the end of the array:
In [40]: # all items from position 5 onwards a1[5:] Out[40]: array([6, 7, 8, 9])
To select the first n
element in an array, simply leave the start
position unspecified and set end
to be the value of n
. The following selects the first five items in the array:
In [41]: # the items in the first 5 positions a1[:5] Out[41]: array([1, 2, 3, 4, 5])
Two-dimensional arrays can also be sliced. We have already seen how to select a specific element from a two-dimensional array, and how to select a specific row. The column selection example that we saw actually used the slice notation. To revisit that, the following code selects items from the second column of a matrix:
In [42]: # we saw this earlier # : in rows specifier means all rows # so this gets items in column position 1, all rows m[:,1] Out[42]: array([ 1, 5, 9, 13, 17])
To the left of the comma is a slice object for the rows, and to the right is one for the columns. The following code selects columns in position 2
through 3
of the matrix:
In [43]: # in all rows, but for all columns in positions # 1 up to but not including 3 m[:,1:3] Out[43]: array([[ 1, 2], [ 5, 6], [ 9, 10], [13, 14], [17, 18]])
Rows can also be sliced, and the step
value is valid for both rows and columns. The following code returns rows in position 3
and 4
:
In [44]: # in row positions 3 up to but not including 5, all columns m[3:5,:] Out[44]: array([[12, 13, 14, 15], [16, 17, 18, 19]])
Both columns and rows can be sliced at the same time:
In [45]: # combined to pull out a sub matrix of the matrix m[3:5,1:3] Out[45]: array([[13, 14], [17, 18]])
It is also possible to select specific rows or columns by passing a Python list as an element of the slice. The following code explicitly selects by position the first, third, and fourth rows:
In [46]: # using a python array, we can select # non-contiguous rows or columns m[[1,3,4],:] Out[46]: array([[ 4, 5, 6, 7], [12, 13, 14, 15], [16, 17, 18, 19]])
18.118.24.30