Data Analysis for Engineers/Module 3

Module 3 · Section 5 of 11

Lesson 3.4 - Statistical Functions

Target: ~9 min read - 20 min hands-on

Overview

np.mean(), np.std(), np.percentile(), and np.histogram() are the building blocks of quality control. We'll apply them to a batch of machined-part measurements - the kind of check every manufacturing and QA/QC engineer runs.

Why This Matters (Engineering Context)

Statistical acceptance - not just "did any single part fail" but whether the process mean sits safely inside the spec and how much spread there is - is standard practice in manufacturing, and the same math underlies acceptance testing for concrete, cable, fasteners, and chemical product batches.

Code-Along

import numpy as np

np.random.seed(3)   # fixed seed -> the "random" sample is identical every run

# [Industrial] 30 machined shaft diameters (mm), drawn from a Normal distribution
n = 30
diam_mm = np.random.normal(loc=25.02, scale=0.03, size=n)   # loc = mean, scale = std dev

mean_d = np.mean(diam_mm)
std_d = np.std(diam_mm, ddof=1)          # ddof=1 -> divide by (n-1): the SAMPLE std dev
p10, p90 = np.percentile(diam_mm, [10, 90])   # pass a list -> get both percentiles back

print(f"Mean: {mean_d:.3f} mm   Std: {std_d:.3f} mm")
print(f"10th pct: {p10:.3f} mm   90th pct: {p90:.3f} mm")

lsl, usl = 24.95, 25.05                  # lower / upper spec limits
# element-wise comparisons give boolean arrays; | is element-wise OR
outside = (diam_mm < lsl) | (diam_mm > usl)
print(f"\nParts outside spec [{lsl}, {usl}] mm: {outside.sum()} of {n}")   # True counts as 1

# Cpk = how many "3-sigma widths" of margin the closer spec limit has
cpk = min(usl - mean_d, mean_d - lsl) / (3 * std_d)
print(f"Cpk: {cpk:.2f}")

# np.histogram bins the data; returns the count per bin and the bin edges
counts, edges = np.histogram(diam_mm, bins=6)
print("\nHistogram counts:", counts)
print("Bin edges (mm):", np.round(edges, 3))

Run it: with np.random.seed(3) the numbers are identical every run. The point is the pattern: compare the process mean and spread against the spec limits and a capability target, rather than eyeballing the raw data.

Practice Exercises

  1. Compute the coefficient of variation (std / mean) for the diameter data.
  2. Identify which specific samples fall outside the spec limits, using boolean indexing (not just the count).
  3. Re-run with a tighter scale (0.01 instead of 0.03) - what happens to Cpk and the out-of-spec count?
# Try the practice exercises here

Knowledge Check

  1. Why use ddof=1 when computing a sample standard deviation with NumPy?
  2. What does a Cpk well above 1.0 tell you about a process?
  3. What does the 90th percentile of the measurement data tell you?
Answer key
  1. ddof=1 applies Bessel's correction (N-1), the standard unbiased estimator for a sample
  2. The process spread fits comfortably inside the spec limits, with margin
  3. 90% of the measured parts fall at or below that value

27 / 63 sections · Course home · Join the coaching cohort