Recall & Review
beginner
What does the numpy function
np.random.choice() do?It selects one or more random elements from a given array or list. You can choose with or without replacement.
Click to reveal answer
beginner
How do you select 3 random elements from an array
arr without repeating any element?Use
np.random.choice(arr, size=3, replace=False). The replace=False means no repeats.Click to reveal answer
beginner
What happens if you set
replace=True in np.random.choice()?Elements can be picked more than once. This means the same element can appear multiple times in the output.
Click to reveal answer
intermediate
How can you assign different probabilities to elements when using
np.random.choice()?Use the
p parameter with a list of probabilities that sum to 1. For example, p=[0.1, 0.9].Click to reveal answer
beginner
What is the default behavior of
np.random.choice() if you do not specify size?It returns a single random element from the array, not an array of elements.
Click to reveal answer
What does
np.random.choice(arr, size=5, replace=False) do?✗ Incorrect
With
replace=False, elements cannot repeat, so 5 unique elements are chosen.How do you make sure elements can repeat when randomly choosing from an array?
✗ Incorrect
Setting
replace=True allows elements to be picked multiple times.What parameter lets you give different chances to elements in
np.random.choice()?✗ Incorrect
The
p parameter is a list of probabilities for each element.If you want only one random element from an array, what is the simplest call?
✗ Incorrect
By default,
size is 1, so it returns one element.What will happen if you try
np.random.choice(arr, size=10, replace=False) but arr has only 5 elements?✗ Incorrect
Without replacement, you cannot pick more elements than exist, so it raises an error.
Explain how to use
np.random.choice() to pick 4 random elements from an array with no repeats.Think about the parameters controlling number and repetition.
You got /4 concepts.
Describe how to assign different probabilities to elements when randomly choosing from an array using numpy.
Look for the parameter that controls chances for each element.
You got /3 concepts.