NumPy array indexing is the same as accessing array elements. We can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
Program 1:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[0])
print(arr[1])
Output:
1
2
Also, we can access index elements and add them.
Program 2:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr[2] + arr[3])
Output:
7
Accessing 2-D Array Elements
To access elements from 2-D arrays we can use comma separated integers representing the dimension and the index of the element.
Program 3:
import numpy as np
arr = np.array([[1, 2, 3, 4]])
print(arr)
print(arr.ndim)
print(arr.shape)
print(arr[0,0])
print(arr[0,3])
Output:
[[1 2 3 4]]
2
(1, 4)
1
4
Note: In above program; arr is a 2-D array with dimension’s 0-axis and 1-axis. But, there are no elements present at 1-axis and it will result an error; index 1 is out of bounds for axis 0 with size 1 if we try to access arr[1,0].
Program 4:
import numpy as np
list1=[1,2,3,4,5]
list2=[6,7,8,9,10]
arr = np.array([list1, list2])
print(arr)
print(arr.ndim)
print(arr.shape)
print(arr[0, 0])
print(arr[0,1])
print(arr[1,0])
Output:
[[ 1 2 3 4 5]
[ 6 7 8 9 10]]
2
(2, 5)
1
2
6
Note: In above program; arr is a 2-D array with dimension’s 0-axis and 1-axis. Here list 2 elements are present at 1-axis. Therefore, its printing 6 when we are accessing arr[1,0].
Besides using an index to access elements in an array, you can also use a list as the index.
Program 5:
import numpy as np
list1 = [1,2,3,4,5]
arr = np.array(list1)
print(arr[[2,4]])
Output:
[3 5]
Note: In above program; arr[[2,4]] is accessing values present at index values 2 and 4 that is 3 and 5. It will print 1 and 4 if we use arr[[0,3]].
Accessing 3-D Array Elements
To access elements from 3-D arrays we can use comma separated integers representing the dimensions and the index of the element.
Program 6:
import numpy as np
arr = np.array([[[1, 2, 3]]])
print(arr[0, 0, 2])
Output:
3
Note: In above program; arr is a 3-D array with dimension’s 0-axis, 1-axis and 2-axis. But, there are no elements present at 1-axis and 2-axis. It will result an error; index 1 is out of bounds for axis 1 with size 1 if we try to access arr[0,1,0].
Program 7:
import numpy as np
arr = np.array([[[1, 2, 3],[4,5,6]]])
print(arr[0, 1, 0])
Output:
4
Note: In above program; arr is a 3-D array with dimension’s 0-axis, 1-axis and 2-axis. But, there are [4,5,6] elements present at 1-axis. Therefore, its printing 4 when we are accessing arr[0,1,0].
Program 8:
import numpy as np
arr = np.array([[[1, 2, 3],[4, 5, 6]],[[7, 8, 9], [10, 11, 12]]])
print(arr)
print(arr[1, 1, 2])
Output:
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
12
Note: In above program; arr is a 3-D array with dimension’s 0-axis, 1-axis and 2-axis. At 2-axis, we are having index 0 and index 1 with elements [[[ 1 2 3],[ 4 5 6]] and [[ 7 8 9],[10 11 12]]]. At 1-axis, we are having index 0 and index 1 with respect to index 0 of 2-axis with elements [[ 1 2 3],[ 4 5 6]] and index 0 and index 1 with respect to index 1 of 2-axis with elements [[ 7 8 9],[10 11 12]]. At 0-axis, we are having index 0, index 1 and index 2 with respect to index 0 and index 1 of 1-axis with elements [ 1 2 3],[ 4 5 6],[ 7 8 9],[10 11 12].
Negative Indexing
Negative indexing is used to access an array from the end.
Program 9:
import numpy as np
arr = np.array([1,2,3,4,5])
print(arr[-1])
Output:
5
Program 10:
import numpy as np
arr = np.array([[1,2,3,4,5]])
print(arr[0,-1])
Output:
5
Note: In above program; arr is a 2-D array with dimension’s 0-axis and 1-axis. All element’s are present at 0-axis and none at 1-axis. Therefore, as per negative indexing; we are getting result as 5 (last element) when we are accessing arr[0,-1].
Program 11:
import numpy as np
arr = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print(arr[1,-1])
Output:
10
Note: In above program; arr is a 2-D array with dimension’s 0-axis and 1-axis. Element’s [1,2,3,4,5] are present at 0-axis and elements [6,7,8,9,10] are present at 1-axis. Therefore, as per negative indexing; we are getting result as 10 (last element) when we are accessing arr[1,-1].
Boolean Indexing
In addition to using indexing to access elements in an array, there is another very cool way to access elements in a NumPy array.
Program 12:
import numpy as np
list = [6,7,8,9,10]
arr = np.array(list)
print(arr)
print(arr>7)
Output:
[[ 6 7 8 9 10]]
[[False False True True True]]
Note: What it actually does is to go through each element in arr and check if each element is more than two. The result is a Boolean value, and a list of Boolean values is created at the end of the process.
We can feed the list results back into the array as the index.
Program 13:
import numpy as np
list = [6,7,8,9,10]
arr = np.array(list)
print(arr)
print(arr>7)
print(arr[arr>7])
Output:
[[ 6 7 8 9 10]]
[[False False True True True]]
[ 8 9 10]
This method of accessing elements in an array is known as Boolean Indexing. This method is very useful. If we want to retrieve all of the odd numbers from the list, we could simply use Boolean Indexing.
Program: 14
import numpy as np
nums = np.arange(20)
print(nums)
odd_num = nums[nums % 2 == 1]
print(odd_num)
Output:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
[ 1 3 5 7 9 11 13 15 17 19]