Indexing in Numpy Arrays
Indexing in Numpy Arrays

Indexing in Numpy Arrays

What is Indexing in Numpy Array?

  • Access the elements in a Numpy array based on index values.
  • Accessing the elements in a Numpy array is as same as accessing in a list.
  • Numpy array index starts from 0 and so on.
  • This means index value of 1st element is 0 and index value of last element is N-1.


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

  • 1D array that consists of natural numbers.
  • 2D array that consists of natural and whole numbers.
  • 3D array that consists of natural numbers, whole numbers and integer numbers.


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

  1. Positive Indexing
  2. Negative Indexing


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.

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!!!

To view or add a comment, sign in

More articles by Tharunkumar S

  • Comparison Operations in Numpy Arrays

    Comparison Operations in Numpy As we already know the comparison or relational operations like less than, less than and…

  • Arithmetic Operations in Numpy Arrays

    Arithmetic Operations in Numpy As we already know the arithmetic operations like addition, subtraction, division…

  • Shape of an Array and Reshape() function

    Shape of an Numpy array In Numpy array, shape is an attribute that is used to check the dimension of an array. shape…

  • Slicing in Numpy Arrays

    What is Slicing in Numpy Array? Slice the array in a particular range of elements. In other words, Make an sub array…

  • Explore array(), type() and astype() functions

    What is ndarray in Numpy? "ndarray" stands for n-dimensional array. Numpy creates an object for an array in this type.

  • Introduction to Numpy Library in Python

    Introduction NUMPY stands for Numerical Python. Numpy is one of the libraries for the python programming language.

  • Journey of Machine Learning Begins

    Machine Learning Definition : The term Machine Learning was coined by Arthur Samuel in 1959, an American pioneer in the…

Others also viewed

Explore content categories