0
0
NumPydata~30 mins

Why random generation matters in NumPy - See It in Action

Choose your learning style9 modes available
Why random generation matters
📖 Scenario: Imagine you are a data scientist working with a large dataset of customer ages. You want to understand how random sampling can help you estimate the average age without looking at the entire dataset.
🎯 Goal: You will create a dataset of ages, set a sample size, randomly select a sample, and then calculate the average age of that sample to see how it compares to the whole dataset.
📋 What You'll Learn
Create a numpy array called ages with these exact values: 22, 25, 47, 35, 46, 52, 23, 43, 36, 44
Create a variable called sample_size and set it to 4
Use numpy.random.choice to randomly select sample_size ages from ages and store in sample
Calculate the average of sample and store it in sample_average
Print the sample array and the sample_average value
💡 Why This Matters
🌍 Real World
Random sampling is used in surveys, polls, and experiments to estimate results without checking every individual.
💼 Career
Data scientists use random sampling to analyze big data efficiently and make predictions or decisions based on samples.
Progress0 / 4 steps
1
Create the dataset of ages
Create a numpy array called ages with these exact values: 22, 25, 47, 35, 46, 52, 23, 43, 36, 44
NumPy
Need a hint?

Use np.array to create the array with the exact values.

2
Set the sample size
Create a variable called sample_size and set it to 4
NumPy
Need a hint?

Just assign the number 4 to the variable sample_size.

3
Randomly select a sample
Use numpy.random.choice to randomly select sample_size ages from ages and store the result in sample
NumPy
Need a hint?

Use np.random.choice with size=sample_size and replace=False to avoid duplicates.

4
Calculate and print the sample average
Calculate the average of sample using np.mean and store it in sample_average. Then print both sample and sample_average
NumPy
Need a hint?

Use np.mean(sample) to get the average. Then print both variables.