
- 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 - Fancy Indexing
Fancy Indexing in NumPy
Fancy indexing in NumPy is a method to select multiple elements from an array using arrays or lists of specific index where, index is used to represent the position of element in the array. Instead of picking elements one by one, you can select multiple elements at once on your choice.
It's like giving the array a list of "indices" you want, and it gives you those values directly, making data handling faster and quicker.
Fancy indexing is the advance form of simple indexing. In simple indexing we access single elements or slices using integers, while fancy indexing access multiple elements using arrays or lists of integers. It returns a new array that is independent of the original one.
Fancy indexing allows you to access multiple elements using the following −
-
Another NumPy Array
-
Python List
Example: Simple Indexing
Let us create a 1D array to access a single element using it's position. In the below code arr[3] access the element at index 3 (fourth position) of array which is 60.
import numpy as np x = np.array([50, 90, 70, 60, 40, 100]) print("By using simple indexing:" ,x[3])
Following is the output of the above code −
By using simple indexing: 60
Fancy Indexing Using a NumPy Array
Let us create a 1D array using arange() function that includes numbers from 20 to 30, then we will create a second NumPy array for indexing multiple elements at once.
In the below code we have created a second array which contains indices to access multiple elements i.e. 3, 4, 6 and the elements in those positions are 23, 24, 26.
import numpy as np x = np.arange(20, 31) print(x) arr = np.array([ 3, 4, 6]) print("The elements at 3, 4, 6 positions are :\n" , x[arr])
Following is the output of the above code −
[20 21 22 23 24 25 26 27 28 29 30] The elements at 3, 4, 6 positions are : [23 24 26]
Fancy Indexing Using a Python List
Let us create a 1D array using randint which generates random integers. Then by using python list we will store the positions we want to access. Here [1, 0, 2] is stored in indices, then we will use the indices list to access the array.
import numpy as np array = np.random.randint(10, 59, size = 10) print(array) indices = [1, 0, 2] print("Accessing multiple elements using python list: \n", array[indices])
Following is the output of the above code −
[32 57 48 26 47 32 38 35 30 36] Accessing multiple elements using python list: [57 32 48]
Converting 1D to 2D Array Using Fancy Indexing
In this example, we used the arange() function to build a one-dimensional array containing numbers from 1 to 10. Here the elements we need to access is specified in 2D. In fancy indexing the shape of result is reflected by the shape of index array. Following is the code −
import numpy as np x = np.arange(1, 10) indices = np.array([[5, 3], [4, 5]]) new_2D_arr = x[indices] print(new_2D_arr)
Following is the output of the above code −
[[6 4] [5 6]]
Fancy Indexing in 2D NumPy Array
In the below example we have used fancy indexing to select multiple elements from the 2D array. The row and column indices are provided in lists and the code selects the specified elements from the 2D array.
import numpy as np x = np.arange(12) x_2D = x.reshape(3,4) row_indices = [ 1, 2] col_indices = [0, 2] selected_indices = x_2D[row_indices, col_indices] print("2D array is :\n", x_2D) print("selected elements are : \n", selected_indices)
Following is the output of the above code −
2D array is : [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] selected elements are : [ 4 10]
Fancy Indexing in 3D NumPy Array
In the below example we have created 3D array and specified depth_indices, row_indices, col_indices to select particular multiple elements using fancy indexing.
import numpy as np x = np.arange(27) x_3D = x.reshape(3, 3, 3) depth_indices = [0, 1] row_indices = [ 1, 2] col_indices = [0, 2] selected_indices = x_3D[depth_indices, row_indices, col_indices] print("3D array is :\n", x_3D) print("selected elements in the 3D array : \n", selected_indices)
Following is the output of the above code −
3D array is : [[[ 0 1 2] [ 3 4 5] [ 6 7 8]] [[ 9 10 11] [12 13 14] [15 16 17]] [[18 19 20] [21 22 23] [24 25 26]]] selected elements in the 3D array : [ 3 17]
Fancy Indexing with Negative Indices
With the help of fancy indexing we can use negative indices to access multiple elements from the end of the array. Following is the example for fancy indexing with negative indices.
import numpy as np x = np.arange(10) indices = np.array([-1, -2, -3]) print("Selected elements:", x[indices])
Following is the output of the above code −
Selected elements: [9 8 7]