0
0
SciPydata~20 mins

Random variable generation in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Random Variable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output of generating normal random variables
What is the output of this code snippet that generates 3 samples from a normal distribution with mean 0 and standard deviation 1 using scipy.stats?
SciPy
from scipy.stats import norm
samples = norm.rvs(loc=0, scale=1, size=3)
samples_rounded = [round(x, 2) for x in samples]
samples_rounded
A[0.0, 0.0, 0.0]
BA list of 3 float numbers close to 0 but random, e.g., [0.12, -0.45, 1.03]
C[0.5, -1.2, 0.3]
D[1, 1, 1]
Attempts:
2 left
💡 Hint
The output is random but will be three float numbers around zero.
data_output
intermediate
1:00remaining
Shape of generated random variable array
What is the shape of the array generated by this code that samples from a uniform distribution?
SciPy
from scipy.stats import uniform
samples = uniform.rvs(loc=0, scale=10, size=(4, 2))
samples.shape
A(4, 2)
B(2, 4)
C(8,)
D(4,)
Attempts:
2 left
💡 Hint
The size parameter defines the shape of the output array.
🔧 Debug
advanced
1:30remaining
Identify the error in random variable generation code
What error does this code raise when trying to generate samples from a binomial distribution?
SciPy
from scipy.stats import binom
samples = binom.rvs(n=10, p=0.5, size='5')
ATypeError: 'str' object cannot be interpreted as an integer
BValueError: Probability p must be between 0 and 1
CSyntaxError: invalid syntax
DNo error, code runs fine
Attempts:
2 left
💡 Hint
Check the type of the size argument.
🚀 Application
advanced
2:00remaining
Generating and summarizing Poisson random variables
You generate 1000 samples from a Poisson distribution with lambda=3. Which option correctly describes the expected mean and variance of the samples?
SciPy
from scipy.stats import poisson
samples = poisson.rvs(mu=3, size=1000)
mean_val = samples.mean()
var_val = samples.var()
AMean close to 0, variance close to 3
BMean close to 3, variance close to 0
CMean and variance both close to 3
DMean and variance both close to 0
Attempts:
2 left
💡 Hint
For Poisson distribution, mean equals variance.
🧠 Conceptual
expert
1:30remaining
Effect of random seed on reproducibility
Which statement about setting a random seed before generating random variables with scipy.stats is correct?
ASetting the seed has no effect on random variable generation.
BSetting the seed makes the random numbers more random.
CSetting the seed disables randomness and returns constant values.
DSetting the seed ensures the same random numbers are generated every time the code runs.
Attempts:
2 left
💡 Hint
Think about reproducibility in experiments.