
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Advanced Indexing
Advanced indexing offers a robust method to select specific elements from a NumPy array based on predetermined conditions or guidelines.
While basic indexing, like array[1:4] gives you a "view" of the original array (where modifications to the slice affect the original data), advanced indexing always creates a copy of the selected data.
It allows you to select elements from an ndarray that is a non-tuple sequence, ndarray object of integer or Boolean data type, or a tuple with at least one item being a sequence object. This method is helpful in dynamic data selection as well as conditional data extraction.
There are two types of advanced indexing −
- Integer Indexing
- Boolean Array Indexing
Integer Indexing
This allows you to select specific elements from an array using their exact positions (indices) based on its N dimensional index. Each integer array represents the number of indexes into that dimension.
If the number of integer arrays corresponds to the dimensions of the target ndarray, selecting items becomes simple and straightforward. It's like selecting item at x, y, z position.
Example: Selecting Elements by Indices
The following example selects one element from each row of the array using integer arrays for both rows and columns. The selection includes elements at (0,0), (1,1) and (2,0) from the first array. Following is the code −
import numpy as np x = np.array([[1, 2], [3, 4], [5, 6]]) y = x[[0,1,2], [0,1,0]] print(x) print('The new array is :\n', y)
Following is the output of the above code −
[[1 2] [3 4] [5 6]] The new array is : [1 4 5]
Example: Selecting Corner Elements
In the following example, elements placed at corners of a 4X3 array are selected. The row indices of selection are [0, 0] and [3,3] whereas the column indices are [0,2] and [0,2].
import numpy as np x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]]) print('Our array is: \n', x) rows = np.array([[0,0],[3,3]]) cols = np.array([[0,2],[0,2]]) y = x[rows,cols] print('The corner elements of this array are: \n', y)
Following is the output of the above code −
Our array is: [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] The corner elements of this array are: [[ 0 2] [ 9 11]]
Example: Accessing Specific Marks
In this example we have 2D array which contains marks of three different students representing in rows and marks in three columns of different subjects Hindi, Maths, English.
Now, we will see how to access a score of student 1 in Hindi and score of student 2 in Maths. Following is the code −
import numpy as np marks = np.array([[85, 99, 88], [78, 93, 85], [86, 45, 90]]) specific_results = marks[[0, 1], [0, 1]] print('Marks:\n', marks) print('Selected marks: \n', specific_results)
Output of the above code is as follows −
Marks: [[85 99 88] [78 93 85] [86 45 90]] Selected marks: [85 93]
Example: Index Out of Bounds Error
In NumPy an index error (index out of bounds) occurs when you try to access an element in the array using the index that is outside the valid range of array dimensions.
Let us understand this with example where an array contains 3 rows but the code attempts to access the 4th row (index 3), throws index error −
import numpy as np x = np.array([[0, 1], [2, 3], [4, 65]]) print('The 2D arrays is : \n', x) print(x[3,1])
Following is the output of the above code −
The 2D arrays is : [[ 0 1] [ 2 3] [ 4 65]] Traceback (most recent call last): File "/home/cg/root/22245/main.py", line 4, inprint(x[3,1]) IndexError: index 3 is out of bounds for axis 0 with size 3
Boolean Array Indexing
NumPy's Boolean indexing lets you select array elements that meet a certain condition. It involves creating a Boolean array where each element matches up with a True or False condition. The elements from the original array is selected where the Boolean array shows True.
This approach works well to filter data using logical conditions, like comparison operations. It is a powerful tool to pull out specific values from arrays.
Example: Using a Boolean Array to Select Specific Elements
This code illustrates how to use a Boolean array as a mask for selecting certain elements from a NumPy array. The Boolean array specifies which elements are to be included (True) or excluded (False) in the final array.
import numpy as np arr = np.array([10, 20, 30, 40, 50]) bool_array = np.array([True, False, True, False, True]) selected_elements = arr[bool_array] print("Original array:", arr) print("Boolean array:", bool_array) print("Selected elements:", selected_elements)
Output of the above code is as follows −
Original array: [10 20 30 40 50] Boolean array: [ True False True False True] Selected elements: [10 30 50]
Example: Filtering Items Greater Than 5
In this example, items greater than 5 are returned as a result of Boolean indexing.
import numpy as np x = np.array([[ 0, 1, 2],[ 3, 4, 5],[ 6, 7, 8],[ 9, 10, 11]]) print('Our array is: \n', x) print('The items greater than 5 are:' , x[x > 5])
When we run above program, it produces following result −
Our array is: [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] The items greater than 5 are: [ 6 7 8 9 10 11]
Example: Removing NaN Values
In this example, NaN (Not a Number) elements are omitted by using ~ (complement operator).
import numpy as np a = np.array([np.nan, 1,2,np.nan,3,4,5]) print(a[~np.isnan(a)])
When we run above program, it produces following result −
[1. 2. 3. 4. 5.]
Example: Filtering Complex Numbers
The following example shows how to filter out the complex numbers from an array.
import numpy as np a = np.array([1, 2+6j, 5, 3.5+5j]) print(a[np.iscomplex(a)])
Output of the above code is as follows −
[2. +6.j 3.5+5.j]
Example: Extracting Even Numbers
Let us see how to extract even numbers from an array using boolean indexing −
import numpy as np x = np.array([1, 2, 3, 4, 5, 6, 7, 8]) print('The even numbers are:', x[x % 2 == 0])
Following is the output of the above code −
The even numbers are: [2 4 6 8]