NumPy Array Indexing (Python Tutorial)

NumPy Array Indexing (Python Tutorial)

This article explains the NumPy array Indexing. Learn to fetch the values of different array items or matrices values using the indexing with examples.

NumPy Arrays Indexing

In NumPy arrays can be indexed using standard python X[obj] syntax, where x is an array and obj is the selection. Array indexing is almost similar to accessing the elements of the array with the help of index number. The index numbers always start from 0 and go on to the number of elements present in the array. So the first element in the array will have its index number as 0 and the second element will have its index number as 1, and it will go on this for every element of the array.

Let us see how we can access elements of the array with the help of their index number:

import numpy as np
array1=np.array([1,2,3,4,5,6,7,7])
print(array1[3])

Output:

4

Here we are accessing the third element of the array with the help of their index number. Now let us try another example where we will try multiplying two elements of the array:

import numpy as np 
array1=np.array([1,2,3,4,5,6,7,7]) 
print(array1[3]*array1[2])

Output:

12

Here we are accessing the helmet with the help of their index number and also multiplying them as ar result it is taking the element at those indexes and multiplying it with each other.

Accessing 2-D and 3-D arrays

Now let us look through how we can access the elements within  a 2-D array:

import numpy as np 
array1=np.array([[1,2,3,4],[5,6,7,7]]) 
print(array1[0,2])

Output:

3

Here we are accessing are trying to access the third element from the first array. So we first give the array we want to go to and then the element we want to access, and this can be written with the help of commas and brackets. The same goes for a 3-D array where we need to specify first the array want to access and the other array inside and also the element we want to access. It will be more clear if you look at this example:

import numpy as np 
array1=np.array([[[1,2,3,4],[5,6,7,7]],[[1,2,3,4],[5,6,7,7]]]) 
print(array1[1,0,3])

Output:

4

Here we are trying to access the element which is present in the second array and then inside this array; we are accessing the first array’s element with index number three which gives us output as 4.

Negative Indexing

Negative indexing is used to access the last elements of the array. Whenever we directly need to use the last element of the array, we use negative indexing. So another so of this indexing is to check the previous memory block of the array in order to determine if these two arrays can be merged together. And this feature can also be used to develop a non- volatile memory manager.

Let us take an example and see how it is useful for us:

import numpy as np 
array1=np.array([1,2,3,4,5,6,7,7]) 
print(array1[-2])

Output:

7

Here the output is 7 because we have given index number as -2 which accessing the second last number of the array.

For 2-D Arrays

import numpy as np 
array1=np.array([[1,2,3,4],[5,6,7,7]]) 
print(array1[1,-2])

Output:

7

Here we are trying to access the second last element from the array with index number as 1. So, as a result, it is first choosing the array to be accessed and then displaying the element.

For 3-d Arrays

import numpy as np 
array1=np.array([[[1,2,3,4],[5,6,7,7]],[[1,2,3,4],[5,6,7,7]]]) 
print(array1[1,0,-1])

Output:

4

Now here we first gave an index of the array we want to access between the two arrays and form that array we choose the array again which we want to go to. The third one is the index of the element we want to access. We can go through another example in order to get clear with indexing in 3-d Arrays. let us take another example:

#here we are impoting the NumPy by makin an Alias as np
import numpy as np 
#now we create an 3-D array 
array1=np.array([[[1,2,3],[6,7,7]],[[3,4,8],[6,7,7]]]) 
# we try to access the elements here
print(array1[0,1,-2])

Output:

7

Now let us go step by step in this example:

  1. we know this is a 3-d array and has two elements [[1,2,3],[6,7,7]] and [[3,4,8],[6,7,7]].
  2. Now if we further divide these arrays.
  3. We get we find that every array has two more elements [1,2,3]and [6,7,7] and also [3,4,8] and [6,7,7].
  4. While accessing the element, we gave three value first one being 0.
  5. which means we will select array with index [0] which is [[1,2,3],[6,7,7]].
  6. The second value given by us is 1, so now we know that out of the array selected in the previous point.
  7. We have to select the array with index[1] which is [6,7,7].
  8. Now if we see the third value, which is -2.
  9. So we select elements with index[-2] from [6,7,7].
  10. With this, we get our output as 7.

So to conclude the indexing of arrays helps a lot in the manipulation of the data in these arrays, which quite useful in the fields of data manipulation.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *