Normal (Gaussian) Distribution

πŸ“Š Normal (Gaussian) Distribution in Python

The Normal Distribution (also called Gaussian distribution) is a bell-shaped curve where most of the data points cluster around the mean. It’s widely used in statistics, machine learning, and natural phenomena modeling.


βœ… 1. Characteristics of Normal Distribution

  • Symmetric about the mean

  • Mean, median, and mode are equal

  • Defined by mean (ΞΌ) and standard deviation (Οƒ)

  • Probability Density Function (PDF):

f(x)=1Οƒ2Ο€eβˆ’(xβˆ’ΞΌ)22Οƒ2f(x) = \frac{1}{\sigma \sqrt{2\pi}} e^{-\frac{(x-\mu)^2}{2\sigma^2}}


βœ… 2. Generate Normal Distribution Using NumPy

import numpy as np
import matplotlib.pyplot as plt

# Parameters
mu = 0 # mean
sigma = 1 # standard deviation
size = 1000 # number of samples

# Generate random numbers
data = np.random.normal(mu, sigma, size)

# Display first 10 numbers
print(data[:10])


βœ… 3. Visualize Normal Distribution

Histogram

plt.hist(data, bins=30, density=True, alpha=0.6, color='g')
plt.title("Histogram of Normal Distribution")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()

Using Seaborn

import seaborn as sns

sns.histplot(data, bins=30, kde=True, color='blue')
plt.title("Normal Distribution with KDE")
plt.show()

  • kde=True β†’ plots the smooth density curve


βœ… 4. Generate Normal Distribution with Different Mean & Std

data = np.random.normal(loc=50, scale=5, size=1000)

sns.histplot(data, bins=30, kde=True, color='orange')
plt.title("Normal Distribution (ΞΌ=50, Οƒ=5)")
plt.show()


βœ… 5. Check Mean and Standard Deviation

print("Mean:", np.mean(data))
print("Standard Deviation:", np.std(data))

βœ… 6. Probability Density Function (PDF)

from scipy.stats import norm

x = np.linspace(-5, 5, 100)
y = norm.pdf(x, 0, 1) # mean=0, std=1

plt.plot(x, y)
plt.title("PDF of Normal Distribution")
plt.xlabel("x")
plt.ylabel("Probability Density")
plt.show()


🧠 Summary

Parameter Meaning
mu / loc Mean (center of the distribution)
sigma / scale Standard deviation (spread)
size Number of random samples
  • Use numpy.random.normal() for generating data

  • Use matplotlib or seaborn for visualization

  • The bell curve is symmetric and widely applicable in statistics


🎯 Practice Task

  1. Generate 1000 numbers with ΞΌ=100 and Οƒ=15

  2. Plot histogram with KDE

  3. Verify mean and standard deviation of the generated data

  4. Plot the corresponding PDF curve

CodeCapsule

Sanjit Sinha β€” Web Developer | PHP β€’ Laravel β€’ CodeIgniter β€’ MySQL β€’ Bootstrap Founder, CodeCapsule β€” Student projects & practical coding guides. Email: info@codecapsule.in β€’ Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *