0
0
NumPydata~10 mins

Why advanced indexing matters in NumPy - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select the first row of the array using basic indexing.

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
first_row = arr[1]
Drag options to blanks, or click blank then click option'
A[[0]]
B[[0, 0]]
C[0, 0]
D[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using double brackets returns a 2D array instead of 1D.
Using two indices selects a single element, not a row.
2fill in blank
medium

Complete the code to select rows 0 and 2 using advanced indexing.

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
selected_rows = arr[[1]]
Drag options to blanks, or click blank then click option'
A0, 2
B[0, 2]
C[[0, 2]]
Dnp.array([0, 2])
Attempts:
3 left
💡 Hint
Common Mistakes
Using a plain list without np.array may work but is less explicit.
Using comma-separated indices causes syntax errors.
3fill in blank
hard

Fix the error in the code to select elements at positions (0,1) and (2,0) using advanced indexing.

NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
result = arr[[1], [2]]
Drag options to blanks, or click blank then click option'
Anp.array([0, 2])
B[0, 2]
C0, 2
Dnp.array([1, 0])
Attempts:
3 left
💡 Hint
Common Mistakes
Using lists instead of arrays causes unexpected behavior.
Using the same index array for both rows and columns without matching positions.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

NumPy
words = ['data', 'science', 'is', 'fun']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using the word in the condition instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 2.

NumPy
words = ['AI', 'ML', 'Data', 'Code']
result = [1]: [2] for w in words if [3]
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
Clen(w) > 2
Dw.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase conversion instead of uppercase.
Using the word itself instead of its length as the value.