Module 6 · Section 6 of 8
Case Study 6.5 - Manufacturing Quality Control
Industrial Engineering
An electronics assembly line in a PH economic zone logs two measurements per fastening operation: torque (N-m) and rotation angle (deg). Use control limits and an anomaly detector to separate routine variation from assignable-cause defects.
from sklearn.ensemble import IsolationForest
np.random.seed(105)
n = 500
torque = np.random.normal(12.0, 0.35, n) # N-m, normal process variation
angle = np.random.normal(35.0, 1.4, n) # deg
# manually corrupt a handful of records to simulate real assignable-cause defects
for i in (90, 210, 360, 470):
torque[i] += np.random.uniform(2.0, 3.5)
angle[i] += np.random.uniform(4.0, 7.0)
qc = pd.DataFrame({"torque_nm": torque, "angle_deg": angle})
# --- Univariate SPC: 3-sigma limits on torque alone ---
mu, sd = qc["torque_nm"].mean(), qc["torque_nm"].std()
ucl, lcl = mu + 3 * sd, mu - 3 * sd
spc_flags = (qc["torque_nm"] > ucl) | (qc["torque_nm"] < lcl) # boolean Series
print(f"Torque UCL/LCL: {lcl:.2f} / {ucl:.2f} N-m")
print(f"SPC out-of-limit points: {int(spc_flags.sum())}")
# --- Multivariate: IsolationForest flags points that are unusual in (torque, angle) jointly ---
# contamination = the fraction of points you expect to be anomalies
iso = IsolationForest(contamination=0.02, random_state=1).fit(qc[["torque_nm", "angle_deg"]])
iso_flags = iso.predict(qc[["torque_nm", "angle_deg"]]) == -1 # predict() returns +1 normal / -1 anomaly
print(f"IsolationForest anomalies: {int(iso_flags.sum())}")
fig, ax = plt.subplots(figsize=(7, 6))
# ~iso_flags = the normal points; iso_flags = the flagged ones
ax.scatter(qc.loc[~iso_flags, "torque_nm"], qc.loc[~iso_flags, "angle_deg"], alpha=0.4, label="normal")
ax.scatter(qc.loc[iso_flags, "torque_nm"], qc.loc[iso_flags, "angle_deg"], color="red", label="anomaly")
ax.set_xlabel("Torque (N-m)"); ax.set_ylabel("Angle (deg)")
ax.set_title("Fastening QC - Anomaly Detection"); ax.legend()
plt.tight_layout(); plt.show()
Key Takeaways
- A univariate control chart on torque alone misses defects that only show up as an unusual combination of torque and angle - multivariate detection catches those.
contaminationis a tuning knob: set it from the historical defect rate, then review the flagged points with a process engineer before trusting it.- SPC and ML anomaly detection are complements, not competitors - SPC is transparent and auditable; the detector adds multivariate reach.
61 / 63 sections · Course home · Join the coaching cohort