NumPy's fancy indexing allows you to select elements from an array using arrays of indices or boolean masks. It provides a powerful way to access and manipulate elements in arrays. Fancy indexing is implemented efficiently in NumPy, typically using C or Cython under the hood for optimized performance.
Let's go through five examples of fancy indexing and explain each step in detail:
Example 1: Basic Fancy Indexing
import numpy as np
# Creating an array
arr = np.array([1, 2, 3, 4, 5])
# Fancy indexing
indices = [0, 2, 4]
result = arr[indices]
print(result)
Explanation:
- We import the NumPy library.
- We create an array
arr
containing[1, 2, 3, 4, 5]
. - We define
indices
as[0, 2, 4]
, indicating the indices we want to select. - Using fancy indexing
arr[indices]
, we select elements at indices0
,2
, and4
. - The selected elements
[1, 3, 5]
are returned asresult
.
Output: [1 3 5]
Example 2: Fancy Indexing with 2D Array
import numpy as np
# Creating a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Fancy indexing
indices = [0, 2]
result = arr[:, indices]
print(result)
Explanation:
- We create a 2D array
arr
. -
arr
is:
[[1 2 3]
[4 5 6]
[7 8 9]]
- We define
indices
as[0, 2]
, indicating the columns we want to select. - Using fancy indexing
arr[:, indices]
, we select all rows (:
) and columns at indices0
and2
. - The selected columns
[[1 3] [4 6] [7 9]]
are returned asresult
.
Output:
[[1 3]
[4 6]
[7 9]]
Example 3: Fancy Indexing with Boolean Mask
import numpy as np
# Creating an array
arr = np.array([1, 2, 3, 4, 5])
# Boolean mask
mask = np.array([True, False, True, False, True])
result = arr[mask]
print(result)
Explanation:
- We create an array
arr
containing[1, 2, 3, 4, 5]
. - We define a boolean mask
mask
whereTrue
indicates the elements to be selected. - Using fancy indexing
arr[mask]
, we select elements where the mask isTrue
. - The selected elements
[1, 3, 5]
are returned asresult
.
Output: [1 3 5]
Example 4: Combining Fancy Indexing and Slicing
import numpy as np
# Creating a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# Fancy indexing with slicing
indices = [0, 2]
result = arr[:2, indices]
print(result)
Explanation:
- We create a 2D array
arr
. - We define
indices
as[0, 2]
. - Using fancy indexing and slicing
arr[:2, indices]
, we select the first two rows and columns at indices0
and2
. - The selected elements
[[1 3] [4 6]]
are returned asresult
.
Output:
[[1 3]
[4 6]]
Example 5: Fancy Indexing with Repeating Indices
import numpy as np
# Creating an array
arr = np.array([1, 2, 3, 4, 5])
# Fancy indexing with repeating indices
indices = [0, 0, 2, 2, 4, 4]
result = arr[indices]
print(result)
Explanation:
- We create an array
arr
containing[1, 2, 3, 4, 5]
. - We define
indices
with repeating indices. - Using fancy indexing
arr[indices]
, NumPy repeats the elements at specified indices. - The selected elements
[1, 1, 3, 3, 5, 5]
are returned asresult
.
Output: [1 1 3 3 5 5]
These examples demonstrate the versatility and power of fancy indexing in NumPy, allowing you to select elements from arrays in various ways to suit your needs.
Top comments (0)