NumPy Advance Indexing

NumPyNumPyBeginner
Practice Now

Introduction

In this lab, you will learn about NumPy advance indexing which is a technique used to select random elements from different rows and columns of an ndarray when the elements you want to pick are in no particular sequence.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.

Importing Required Libraries and Creating the NumPy Array

First, we need to import numpy library and create a NumPy array on which we will perform advance indexing.

import numpy as np

x = np.array([[11, 28], [23, 84], [95, 56]])
print("The original array")
print(x)

In the above code, we have imported the numpy library and created a NumPy array that we will use for advance indexing.

Integer Indexing

Using integer indexing, we can select arbitrary items based on the N-dimensional index. Each integer array is used to represent the number of indexes into that dimension.

y = x[[0, 1, 2], [0, 0, 1]]
print("The output after integer indexing")
print(y)

In the above code, we are performing integer indexing on the NumPy array x and creating a new array y that will contain the selected elements. We are selecting one element of the specified column from each row of the NumPy array x. The row index contains all row numbers, and the column index specifies the element to be selected.

Boolean Indexing

Boolean indexing is used when we want to pick elements from an ndarray based on some condition using comparison operators or some other operator.

print("The items greater than 11 are:")
print(x[x > 11])

In the above code, we are performing boolean indexing on the NumPy array x. We are returning the elements that are greater than 11 from the NumPy array x.

Combining Advanced and Basic Indexing

We can combine advanced and basic indexing using one slice (:) or ellipsis (...) with an index array.

z = x[1:4, 1:3]
print("After using basic slicing")
print(z)

y = x[1:4, [1, 2]]
print("After slicing using advance index for column")
print(y)

In the above code, we are slicing the NumPy array x. We are using basic slicing and advanced indexing for column.

Remove Not a Number Values

We can remove Not A Number (NaN) values using the complement operator (~).

a = np.array([np.nan, 1, 12, np.nan, 3, 41, 54])
print("After omitting NaN the output array is :")
print (a[~np.isnan(a)])

In the above code, we are removing Not A Number (NaN) values from the NumPy array a using the complement operator (~).

Remove Non-Complex Numbers

We can filter out non-complex numbers from an array using the iscomplex function.

a = np.array([1, 2+6j, 5, 3.5+5j])
print("After filtering the non-complex numbers :")
print (a[np.iscomplex(a)])

In the above code, we are filtering out non-complex numbers from the NumPy array a using the iscomplex function.

Summary

In this lab, you have learned about different types of advance indexing of ndarray elements in the NumPy library. You have covered some examples for different types of use-cases for advance indexing in NumPy. Using this technique, you can select random elements from different rows and columns of a NumPy array.

Other NumPy Tutorials you may like