Numpy Array Slicing
Summary: in this tutorial, you’ll learn about the numpy array slicing that extracts one or more elements from a numpy array.
Numpy array slicing on on-dimensional arrays
NumPy arrays use brackets [] and : notations for slicing like lists. By using slices, you can select a range of elements in an array with the following syntax:
[m:n]
Code language: Python (python)
This slice selects elements starting with m and ending with n-1 . Note that the nth element is not included. In fact, the slice m:n can be explicitly defined as:
[m:n:1]
Code language: Python (python)
The number 1 specifies that the slice selects every element between m and n .
To select every two elements, you can use the following slice:
[m:n:2]
Code language: Python (python)
In general, the following expression selects every k element between m and n :
[m:n:k]
Code language: Python (python)
If k is negative, the slice returns elements in reversed order starting from m to n+1 . The following table illustrates the slicing expressions:
Slicing Expression | Meaning |
---|---|
a[m:n] | Select elements with an index starting at m and ending at n-1. |
a[:] or a[0:-1] | Select all elements in a given axis |
a[:n] | Select elements starting with index 0 and up to element with index n-1 |
a[m:] | Select elements starting with index m and up to the last element |
a[m:-1] | Select elements starting with index m and up to the last element |
a[m:n:k] | Select elements with index m through n (exclusive), with an increment k |
a[::-1] | Select all elements in reverse order |
See the following example:
import numpy as np a = np.arange(0, 10) print('a=', a) print('a[2:5]=', a[2:5]) print('a[:]=', a[:]) print('a[0:-1]=', a[0:-1]) print('a[0:6]=', a[0:6]) print('a[7:]=', a[7:]) print('a[5:-1]=', a[5:-1]) print('a[0:5:2]=', a[0:5:2]) print('a[::-1]=', a[::-1])
Code language: Python (python)
a= [0 1 2 3 4 5 6 7 8 9] a[2:5]= [2 3 4] a[:]= [0 1 2 3 4 5 6 7 8 9] a[0:-1]= [0 1 2 3 4 5 6 7 8] a[0:6]= [0 1 2 3 4 5] a[7:]= [7 8 9] a[5:-1]= [5 6 7 8] a[0:5:2]= [0 2 4] a[::-1]= [9 8 7 6 5 4 3 2 1 0]
Code language: Python (python)
Numpy array slicing on multidimensional arrays
To slice a multidimensional array, you apply the square brackets [] and the : notation to each dimension (or axis). The slice returns a reduced array where each element matches the selection rules. For example:
import numpy as np a = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) print(a[0:2, :])
Code language: Python (python)
[[1 2 3] [4 5 6]]
Code language: Python (python)
In this example, array a is a 2-D array. In the expression a[0:2, :] :
First, the 0:2 selects the element at index 0 and 1, not 2 that returns:
[[1 2 3] [4 5 6]]
Code language: Python (python)
Then, the : select all elements. Therefore the whole expression returns:
[[1 2 3] [4 5 6]]
Code language: Python (python)
import numpy as np a = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]) print(a[1:, 1:])
Code language: Python (python)
[[5 6] [8 9]]
Code language: Python (python)
First, 1: selects the elements starting at index 1 to the last element of the first axis (or row), which returns:
[[4 5 6] [7 8 9]]
Code language: Python (python)
Second, 1: selects the elements starting at index 1 to the last elements of the second axis (or column), which returns:
[[5 6] [8 9]]
Code language: Python (python)
Summary
- Use slicing to extract elements from a numpy array
- Use a[m:n:p] to slice one-dimensional arrays.
- Use a[m:n:p, i:j:k, . ] to slice multidimensional arrays
NumPy Array Slicing
Slicing in python means taking elements from one given index to another given index.
We pass slice instead of index like this: [start:end] .
We can also define the step, like this: [start:end:step] .
If we don’t pass start its considered 0
If we don’t pass end its considered length of array in that dimension
If we don’t pass step its considered 1
Example
Slice elements from index 1 to index 5 from the following array:
arr = np.array([1, 2, 3, 4, 5, 6, 7])
Note: The result includes the start index, but excludes the end index.
Example
Slice elements from index 4 to the end of the array:
arr = np.array([1, 2, 3, 4, 5, 6, 7])
Example
Slice elements from the beginning to index 4 (not included):
arr = np.array([1, 2, 3, 4, 5, 6, 7])
Negative Slicing
Use the minus operator to refer to an index from the end:
Example
Slice from the index 3 from the end to index 1 from the end:
arr = np.array([1, 2, 3, 4, 5, 6, 7])
STEP
Use the step value to determine the step of the slicing:
Example
Return every other element from index 1 to index 5:
arr = np.array([1, 2, 3, 4, 5, 6, 7])
Example
Return every other element from the entire array:
arr = np.array([1, 2, 3, 4, 5, 6, 7])
Slicing 2-D Arrays
Example
From the second element, slice elements from index 1 to index 4 (not included):
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
Note: Remember that second element has index 1.
Example
From both elements, return index 2:
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
Example
From both elements, slice index 1 to index 4 (not included), this will return a 2-D array:
arr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])