0
0
NumPydata~5 mins

Random choice from array in NumPy - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AReturns the original array arr
BSelects 5 random elements from arr allowing repeats
CSelects 5 unique random elements from arr
DSelects 1 random element from arr
How do you make sure elements can repeat when randomly choosing from an array?
ASet <code>replace=True</code>
BSet <code>replace=False</code>
CSet <code>size=1</code>
DSet <code>p=None</code>
What parameter lets you give different chances to elements in np.random.choice()?
A<code>replace</code>
B<code>size</code>
C<code>arr</code>
D<code>p</code>
If you want only one random element from an array, what is the simplest call?
A<code>np.random.choice(arr)</code>
B<code>np.random.choice(arr, size=5)</code>
C<code>np.random.choice(arr, replace=False)</code>
D<code>np.random.choice(arr, p=[0.5, 0.5])</code>
What will happen if you try np.random.choice(arr, size=10, replace=False) but arr has only 5 elements?
AIt will repeat elements to fill 10
BIt will cause an error
CIt will return 5 elements only
DIt will return an empty array
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.