ML Normal Data Distribution
A normal distribution describes data that clusters symmetrically around a mean, forming the familiar bell-shaped curve that underlies much of statistics and machine learning.
What Is a Normal Distribution?
In a normal distribution, most values cluster around a central mean, becoming progressively rarer the further they sit from that mean in either direction. Plotted as a histogram or density curve, this produces the classic symmetric 'bell curve.' Many real-world measurements - heights, test scores, measurement errors, sensor noise - approximate a normal distribution, which is why it shows up constantly in statistics and machine learning.
Generating Normal Data with NumPy
NumPy's random module can generate synthetic normally distributed data, which is useful for testing algorithms, simulating noise, or learning how statistical methods behave. The function numpy.random.normal() takes three main arguments: loc (the mean), scale (the standard deviation), and size (how many values to generate).
Example
import numpy as np
# Generate 10 random values from a normal distribution
# with mean 5.0 and standard deviation 1.5
data = np.random.normal(loc=5.0, scale=1.5, size=10)
print(data)
print("Mean:", np.mean(data))
print("Std Dev:", np.std(data))Visualizing the Bell Curve with a Histogram
A single array of numbers is hard to interpret by eye, but a histogram groups values into bins and shows how many fall into each one. With enough samples, the histogram of normally distributed data will approximate the symmetric bell shape - tall in the middle and tapering off at both ends.
Example
import numpy as np
import matplotlib.pyplot as plt
# Generate 100,000 values so the bell shape is clearly visible
data = np.random.normal(loc=0, scale=1, size=100000)
plt.hist(data, bins=100)
plt.title("Standard Normal Distribution")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()- Symmetric: the left half mirrors the right half around the mean.
- Mean, median, and mode are all equal and sit at the center of the curve.
- The 68-95-99.7 rule: about 68% of values fall within 1 standard deviation of the mean, 95% within 2, and 99.7% within 3.
- Tails are asymptotic - they approach zero but never quite reach it.
- Shape is fully described by just two numbers: the mean and the standard deviation.
Example
import numpy as np
import matplotlib.pyplot as plt
# Compare a "narrow" and a "wide" normal distribution
narrow = np.random.normal(loc=50, scale=2, size=10000)
wide = np.random.normal(loc=50, scale=10, size=10000)
plt.hist(narrow, bins=100, alpha=0.6, label="scale=2")
plt.hist(wide, bins=100, alpha=0.6, label="scale=10")
plt.legend()
plt.title("Effect of Standard Deviation on Spread")
plt.show()Exercise: ML Normal Data Distribution
What shape does a normal distribution's histogram typically take?