6.2 C
London
Thursday, December 19, 2024
HomePythonProbability Distributions in PythonHow to Generate a Normal Distribution in Python (With Examples)

How to Generate a Normal Distribution in Python (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can quickly generate a normal distribution in Python by using the numpy.random.normal() function, which uses the following syntax:

numpy.random.normal(loc=0.0, scale=1.0, size=None)

where:

  • loc: Mean of the distribution. Default is 0.
  • scale: Standard deviation of the distribution. Default is 1.
  • size: Sample size.

This tutorial shows an example of how to use this function to generate a normal distribution in Python.

Related: How to Make a Bell Curve in Python

Example: Generate a Normal Distribution in Python

The following code shows how to generate a normal distribution in Python:

from numpy.random import seed
from numpy.random import normal

#make this example reproducible
seed(1)

#generate sample of 200 values that follow a normal distribution 
data = normal(loc=0, scale=1, size=200)

#view first six values
data[0:5]

array([ 1.62434536, -0.61175641, -0.52817175, -1.07296862,  0.86540763])

We can quickly find the mean and standard deviation of this distribution:

import numpy as np

#find mean of sample
np.mean(data)

0.1066888148479486

#find standard deviation of sample
np.std(data, ddof=1)

0.9123296653173484

We can also create a quick histogram to visualize the distribution of data values:

import matplotlib.pyplot as plt
count, bins, ignored = plt.hist(data, 30)
plt.show()

Generate normal distribution in Python

We can even perform a Shapiro-Wilk test to see if the dataset comes from a normal population:

from scipy.stats import shapiro

#perform Shapiro-Wilk test
shapiro(data)

ShapiroResult(statistic=0.9958659410, pvalue=0.8669294714)

 The p-value of the test turns out to be 0.8669. Since this value is not less than .05, we can assume the sample data comes from a population that is normally distributed.

This result shouldn’t be surprising since we generated the data using the numpy.random.normal() function, which generates a random sample of data that comes from a normal distribution.

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories