0
0
NumPydata~10 mins

Generating random samples 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 generate 5 random numbers from a uniform distribution between 0 and 1.

NumPy
import numpy as np
samples = np.random.[1](5)
Drag options to blanks, or click blank then click option'
Achoice
Brand
Cnormal
Drandint
Attempts:
3 left
💡 Hint
Common Mistakes
Using randint which generates integers, not floats.
Using normal which generates samples from a normal distribution.
2fill in blank
medium

Complete the code to generate 10 random integers between 1 and 100 (inclusive).

NumPy
import numpy as np
ints = np.random.[1](1, 101, 10)
Drag options to blanks, or click blank then click option'
Arandint
Brand
Cchoice
Drandom_sample
Attempts:
3 left
💡 Hint
Common Mistakes
Using rand which generates floats, not integers.
Using choice without specifying the range.
3fill in blank
hard

Fix the error in the code to generate 7 random samples from a normal distribution with mean 0 and standard deviation 1.

NumPy
import numpy as np
samples = np.random.normal([1], 1, 7)
Drag options to blanks, or click blank then click option'
A1
B7
C0
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing mean and standard deviation values.
Passing size as the mean.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values, but only include words longer than 4 characters.

NumPy
words = ['apple', 'dog', 'banana', 'cat', 'grape']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 4
Cword
Dword > 4
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using the word string in the condition instead of its length.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values, including only words longer than 3 characters.

NumPy
words = ['tree', 'sun', 'flower', 'sky']
result = { [1]: [2] for w in words if [3] }
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
Clen(w) > 3
Dw.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase keys instead of uppercase.
Not filtering words by length correctly.