Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Generating Random Samples with NumPy
📖 Scenario: Imagine you are a data scientist working on a project where you need to simulate data for testing. You want to create random samples from a normal distribution to mimic real-world measurements.
🎯 Goal: Learn how to generate random samples using NumPy's random module and display the generated data.
📋 What You'll Learn
Create a NumPy array of random samples from a normal distribution
Set the mean and standard deviation for the distribution
Generate a specific number of samples
Print the generated samples
💡 Why This Matters
🌍 Real World
Generating random samples is useful for simulating data when real data is not available or for testing algorithms.
💼 Career
Data scientists often need to create synthetic data to test models or understand statistical properties.
Progress0 / 4 steps
1
Import NumPy and set up sample size
Import the numpy library as np and create a variable called sample_size with the value 10.
NumPy
Hint
Use import numpy as np to import NumPy. Then create sample_size = 10.
2
Set mean and standard deviation
Create two variables: mean with value 0 and std_dev with value 1 to represent the mean and standard deviation of the normal distribution.
NumPy
Hint
Set mean = 0 and std_dev = 1.
3
Generate random samples
Use np.random.normal with parameters mean, std_dev, and sample_size to create a variable called samples that holds the random samples.
NumPy
Hint
Call np.random.normal(mean, std_dev, sample_size) and assign it to samples.
4
Print the generated samples
Print the variable samples to display the generated random numbers.
NumPy
Hint
Use print(samples) to show the array of random numbers.
Practice
(1/5)
1. What does the numpy.random.choice function do?
easy
A. It calculates the mean of an array.
B. It selects random elements from a given array or list.
C. It sorts an array in ascending order.
D. It reshapes an array into a new shape.
Solution
Step 1: Understand the function purpose
numpy.random.choice is designed to pick random elements from a given array or list.
Step 2: Compare with other options
Sorting, calculating mean, and reshaping are different numpy functions, not related to random sampling.
Final Answer:
It selects random elements from a given array or list. -> Option B
Quick Check:
Random sampling = selecting elements randomly [OK]
Hint: Remember: choice means picking randomly from data [OK]
Common Mistakes:
Confusing choice with sorting or reshaping functions
Thinking it calculates statistics like mean
Assuming it modifies array shape
2. Which of the following is the correct syntax to randomly select 3 elements from array arr without replacement using numpy?
easy
A. numpy.random.choice(arr, 3, replace=True)
B. numpy.choice(arr, size=3, replace=False)
C. numpy.random.choice(arr, size=3, replace=False)
D. numpy.random.choice(arr, size=3, replace=True)
Solution
Step 1: Identify correct function and parameters
The function is numpy.random.choice. To select 3 elements without replacement, use size=3 and replace=False.
Step 2: Check each option
numpy.random.choice(arr, size=3, replace=False) uses correct function and parameters. numpy.random.choice(arr, 3, replace=True) uses replacement True (wrong). numpy.choice(arr, size=3, replace=False) uses wrong function name. numpy.random.choice(arr, size=3, replace=True) uses replacement True (wrong).
Final Answer:
numpy.random.choice(arr, size=3, replace=False) -> Option C
D. Size is larger than array length without replacement.
Solution
Step 1: Analyze parameters and array size
The array has 3 elements, but size=5 is requested without replacement.
Step 2: Understand replacement=False effect
Without replacement, you cannot pick more elements than exist. This causes a ValueError.
Final Answer:
Size is larger than array length without replacement. -> Option D
Quick Check:
Sampling more than available without replace=False causes error [OK]
Hint: Check if sample size > array length when replace=False [OK]
Common Mistakes:
Assuming replacement=True by default
Ignoring array length vs sample size
Thinking data type causes error
5. You want to simulate rolling a weighted 6-sided die 10 times using numpy, where side 6 is twice as likely as others. Which code correctly generates this sample?
hard
A. np.random.choice([1,2,3,4,5,6], size=10, replace=True, p=[1/7,1/7,1/7,1/7,1/7,2/7])
B. np.random.choice([1,2,3,4,5,6], size=10, replace=False, p=[1/6]*6)
C. np.random.choice([1,2,3,4,5,6], size=10, replace=True)
D. np.random.choice([1,2,3,4,5,6], size=10, replace=True, p=[1/6,1/6,1/6,1/6,1/6,1/6])
Solution
Step 1: Understand weighted probabilities
Side 6 should be twice as likely, so probabilities sum to 1 with side 6 having weight 2/7 and others 1/7 each.
Step 2: Check sampling parameters
Sampling 10 times with replacement is needed to allow repeats. np.random.choice([1,2,3,4,5,6], size=10, replace=True, p=[1/7,1/7,1/7,1/7,1/7,2/7]) uses correct probabilities and replace=True.
Step 3: Verify other options
The code with replace=False, p=[1/6]*6 incorrectly prevents repeats needed for multiple rolls. The codes with uniform probabilities (explicit [1/6,1/6,1/6,1/6,1/6,1/6] or none specified) do not weight side 6 twice as likely.
Final Answer:
np.random.choice([1,2,3,4,5,6], size=10, replace=True, p=[1/7,1/7,1/7,1/7,1/7,2/7]) -> Option A
Quick Check:
Weighted probabilities + replace=True for repeated rolls [OK]
Hint: Use p= with weights summing to 1 and replace=True [OK]