Normal Data Distribution in Python
Normal Data Distribution in Python
A normal distribution (also known as Gaussian distribution) is one of the most commonly used probability distributions in statistics. It is symmetric about the mean and is characterized by its bell-shaped curve. The normal distribution is defined by two parameters:
Mean (?): The central value of the distribution.
Standard Deviation (?): A measure of the spread or dispersion of the distribution.
In Python, you can generate and visualize data that follows a normal distribution using libraries such as NumPy and Matplotlib.
1. Install Required Libraries
Make sure you have the required libraries installed:
pip install numpy matplotlib2. Generate Normal Distribution Data
You can generate a dataset following a normal distribution using numpy.random.normal().
Syntax:
numpy.random.normal(loc=0.0, scale=1.0, size=None)locis the mean (?).scaleis the standard deviation (?).sizespecifies the number of data points to generate.
Here is how to generate a normal distribution with a mean of 0 and a standard deviation of 1.
import numpy as npimport matplotlib.pyplot as plt# Generate 1000 random numbers following a normal distributionmu, sigma = 0, 0.1 # mean and standard deviationdata = np.random.normal(mu, sigma, 1000)# Print the first few data pointsprint(data[:10])3. Plotting the Normal Distribution
You can visualize the generated data using matplotlib.
# Create a histogram of the dataplt.hist(data, bins=30, density=True, alpha=0.6, color='g')# Plot a best fit line (normal distribution curve)from scipy.stats import normxmin, xmax = plt.xlim()x = np.linspace(xmin, xmax, 100)p = norm.pdf(x, mu, sigma)plt.plot(x, p, 'k', linewidth=2)# Add labels and titleplt.title('Histogram of Normal Distribution')plt.xlabel('Value')plt.ylabel('Frequency')# Show the plotplt.show()This code will generate a histogram representing the distribution of your data, and overlay it with a normal distribution curve that fits the data.
4. Descriptive Statistics
You can also calculate basic descriptive statistics like the mean and standard deviation of the generated data to verify that it follows a normal distribution.
# Calculate the mean and standard deviation of the datacalculated_mean = np.mean(data)calculated_std = np.std(data)print(f"Calculated Mean: {calculated_mean}")print(f"Calculated Standard Deviation: {calculated_std}")5. Example Code: Generating and Plotting a Normal Distribution
import numpy as npimport matplotlib.pyplot as pltfrom scipy.stats import norm# Generate 1000 random numbers following a normal distributionmu, sigma = 0, 0.1 # mean and standard deviationdata = np.random.normal(mu, sigma, 1000)# Create a histogram of the dataplt.hist(data, bins=30, density=True, alpha=0.6, color='g')# Plot a best fit line (normal distribution curve)xmin, xmax = plt.xlim()x = np.linspace(xmin, xmax, 100)p = norm.pdf(x, mu, sigma)plt.plot(x, p, 'k', linewidth=2)# Add labels and titleplt.title('Histogram of Normal Distribution')plt.xlabel('Value')plt.ylabel('Frequency')# Show the plotplt.show()# Calculate the mean and standard deviation of the datacalculated_mean = np.mean(data)calculated_std = np.std(data)print(f"Calculated Mean: {calculated_mean}")print(f"Calculated Standard Deviation: {calculated_std}")6. Explanation of the Code
Generate Data: The function
np.random.normal(mu, sigma, 1000)generates 1000 random data points with the specified mean (mu) and standard deviation (sigma).Plot Histogram: We use
plt.hist()to create a histogram of the data. Thedensity=Trueoption normalizes the histogram to create a probability density.Plot Normal Distribution Curve: We calculate the probability density function (
pdf) of the normal distribution usingscipy.stats.norm.pdf()and plot it as a curve over the histogram.Calculate Descriptive Statistics:
np.mean()andnp.std()are used to calculate the mean and standard deviation of the data.
Summary
A normal distribution is symmetric, bell-shaped, and defined by its mean and standard deviation.
You can generate normal distribution data in Python using
numpy.random.normal().The generated data can be visualized using a histogram with an overlaid normal distribution curve using
matplotlib.You can calculate descriptive statistics (mean and standard deviation) to confirm the characteristics of the data.
This is how you can work with normal data distribution in Python!