0
0
NumPydata~10 mins

np.take() and np.put() for advanced selection in NumPy - Interactive Code Practice

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

Complete the code to select elements from array 'a' at indices 1 and 3 using np.take().

NumPy
import numpy as np

a = np.array([10, 20, 30, 40, 50])
indices = [1, 3]
selected = np.[1](a, indices)
print(selected)
Drag options to blanks, or click blank then click option'
Atake
Bput
Cselect
Dchoose
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.put() instead of np.take() for selection.
Trying to use np.select() which is for conditions.
2fill in blank
medium

Complete the code to replace elements at indices 0 and 2 in array 'a' with values 100 and 200 using np.put().

NumPy
import numpy as np

a = np.array([1, 2, 3, 4, 5])
indices = [0, 2]
values = [100, 200]
np.[1](a, indices, values)
print(a)
Drag options to blanks, or click blank then click option'
Aput
Btake
Cinsert
Dreplace
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.take() which only selects elements.
Trying to use np.insert() which inserts elements shifting others.
3fill in blank
hard

Fix the error in the code to correctly select elements at indices 2 and 4 from array 'a' using np.take().

NumPy
import numpy as np

a = np.array([5, 10, 15, 20, 25])
indices = (2, 4)
selected = np.[1](a, indices)
print(selected)
Drag options to blanks, or click blank then click option'
Aput
Btake
Cget
Dfetch
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.put() which is for assignment, not selection.
Using non-existent functions like np.get() or np.fetch().
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.

NumPy
words = ['apple', 'bat', 'cat', 'dog', 'elephant']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using word instead of len(word) for the value.
Using word > 3 which compares string to number.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their values only if the value is positive.

NumPy
data = {'a': 1, 'b': -2, 'c': 3}
result = { [1]: [2] for k, v in data.items() if v [3] 0 }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
C>
Dk.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using k.lower() instead of k.upper().
Using v < 0 which filters negative values.