0
0
NumPydata~10 mins

Normal distribution with normal() 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 normal distribution with mean 0 and standard deviation 1.

NumPy
import numpy as np
samples = np.random.normal(loc=0, scale=[1], size=5)
print(samples)
Drag options to blanks, or click blank then click option'
A10
B0
C1
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 for scale which results in all zeros.
Confusing mean (loc) with standard deviation (scale).
2fill in blank
medium

Complete the code to generate 10 random numbers from a normal distribution with mean 5 and standard deviation 2.

NumPy
import numpy as np
samples = np.random.normal(loc=[1], scale=2, size=10)
print(samples)
Drag options to blanks, or click blank then click option'
A5
B0
C2
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using scale instead of loc for the mean.
Setting mean to 0 instead of 5.
3fill in blank
hard

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

NumPy
import numpy as np
samples = np.random.normal(loc=0, scale=1, size=[1])
print(samples)
Drag options to blanks, or click blank then click option'
A3
B1
C0
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Passing size as a string instead of an integer.
Using scale 0 which results in no variation.
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', 'bat', 'carrot', 'dog', 'elephant']
lengths = {word: [1] for word in words if len(word) [2] 4}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using '<' instead of '>' in the condition.
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 = ['sun', 'moon', 'star', 'sky', 'planet']
result = { [1]: [2] for word in words if len(word) [3] 3 }
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original word as key instead of uppercase.
Using '<' instead of '>' in the condition.
Using word instead of length for values.