Advanced indexing
NumPy Advanced indexing returns a copy of data rather than a view of it. Advanced indexing is of two types integer and Boolean.
Advanced indexing in the NumPy array allows you to access and manipulate complex patterns of the data.
Advanced indexing is triggered when obj is :
- an ndarray of type integer or Boolean
- or a tuple with at least one sequence object
- is a non tuple sequence object
Types of Advanced Indexing
There are two types of Advanced Indexing in NumPy array indexing:
- Purely integer indexing
- Boolean integer indexing
Purely integer array indexing
Purely integer array indexing allows us to access elements from an ndarray (N-dimensional array) using integers.
When integers are used for indexing. Each element of the first dimension is paired with the element of the second dimension. So the index of the elements in this case are (0,0),(1,0),(2,1) and the corresponding elements are selected.
Example: Using purely integer array indexing
Python
# Python program showing advanced indexing import numpy as np a = np.array([[ 1 , 2 ],[ 3 , 4 ],[ 5 , 6 ]]) print (a[[ 0 , 1 , 2 ],[ 0 , 0 , 1 ]]) |
Output :
[1 3 6]
Boolean Indexing
This indexing has some boolean expressions as the index.
Those elements are returned which satisfy that Boolean expression. It is used for filtering the desired element values.
Example 1: Using boolean indexing on NumPy array to find numbers greater than 50
Python
# You may wish to select numbers greater than 50 import numpy as np a = np.array([ 10 , 40 , 80 , 50 , 100 ]) print (a[a> 50 ]) |
Output :
[80 100]
Example 2: Using boolean indexing on NumPy array to find numbers whose sum row is 10
Python
# You may wish to select those elements whose # sum of row is a multiple of 10. import numpy as np b = np.array([[ 5 , 5 ],[ 4 , 5 ],[ 16 , 4 ]]) sumrow = b. sum ( - 1 ) print (b[sumrow % 10 = = 0 ]) |
Output :
array([[ 5, 5], [16, 4]])
Conclusion
Indexing and Slicing are very important concepts for accessing data. Python NumPy allows you very easy methods for indexing and slicing elements from a NumPy array.
In this tutorial, we have basic slicing and advanced indexing in NumPy Python. We have explained basic slicing with examples and also dived deep into Advanced indexing. We have covered types of advanced indexing and explained each type in easy words with examples.
After completing this tutorial you will be easily able to apply index and slicing operations on your NumPy ndarrays. This tutorial will help you to save time and gain more skills.
Basic Slicing and Advanced Indexing in NumPy
Indexing a NumPy array means accessing the elements of the NumPy array at the given index.
There are two types of indexing in NumPy: basic indexing and advanced indexing.
Slicing a NumPy array means accessing the subset of the array. It means extracting a range of elements from the data.
In this tutorial, we will cover basic slicing and advanced indexing in the NumPy. NumPy arrays are optimized for indexing and slicing operations making them a better choice for data analysis projects.
Prerequisites
Numpy in Python Introduction