We use the normal() function to create numbers that follow a bell-shaped pattern. This helps us model real-world things like heights or test scores.
Normal distribution with normal() in NumPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
NumPy
numpy.random.normal(loc=0.0, scale=1.0, size=None)
loc is the average (mean) value where the data centers.
scale is how spread out the data is (standard deviation).
Examples
NumPy
numpy.random.normal()
NumPy
numpy.random.normal(loc=5, scale=2, size=3)
NumPy
numpy.random.normal(size=5)Sample Program
This code creates 10 random numbers that are mostly around 100 but can vary by about 15. It then prints the numbers, their average, and how spread out they are.
NumPy
import numpy as np # Generate 10 random numbers from a normal distribution # with mean 100 and standard deviation 15 data = np.random.normal(loc=100, scale=15, size=10) print('Generated data:', data) print('Mean of data:', np.mean(data)) print('Standard deviation of data:', np.std(data))
Important Notes
Each time you run the code, you get different numbers because they are random.
You can set a random seed with np.random.seed(number) to get the same results every time.
Summary
The normal() function creates random numbers that follow a bell curve.
You control the center with loc and the spread with scale.
This helps simulate and understand real-world data that varies naturally.
Practice
1. What does the
loc parameter control in the numpy.random.normal() function?easy
Solution
Step 1: Understand the parameters of normal()
Thenormal()function has parameterslocandscale.locsets the mean (center) of the distribution.Step 2: Identify the role of
The mean is the center point where most values cluster in a bell curve.locFinal Answer:
The center (mean) of the normal distribution -> Option BQuick Check:
loc= center [OK]
Hint: Remember: loc = center, scale = spread [OK]
Common Mistakes:
- Confusing loc with scale
- Thinking loc controls number of samples
- Assuming loc changes distribution shape
2. Which of the following is the correct syntax to generate 5 random numbers from a normal distribution with mean 10 and standard deviation 2 using numpy?
easy
Solution
Step 1: Recall the correct function and parameters
The function isnumpy.random.normal()with parameterslocfor mean,scalefor std dev, andsizefor number of samples.Step 2: Match parameters to correct syntax
numpy.random.normal(loc=10, scale=2, size=5) correctly usesloc=10,scale=2, andsize=5.Final Answer:
numpy.random.normal(loc=10, scale=2, size=5) -> Option DQuick Check:
Correct parameter names and order [OK]
Hint: Use loc=mean, scale=std, size=number [OK]
Common Mistakes:
- Using wrong parameter names like mean or std
- Mixing order without keywords
- Calling numpy.normal instead of numpy.random.normal
3. What is the output shape of the following code?
import numpy as np arr = np.random.normal(loc=0, scale=1, size=(3,4)) print(arr.shape)
medium
Solution
Step 1: Understand the size parameter
Thesizeargument is set to(3,4), which means generate a 2D array with 3 rows and 4 columns.Step 2: Check the shape of the generated array
Printingarr.shapereturns the shape tuple, which matches the size argument.Final Answer:
(3, 4) -> Option CQuick Check:
size=(3,4) means shape=(3,4) [OK]
Hint: size tuple = output shape [OK]
Common Mistakes:
- Confusing rows and columns order
- Expecting flattened array shape
- Ignoring tuple format for size
4. Identify the error in this code snippet:
import numpy as np samples = np.random.normal(mean=0, std=1, size=10) print(samples)
medium
Solution
Step 1: Check parameter names for normal()
The functionnp.random.normal()expectslocfor mean andscalefor standard deviation, notmeanorstd.Step 2: Verify other parts of the code
Import is correct, size can be integer, and print syntax is valid.Final Answer:
Incorrect parameter names: should use loc and scale instead of mean and std -> Option AQuick Check:
Use loc and scale for mean and std [OK]
Hint: Use loc=mean, scale=std; mean/std are invalid [OK]
Common Mistakes:
- Using mean or std instead of loc and scale
- Thinking size must be tuple always
- Assuming print syntax error
5. You want to simulate daily temperatures for a week that average 20°C with a standard deviation of 3°C. Which code correctly generates this data and calculates the average temperature?
hard
Solution
Step 1: Generate temperatures with correct parameters
Useloc=20for mean temperature andscale=3for standard deviation, withsize=7for a week.Step 2: Calculate the average temperature correctly
Usetemps.mean()to get the average. Round for neat output.Final Answer:
temps = np.random.normal(loc=20, scale=3, size=7) avg_temp = temps.mean() print(round(avg_temp, 2)) -> Option AQuick Check:
loc=mean, scale=std, mean() for average [OK]
Hint: Use loc=mean, scale=std, mean() to average [OK]
Common Mistakes:
- Swapping loc and scale values
- Using mean or std instead of loc and scale
- Using sum() instead of mean() for average
- Using median() instead of mean()
