0
0
NumPydata~20 mins

Random choice from array in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Random Choice Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of numpy random choice with replacement
What is the output of this code snippet?
import numpy as np
np.random.seed(0)
arr = np.array([10, 20, 30, 40])
result = np.random.choice(arr, size=3, replace=True)
print(result)
NumPy
import numpy as np
np.random.seed(0)
arr = np.array([10, 20, 30, 40])
result = np.random.choice(arr, size=3, replace=True)
print(result)
A[30 30 30]
B[20 20 40]
C[40 10 10]
D[10 40 20]
Attempts:
2 left
💡 Hint
Remember that numpy.random.choice with replace=True can pick the same element multiple times.
data_output
intermediate
2:00remaining
Number of unique elements in random choice without replacement
Given this code, how many unique elements will the result contain?
import numpy as np
np.random.seed(1)
arr = np.array([5, 10, 15, 20, 25])
result = np.random.choice(arr, size=4, replace=False)
print(len(np.unique(result)))
NumPy
import numpy as np
np.random.seed(1)
arr = np.array([5, 10, 15, 20, 25])
result = np.random.choice(arr, size=4, replace=False)
print(len(np.unique(result)))
A3
B4
C5
D1
Attempts:
2 left
💡 Hint
When replace=False, elements cannot repeat in the sample.
🔧 Debug
advanced
2:00remaining
Identify the error in numpy random choice usage
What error does this code raise?
import numpy as np
arr = np.array([1, 2, 3])
result = np.random.choice(arr, size=5, replace=False)
print(result)
NumPy
import numpy as np
arr = np.array([1, 2, 3])
result = np.random.choice(arr, size=5, replace=False)
print(result)
AValueError: Cannot take a larger sample than population when 'replace=False'
BTypeError: 'replace' argument must be boolean
CIndexError: index out of bounds
DNo error, prints 5 elements
Attempts:
2 left
💡 Hint
Check if the sample size is larger than the array length when replace=False.
🚀 Application
advanced
2:00remaining
Weighted random choice output
What is the output of this code?
import numpy as np
np.random.seed(2)
arr = np.array(['red', 'green', 'blue'])
weights = [0.1, 0.7, 0.2]
result = np.random.choice(arr, size=5, p=weights)
print(result)
NumPy
import numpy as np
np.random.seed(2)
arr = np.array(['red', 'green', 'blue'])
weights = [0.1, 0.7, 0.2]
result = np.random.choice(arr, size=5, p=weights)
print(result)
A['green' 'blue' 'red' 'green' 'blue']
B['red' 'red' 'green' 'blue' 'green']
C['blue' 'blue' 'green' 'green' 'red']
D['green' 'green' 'green' 'blue' 'green']
Attempts:
2 left
💡 Hint
Weights affect the probability of each element being chosen.
🧠 Conceptual
expert
2:00remaining
Effect of random seed on numpy random choice
Which statement is true about setting a random seed before using numpy.random.choice?
ASetting the seed ensures the random choices are the same every time the code runs.
BSetting the seed makes the choices completely unpredictable.
CThe seed only affects random integers, not random choice.
DRandom seed must be set after calling numpy.random.choice to affect output.
Attempts:
2 left
💡 Hint
Think about reproducibility in random operations.