The Normal Curve
Part of the Statistics for ML section of Coddy's Machine Learning journey. Lesson 9 of 64.
Heights, exam scores, bags of rice: many measurements pile up as a normal distribution, the bell curve. 500 bags from one machine:
Two numbers draw it: the mean puts the peak, the std sets the width. SciPy, the science library, knows the formula:
from scipy import stats
ax.hist(bags, bins=16, density=True)
xs = np.linspace(bags.min(), bags.max(), 100)
ax.plot(xs, stats.norm.pdf(xs, bags.mean(), bags.std()))stats.norm.pdf(x, mean, std) is the curve's height at x, one value or an array. Its area is 1, like a density histogram's, so both share a chart.
Some models prefer roughly normal features. This chart checks that.
Challenge
EasyBolts should be 50 mm long. The starter holds 240 measured lengths in lengths and sets up the chart. Check them against a normal curve:
- Print their mean and std (
np.std), rounded to 2 decimals, as below. - Draw a density histogram with
bins=15. - Draw the fitted curve over it: 100 points from the shortest to the longest bolt.
- Show the chart.
For another batch:
Fitted curve: mean 49.99 mm, std 0.42 mmand the chart:
Try it yourself
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
# Don't change the lines below
rng = np.random.default_rng(31)
lengths = np.round(rng.normal(50, 0.4, size=240), 2)
fig, ax = plt.subplots()
ax.set_title("Bolt Lengths")
ax.set_xlabel("Length (mm)")
ax.set_ylabel("Density")
# Write your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Statistics for ML
1Center and Spread
Mean and MedianPercentiles and QuartilesVariance and StdSample or Population?The Most Common ValueRecap: Delivery Times4Samples and Populations
Population and SampleSample Means VaryThe Sampling DistributionBigger Samples, Less NoiseRecap: Polling a Town2Distributions
Density HistogramsShapes of DataThe Normal CurveThe 68-95-99.7 RuleZ-ScoresRecap: Battery Life3Probability by Simulation
Probability as FrequencyThe Law of Large NumbersSums of DiceExpected ValueConditional ProbabilityRecap: Dice Stall Odds6Relationships
Reading Scatter PlotsCorrelationCorrelation MatrixCorrelation HeatmapsCorrelation Is Not CauseRecap: Ice Cream and SunPractice on your own: Online Python compiler