Fancy indexing enables advanced and flexible way to retrieve data from the numpy array.
By using fancy indexing we use a very simple syntax to get any data from the array.
Syntax:
array[[row_indices], [column_indices]]
Example or problem (Let's say we have an numpy array:) :
import numpy as np
x = np.array([[12, 34, 45, 45], [34, 5, 1, 63], [12, 23, 6, 34], [111, 222, 2, 333]])
print(x)
[[ 12 34 45 45]
[ 34 5 1 63]
[ 12 23 6 34]
[111 222 2 333]]
Question is : Retrieve the first and fourth rows and second and third columns only using indexing:
Solution:
Now, with the help of simple indexing, it looks like impossible to retrieve this data. So let's do this with fancy indexing:
import numpy as np
x = np.array([[12, 34, 45, 45], [34, 5, 1, 63], [12, 23, 6, 34], [111, 222, 2, 333]])
row_indexes = [0, 2]
print(x[row_indexes][:])
Note : Note: Remember, when we use : this symbol, it means all rows or columns.
Output: By this we simply get 0 index and 2 nd index rows.
[[12 34 45 45]
[12 23 6 34]]
Not we target the columns only to complete the problem:
import numpy as np
x = np.array([[12, 34, 45, 45], [34, 5, 1, 63], [12, 23, 6, 34], [111, 222, 2, 333]])
column_indexes = [1, 2]
print(x[:, column_indexes])
Output: By this we simply get 1 index and 2 nd index columns.
[[ 34 45]
[ 5 1]
[ 23 6]
[222 2]]
Let's merge these two queries to achieve the desired result.
import numpy as np
x = np.array([[12, 34, 45, 45], [34, 5, 1, 63], [12, 23, 6, 34], [111, 222, 2, 333]])
row_indexes = [0, 2]
column_indexes = [1, 2]
print(x[row_indexes, :][:, column_indexes])
Output: By merging these two queries we get our result :
[[34 45]
[23 6]]
Let's do one more example:
We have a numpy array:
import numpy as np
pq = np.array([[6, 113, 83, 115, 105], [110, 83, 76, 96, 18], [ 33, 76, 5, 84, 58], [116, 105, 11, 84, 69]])
print(pq)
From this numpy array, i need to get this data:
[[ 6 113 83 115 105]
[110 83 76 96 18]
[ 33 76 5 84 58]
[116 105 11 84 69]]
Solution:
import numpy as np
pq = np.array([[6, 113, 83, 115, 105], [110, 83, 76, 96, 18], [ 33, 76, 5, 84, 58], [116, 105, 11, 84, 69]])
print(pq[[1, 2, 3], :][:, [1, 2, 3]])
Output:
[[ 83 76 96]
[ 76 5 84]
[105 11 84]]
15+ Fancy Indexing Interview Questions in Numpy
Question 1: What is fancy indexing in NumPy?
Answer: Fancy indexing is a technique in NumPy that allows accessing array elements using an array or list of integer indices, rather than slicing with simple ranges.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
indices = [0, 2, 4]
print(arr[indices])
Output:
[10 30 50]
Question 2: How is fancy indexing different from regular slicing?
Answer: Regular slicing uses a range or step (:) to extract data, while fancy indexing uses specific indices in the form of arrays or lists.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[1:4]) # Slices a range
# Fancy Indexing
indices = [0, 2, 4]
print(arr[indices]) # Selects specific elements
Output:
[20 30 40]
[10 30 50]
Question 3: Can fancy indexing be used on multidimensional arrays?
Answer: Yes, fancy indexing works with multidimensional arrays.
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
row_indices = [0, 2]
col_indices = [1, 2]
print(arr[row_indices, col_indices])
Output:
[2 9]
Question 4: How can you retrieve rows and columns using fancy indexing?
Answer: To retrieve specific rows and columns, you combine fancy indexing for rows and columns.
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
rows = [0, 2]
cols = [1, 2]
print(arr[rows][:, cols])
Output:
[[2 3]
[8 9]]
Question 5: What happens if you use out-of-bound indices in fancy indexing?
Answer: Using out-of-bound indices raises an IndexError.
Example:
import numpy as np
arr = np.array([1, 2, 3, 4])
indices = [0, 5] # 5 is out-of-bounds
print(arr[indices]) # Uncommenting will raise an error
Output:
IndexError: index 5 is out of bounds for axis 0 with size 4
Question 6: Can you mix slicing and fancy indexing?
Answer: Yes, slicing and fancy indexing can be mixed in the same operation.
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr[1:, [0, 2]])
Output:
[[4 6]
[7 9]]
Question 7: How do you use fancy indexing to access all rows but specific columns?
Answer: Use a slice for rows (:) and fancy indexing for columns.
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr[:, [0, 2]])
Output:
[[1 3]
[4 6]
[7 9]]
Question 8: Can you assign values using fancy indexing?
Answer: Yes, you can assign values to specific elements using fancy indexing.
Example:
[ 10 200 30 400 50]
Output:
[[ 83 76 96]
[ 76 5 84]
[105 11 84]]
Question 9: How can fancy indexing be used to retrieve diagonal elements?
Answer: Diagonal elements can be accessed by providing matching row and column indices.
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr[[0, 1, 2], [0, 1, 2]])
Output:
[1 5 9]
Question 10: Is it possible to use boolean arrays with fancy indexing?
Answer: Yes, boolean arrays can also be used to filter elements.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
mask = [True, False, True, False, True]
print(arr[mask])
Output:
[10 30 50]
Question 11: Can you use negative indices in fancy indexing?
Answer: Yes, negative indices work in fancy indexing, referring to elements from the end.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[[-1, -3]])
Output:
[50 30]
Question 12: How do you combine rows and columns with fancy indexing?
Answer: Provide row and column indices together in a tuple.
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
rows = [0, 1]
cols = [2, 0]
print(arr[rows, cols])
Output:
[3 4]
Question 13: How can you use fancy indexing to rearrange array elements?
Answer: Fancy indexing can rearrange array elements by specifying indices in the desired order.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
new_order = [4, 0, 3, 1, 2]
print(arr[new_order])
Output:
[50 10 40 20 30]
Question 14: What is the result of x[row_indices][:, column_indices] in fancy indexing?
Answer: It retrieves elements by first slicing rows, then applying fancy indexing to columns.
Example:
import numpy as np
x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
rows = [0, 2]
cols = [1, 2]
print(x[rows][:, cols])
Output:
[[2 3]
[8 9]]
Question 15: Can fancy indexing be applied to 3D arrays?
Answer: Yes, fancy indexing works for 3D arrays.
Example:
import numpy as np
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr[[0, 1], [0, 1], [1, 0]])
Output:
[2 7]
Leave a comment
You must be logged in to post a comment.