0
0
NumPydata~20 mins

Normal distribution with normal() in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Normal Distribution Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of numpy normal() with fixed seed
What is the output of this code snippet that generates 3 random numbers from a normal distribution with mean 0 and standard deviation 1?
NumPy
import numpy as np
np.random.seed(0)
samples = np.random.normal(0, 1, 3)
print(samples)
A[0.49671415 -0.1382643 0.64768854]
B[1.76405235 0.40015721 0.97873798]
C[ 0.76103773 0.12167502 0.44386323]
D[ 1.86755799 -0.97727788 0.95008842]
Attempts:
2 left
💡 Hint
Remember to set the random seed before generating samples to get reproducible results.
data_output
intermediate
1:30remaining
Shape of output array from normal()
What is the shape of the array produced by this code?
NumPy
import numpy as np
result = np.random.normal(loc=5, scale=2, size=(4,3))
print(result.shape)
A(4, 3)
B(4,)
C(12,)
D(3, 4)
Attempts:
2 left
💡 Hint
The size parameter defines the shape of the output array.
visualization
advanced
2:30remaining
Histogram of samples from normal distribution
Which option shows the correct histogram plot code for 1000 samples from a normal distribution with mean 0 and standard deviation 1?
NumPy
import numpy as np
import matplotlib.pyplot as plt
samples = np.random.normal(0, 1, 1000)
# Fill in the code to plot histogram
A
plt.bar(range(30), samples[:30])
plt.title('Bar plot of first 30 samples')
plt.show()
B
plt.plot(samples)
plt.title('Line plot of samples')
plt.show()
C
plt.scatter(range(1000), samples)
plt.title('Scatter plot of samples')
plt.show()
D
plt.hist(samples, bins=30, color='blue', edgecolor='black')
plt.title('Histogram of Normal Samples')
plt.show()
Attempts:
2 left
💡 Hint
Histograms show frequency distribution of data values.
🧠 Conceptual
advanced
1:30remaining
Effect of scale parameter in normal()
What happens to the spread of data when you increase the scale parameter in np.random.normal(loc=0, scale=scale_value, size=1000)?
AThe data points become more concentrated around the mean, decreasing the standard deviation.
BThe mean of the data shifts to the scale value.
CThe data points become more spread out, increasing the standard deviation.
DThe number of data points generated increases.
Attempts:
2 left
💡 Hint
Scale is the standard deviation of the normal distribution.
🔧 Debug
expert
2:00remaining
Identify error in normal() usage
What error will this code raise and why? import numpy as np samples = np.random.normal(0, -1, 5) print(samples)
NumPy
import numpy as np
samples = np.random.normal(0, -1, 5)
print(samples)
AValueError: scale < 0
BTypeError: loc must be a float
CSyntaxError: invalid syntax
DNo error, prints 5 samples
Attempts:
2 left
💡 Hint
Scale (standard deviation) must be positive.