Menu
CoddyTech

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:

A density histogram of 500 rice bag weights with an orange bell curve over it. A dashed line marks the mean, 1000 g, at the peak, and an arrow 8 g long marks the std, the width

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 icon

Challenge

Easy

Bolts should be 50 mm long. The starter holds 240 measured lengths in lengths and sets up the chart. Check them against a normal curve:

  1. Print their mean and std (np.std), rounded to 2 decimals, as below.
  2. Draw a density histogram with bins=15.
  3. Draw the fitted curve over it: 100 points from the shortest to the longest bolt.
  4. Show the chart.

For another batch:

Fitted curve: mean 49.99 mm, std 0.42 mm

and the chart:

A density histogram of 180 bolt lengths from about 49 to 51 mm, with a smooth bell curve over it peaking near 50

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
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Statistics for ML

Practice on your own: Online Python compiler