The function uses size to specify number of elements and replace to allow repeats.
Step 2: Validate each option
np.random.choice(arr, size=3, replace=True) uses correct parameters: size=3 and replace=True. Others have wrong parameter names or extra invalid ones.
Final Answer:
np.random.choice(arr, size=3, replace=True) -> Option C
Quick Check:
Correct syntax = size=3, replace=True [OK]
Hint: Use size= and replace= parameters correctly [OK]
Common Mistakes:
Using positional argument without size=
Setting replace=False when repeats are needed
Adding invalid parameters like axis
3.
What is the output of this code?
import numpy as np
np.random.seed(0)
arr = np.array([1, 2, 3, 4, 5])
result = np.random.choice(arr, size=4, replace=False)
print(result)
medium
A. [5 1 4 3]
B. [4 5 1 3]
C. [1 2 3 4]
D. [2 5 1 4]
Solution
Step 1: Understand seed and choice without replacement
Setting seed ensures reproducible random results. Choosing 4 unique elements without replacement picks 4 different values from arr.
Step 2: Run code or recall output
With seed 0, the output is [2 5 1 4].
Final Answer:
[2 5 1 4] -> Option D
Quick Check:
Seed 0 + replace=False = [2 5 1 4] [OK]
Hint: Use np.random.seed for consistent random output [OK]
Common Mistakes:
Ignoring seed and expecting different output
Confusing replace=True and replace=False results
Assuming output is sorted
4.
Identify the error in this code snippet:
import numpy as np
arr = np.array([1, 2, 3])
result = np.random.choice(arr, size=5, replace=False)
print(result)
medium
A. Size is larger than array length without replacement, causing an error.
B. Array is not defined properly.
C. Missing import statement for numpy.
D. replace parameter should be True to avoid error.
Solution
Step 1: Check size vs array length with replace=False
Choosing 5 elements without replacement from an array of length 3 is invalid and raises an error.
Step 2: Confirm other parts are correct
Array is defined, numpy is imported, and replace parameter is valid. The main issue is size too large without replacement.
Final Answer:
Size is larger than array length without replacement, causing an error. -> Option A
Quick Check:
Size > array length + replace=False = error [OK]
Hint: Size must not exceed array length if replace=False [OK]
Common Mistakes:
Ignoring size vs array length mismatch
Assuming replace=True by default
Not importing numpy before use
5.
You have a numpy array data = np.array([10, 20, 30, 40, 50]). You want to randomly select 3 unique elements but ensure the number 20 is always included in the result. Which approach is correct?
hard
A. Pick 3 elements with replacement and filter for 20 after.
B. Remove 20 from array, pick 2 without replacement, then add 20 back.
C. Use np.random.choice(data, size=3, replace=False) directly.
D. Pick 3 elements with replacement and add 20 manually.
Solution
Step 1: Understand the requirement
We want 3 unique elements including 20 always.
Step 2: Choose method to guarantee 20
Remove 20, pick 2 unique elements from remaining, then add 20 to result ensures 20 is included and no duplicates.
Step 3: Evaluate other options
Direct random choice may exclude 20. Picking with replacement can cause duplicates or exclude 20. Adding 20 manually after picking with replacement may cause duplicates.
Final Answer:
Remove 20 from array, pick 2 without replacement, then add 20 back. -> Option B
Quick Check:
Guarantee element by picking rest then adding it [OK]
Hint: Pick others without 20, then add 20 to ensure inclusion [OK]
Common Mistakes:
Assuming random choice always includes 20
Using replacement causing duplicates
Adding 20 after picking with replacement causing repeats