Indexing in Numpy Arrays
What is Indexing in Numpy Array?
General syntax for accessing element in an Numpy array
array_name[index]
Create an Numpy Array for whole, natural and integer numbers
import numpy as np
natural_n = [1,2,3,4,5]
whole_n = [0,1,2,3,4]
integer_n = [-2,-1,0,1,2]
The above code creates a each separate list for natural numbers, whole numbers and integer numbers.
array_1 = np.array(natural_n)
array_2 = np.array([natural_n, whole_n])
array_3 = np.array([[natural_n, whole_n], [whole_n, integer_n]])
From the above code, we can create an
To check the dimensions of above created arrays are
print(array_1.ndim, array_2.ndim, array_3.ndim)
Output
1 2 3
To print the above created arrays on the screen are
print("1D array : ", array_1)
print("2D array : ", array_2)
print("3D array : ", array_3)
Output
1D array : [1 2 3 4 5]
2D array : [[1 2 3 4 5]
[0 1 2 3 4]]
3D array : [[[ 1 2 3 4 5]
[ 0 1 2 3 4]]
[[ 0 1 2 3 4]
[-2 -1 0 1 2]]]
Types of Indexing in Numpy array
Positive Indexing
In this indexing, the positive numbers from 0 to n are used to access the elements in an array. Positive indexing starts traverse from left to right of an array.
Example
print("Positive Indexing : ", array_1[0])
Output
Positive Indexing : 1
Negative Indexing
In this indexing, the negative numbers are used to access the elements in an array. -1 represents the last element in an array. Negative indexing starts traverse from right to left of an array.
Recommended by LinkedIn
Example
print("Negative Indexing : ", array_1[-1])
Output
Negative Indexing : 5
Accessing in 1D array
syntax : array_name[index_value]
print("Access the 1st element in 1D array : ", array_1[0])
print("Access the Nth element in 1D array : ", array_1[4])
Here, we can access the first and last elements in an 1D array.
Output
Access the 1st element in 1D array : 1
Access the Nth element in 1D array : 5
Accessing in 2D array
syntax : array_name[row_index_value, column_index_value]
print("Access the 1st row in 2D array : ", array_2[0])
print("Access the Nth row in 2D array : ", array_2[1])
print("Access the 1st row 1st element in 2D array : ", array_2[0, 0])
print("Access the 1st row Nth element in 2D array : ", array_2[0, 4])
print("Access the Nth row 1st element in 2D array : ", array_2[1, 0])
print("Access the Nth row Nth element in 2D array : ", array_2[1, 4])
Here, we can access the each row and column elements in an 2D array.
Output
Access the 1st row in 2D array : [1 2 3 4 5]
Access the Nth row in 2D array : [0 1 2 3 4]
Access the 1st row 1st element in 2D array : 1
Access the 1st row Nth element in 2D array : 5
Access the Nth row 1st element in 2D array : 0
Access the Nth row Nth element in 2D array : 4
Accessing in 3D array
syntax : array_name[row_index_value, column_index_value, depth_index_value]
print("Access the 1st row in 3D array : ", array_3[0])
print("Access the Nth row in 3D array : ", array_3[1])
print("Access the 1st row 1st column in 3D array : ", array_3[0, 0])
print("Access the 1st row Nth column in 3D array : ", array_3[0, 1])
print("Access the Nth row 1st column in 3D array : ", array_3[1, 0])
print("Access the Nth row Nth column in 3D array : ", array_3[1, 1])
print("Access the 1st row 1st column 1st element in 3D array : ", array_3[0, 0, 0])
print("Access the 1st row 1st column Nth element in 3D array : ", array_3[0, 0, 4])
print("Access the 1st row Nth column 1st element in 3D array : ", array_3[0, 1, 0])
print("Access the 1st row Nth column Nth element in 3D array : ", array_3[0, 1, 4])
print("Access the Nth row 1st column 1st element in 3D array : ", array_3[1, 0, 0])
print("Access the Nth row 1st column Nth element in 3D array : ", array_3[1, 0, 4])
print("Access the Nth row Nth column 1st element in 3D array : ", array_3[1, 1, 0])
print("Access the Nth row Nth column Nth element in 3D array : ", array_3[1, 1, 4])
Here, we can access the each row and column and depth elements in an 3D array.
Output
Access the 1st row in 3D array : [[1 2 3 4 5]
[0 1 2 3 4]]
Access the Nth row in 3D array : [[ 0 1 2 3 4]
[-2 -1 0 1 2]]
Access the 1st row 1st column in 3D array : [1 2 3 4 5]
Access the 1st row Nth column in 3D array : [0 1 2 3 4]
Access the Nth row 1st column in 3D array : [0 1 2 3 4]
Access the Nth row Nth column in 3D array : [-2 -1 0 1 2]
Access the 1st row 1st column 1st element in 3D array : 1
Access the 1st row 1st column Nth element in 3D array : 5
Access the 1st row Nth column 1st element in 3D array : 0
Access the 1st row Nth column Nth element in 3D array : 4
Access the Nth row 1st column 1st element in 3D array : 0
Access the Nth row 1st column Nth element in 3D array : 4
Access the Nth row Nth column 1st element in 3D array : -2
Access the Nth row Nth column Nth element in 3D array : 2
Few examples using the negative indexing
print("Access the 1st row Nth element in 2D array by Negative index : ", array_2[0, -1])
print("Access the Nth row Nth element in 2D array by Negative index : ", array_2[-1, -1])
Output
Access the 1st row Nth element in 2D array by Negative index : 5
Access the Nth row Nth element in 2D array by Negative index : 4
Challenge - Try to access the elements from 1D, 2D & 3D arrays using negative indexing.
Follow on LinkedIn - Trillionaire TK
Follow on Instagram - Trillionaire TK
Visit our website - Information Technology
Learn, Code, Practice!!!