How To Do Array Indexing By Numpy

How To Access Array Elements With Array Indexing In Python

The Concept of Array Indexing is same as accessing the elements of an Array

You can access the elementof the array 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.

The indexes of the Numpy arrays start with 0,that means in the Numpy Array the first element has the index as 0 and the second index starts with 1 and it goes in this same increamental fashion.

Example

How to get the first element of the Array:

import numpy as np

arr = np.array([1, 2, 3, 4])

####### Here if you want to access the first element of the array which 1 then you have to access it's first index that is 0

print(arr[0])

Output:

1


How To Access Different Elements Of The Array


Now you can see how you can access different elements of the Array

If you want  to access the 3rd element of the array then you have access the 2nd index of the element

import numpy as np

arr = np.array([1, 2, 3, 4])

print(arr[2])

Output:

3

So always decrease the index value by 1 from the postion number which you want to access in the array.

How To Access 2-D Array

To access elements from 2-D arrays we can use comma separated integers representing the dimension and the index of the element.

To access 2-D array you have to work with two seperate integer values.1st Integer Value will represent the dimension and the 2nd Integer value will represent the index.

Example:


Let's have a look at the below example now which have a 2-D array which contains two arrays one contains 1,2,3,4,5 and other contains 6,7,8,9,10

import numpy as np

arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

##########Now if we want to print the 2nd element of 1st row then we will mention the dimension which is 0 for the 1st array and with in the 1st array the index of the 2nd number is 1.Hence the two integer value will be 0,1.

print('2nd element on 1st row: ', arr[0, 1]).

Output:

2nd element on 1st row:  2

How To access the

Example:

How to access the element on the 2nd row, and in the 5th column which is 10

import numpy as np

arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print('5th element on 2nd row: ', arr[1, 4])

Output:

5th element on 2nd row:  10


How To Access 3-D Arrays In Numpy Python:


To access elements from 3-D arrays we can use comma separated integers representing the dimensions and the index of the element.

Example

Access the third element of the second array of the first array:

############### If you will see here this 3-D Array called arr is formed with two 2-D arrays and every 2-D array contains two 1-D array in it.

import numpy as np

arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

print(arr[0, 1, 2])

Output:


6

Explanation:

With the first integer 0 it is showing the dimension of the 1st array

With the integer 1 it is showing index of the 2 nd 1-D array into the 1st 2-D array

With the integer 2 it is showing the index of the third element in the 2nd 1-D array into the 1st 2-D array into the whole 3-D array called arr

Seems complicated!!!!!

Just imagine all the arrays as block and subblock.Here 3-D array is the main block A,then 2-D array B is the subblock under mainblock A and 1-D array C is the subblock under the subblock of 2-D array called B.

What Is Negative Indexing In Numpy Array


If we want to access the array from the end of the array then we can ue the negetive indexing of the array

Example:


In this example we want to access the last element of this below 2D array.The last element of this 2-D array is 10

import numpy as np

arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])

print('Last element from 2nd dim: ', arr[1, -1])


Output:

10


Explanation:

To print the last element 10 of the 2-D array we are using two integer values.One ineteger value is representing the dimension of the 2-D array and the other integer value is represnting the index of the element.With the first integerr value 1 we are showing the dimension of the 1-D array under the 2-D array.With the integer value -1 we are fetching index number 0f 10.As 10 is the last element of the array so the index of the last element always starts with -1 and it will go on like this fashion -2,-3,-4 etc

Comments