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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/ArrayBasicsGroup(["`Array Basics`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/ArrayBasicsGroup -.-> numpy/1d_array("`1D Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/multi_array("`Multi-dimensional Array Creation`") numpy/ArrayBasicsGroup -.-> numpy/data_array("`Data to Array`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/variables_data_types -.-> lab-86385{{"`NumPy Advance Indexing`"}} python/numeric_types -.-> lab-86385{{"`NumPy Advance Indexing`"}} python/for_loops -.-> lab-86385{{"`NumPy Advance Indexing`"}} python/lists -.-> lab-86385{{"`NumPy Advance Indexing`"}} python/tuples -.-> lab-86385{{"`NumPy Advance Indexing`"}} python/importing_modules -.-> lab-86385{{"`NumPy Advance Indexing`"}} python/numerical_computing -.-> lab-86385{{"`NumPy Advance Indexing`"}} python/build_in_functions -.-> lab-86385{{"`NumPy Advance Indexing`"}} numpy/1d_array -.-> lab-86385{{"`NumPy Advance Indexing`"}} numpy/multi_array -.-> lab-86385{{"`NumPy Advance Indexing`"}} numpy/data_array -.-> lab-86385{{"`NumPy Advance Indexing`"}} numpy/bool_idx -.-> lab-86385{{"`NumPy Advance Indexing`"}} numpy/fancy_idx -.-> lab-86385{{"`NumPy Advance Indexing`"}} end

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