All articles →

100 Exercises / anomaly detection / Abnormality detection: 100 Exercises

Manufacturing Anomaly Detection in Python | Seven Methods and Threshold Design

Compare seven anomaly detection methods on manufacturing data and choose thresholds using inspection workload, precision, recall and missed-event costs.

Capturing Early Signs in Injection Molding Equipment: Multivariate Anomaly Detection with 10 Exercises Using Machine Learning (No.051–No.060)

Overview

Monitoring temperature, vibration, current and oil pressure with separate limits can miss unusual combinations of otherwise acceptable values. Using a fictional injection-molding line, we compare seven methods: Isolation Forest, LOF, One-Class SVM, Elliptic Envelope, k-nearest-neighbor distance, KMeans and DBSCAN. We then choose thresholds using workload and loss assumptions.

Choose monitoring rules that operators can sustain. Compare inspection workload, missed-event losses, explainability and retraining requirements alongside detection performance.

About this material This article adapts notebooks used in corporate training by Surikobo or its representative, Hiroshi Wayama, and is published with permission from the client companies.
All data listed is fictional and has no relation whatsoever to real companies, factories, or figures.

Introduction: Practical Challenges in Manufacturing Covered in This Article

At injection molding facility A-01, sensor values are collected for each shot of the product. The goal of maintenance personnel is not to definitively identify failures, but to narrow down shots that differ from usual and to identify inspection targets during planned shutdowns.

The output of the anomaly detection model is treated as a Primary screening to prioritize inspections, not as a facility shutdown command. Scores can also be high due to sensor calibration defects, product switching, or startup conditions, so cross-checking with work history and quality results is essential.

Common situations on site

  • There is a large amount of normal data, but few fault labels and definitions fluctuate
  • With the control limits of a single sensor, it is impossible to detect the breakdown of relationships among multiple variables.
  • There is a limit to the number of inspection personnel, and if alerts are too frequent, operations become mere formality
  • The distribution of ‘normal’ varies depending on variety, equipment, season, and post-maintenance

Why is this issue so difficult to judge?

Unsupervised anomaly detection does not directly learn the correct label, but determines the degree of anomaly based on data sparseness, boundaries, and distribution. Therefore, even with the same data, candidates vary depending on the method and hyperparameters. Also, statistically unusual phenomena and operational abnormalities that lead to stoppages or defects are not synonymous.

Exercises covered in this article

No.Methods and IssuesOn-site Verification
051Isolation ForestShots prone to isolation in multivariate spaces
052Local Outlier FactorLocally less frequent than peripheral shots
053One-Class SVMFlexible boundaries of the normal domain
054Elliptic EnvelopeElliptical normal region considering covariance
055k-nearest-neighbor distanceAnomaly scores based on distance to nearby observations
056clusteringHandling of small groups or points far from the center
057KMeansCandidate selection based on distance to the nearest center
058DBSCANNoise points in low-density regions
059Model ComparisonComparison of detection performance, number of cases, and consensus
060threshold adjustmentReflecting inspection capability and missed costs

Preparing the Python environment

Data is handled with numpy and pandas, utilizing each model of scikit-learn. The graph uses only matplotlib. For reproducibility, the random number seed is fixed at 42.

Run the cells from top to bottom in Jupyter Notebook. [IN: n] identifies an input and [OUT: n] its output. Setup code and output are collapsed below.

Show environment setup code and output
[IN: 1]
import warnings
import numpy as np
import pandas as pd
from IPython.display import display
import matplotlib
import matplotlib.pyplot as plt
import sklearn

from sklearn.cluster import DBSCAN, KMeans
from sklearn.covariance import EllipticEnvelope
from sklearn.ensemble import IsolationForest
from sklearn.metrics import precision_score, recall_score, f1_score
from sklearn.neighbors import LocalOutlierFactor, NearestNeighbors
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import OneClassSVM

warnings.filterwarnings("ignore", category=UserWarning, module="matplotlib")
SEED = 42
np.random.seed(SEED)
pd.set_option("display.max_columns", 20)

print(f"numpy       : {np.__version__}")
print(f"pandas      : {pd.__version__}")
print(f"matplotlib  : {matplotlib.__version__}")
print(f"scikit-learn: {sklearn.__version__}")
[OUT: 1]
numpy       : 2.5.1
pandas      : 3.0.3
matplotlib  : 3.11.0
scikit-learn: 1.9.0

Creation of Fictional Data

Generate 900 ordinary shots and 40 shots requiring review. In normal operation, mold temperature and heater current are positively related, as are vibration and oil pressure. The injected observations combine increased vibration, a changed temperature–current relationship and reduced oil pressure.

The teaching label known_issue represents a condition identified during later inspection: 900 ordinary observations are labeled 0 and 40 injected observations are labeled 1. Labels are not supplied to the models, but scaling, fitting and comparison use the same 940 observations. The results compare outlier detection within this dataset; evaluate future-data performance on a separate period.

Show synthetic data generation code and output
[IN: 2]
rng = np.random.default_rng(SEED)
n_normal, n_issue = 900, 40

mean = np.array([210.0, 2.4, 31.0, 8.0])
cov = np.array(
    [
        [16.0, 0.7, 5.0, 1.0],
        [0.7, 0.36, 0.4, 0.3],
        [5.0, 0.4, 9.0, 1.2],
        [1.0, 0.3, 1.2, 1.0],
    ]
)
normal = rng.multivariate_normal(mean, cov, n_normal)

issue_1 = rng.multivariate_normal([211, 5.4, 33, 9.4], np.diag([9, 0.20, 4, 0.35]), 14)
issue_2 = rng.multivariate_normal([222, 2.7, 25, 8.2], np.diag([8, 0.25, 3, 0.50]), 13)
issue_3 = rng.multivariate_normal([207, 3.6, 32, 5.4], np.diag([7, 0.22, 4, 0.20]), 13)
issue = np.vstack([issue_1, issue_2, issue_3])

columns = ["mold_temp_c", "vibration_mm_s", "heater_current_a", "oil_pressure_mpa"]
df = pd.DataFrame(np.vstack([normal, issue]), columns=columns)
df.insert(0, "shot_id", [f"S{i:04d}" for i in range(1, len(df) + 1)])
df["known_issue"] = np.r_[np.zeros(n_normal, dtype=int), np.ones(n_issue, dtype=int)]
X = df[columns]
X_scaled = StandardScaler().fit_transform(X)

print(
    f"Number of shots: {len(df):,} / Evaluation Confirmation: {df['known_issue'].sum()}"
)
display(df.head())
display(df.groupby("known_issue")[columns].agg(["mean", "std"]).round(2))
[OUT: 2]
Number of shots: 940 / Evaluation Confirmation: 40
shot_id mold_temp_c vibration_mm_s heater_current_a oil_pressure_mpa known_issue
0 S0001 207.579632 2.201556 32.597790 8.998515 0
1 S0002 215.879777 2.994548 37.801656 9.099905 0
2 S0003 209.024388 2.377789 32.812262 9.146080 0
3 S0004 211.071845 2.910164 28.307671 7.929873 0
4 S0005 207.416243 2.676244 32.282850 8.891665 0
mold_temp_c vibration_mm_s heater_current_a oil_pressure_mpa
mean std mean std mean std mean std
known_issue
0 210.04 3.88 2.4 0.59 31.04 2.99 7.98 1.02
1 213.47 6.42 3.9 1.25 30.52 4.45 7.63 1.75
[IN: 3]
fig, axes = plt.subplots(1, 2, figsize=(12, 4.5))
colors = np.where(df["known_issue"].eq(1), "tab:red", "tab:blue")
axes[0].scatter(df["mold_temp_c"], df["heater_current_a"], c=colors, alpha=0.55, s=20)
axes[0].set_title("Temperature vs. heater current")
axes[0].set_xlabel("Mold temperature [C]")
axes[0].set_ylabel("Heater current [A]")
axes[0].grid(True, alpha=0.3)
axes[1].scatter(
    df["vibration_mm_s"], df["oil_pressure_mpa"], c=colors, alpha=0.55, s=20
)
axes[1].set_title("Vibration vs. oil pressure")
axes[1].set_xlabel("Vibration [mm/s]")
axes[1].set_ylabel("Oil pressure [MPa]")
axes[1].grid(True, alpha=0.3)
fig.tight_layout()
plt.show()
[OUT: 3]

Creation of Fictional Data

No.051: Using Isolation Forest

Practical purpose

Isolation Forest considers points where features are isolated with few splits when randomly partitioned as anomalies. Because it does not assume a strict distribution of normal states, it is suitable for early PoCs that quickly extract candidates from complex regions created by multiple sensors.

Analysis and modeling

If the average path length of point xx is E[h(x)]E[h(x)] and the criterion for average path length at sample size nn is c(n)c(n), a standard anomaly score is

s(x,n)=2−E[h(x)]/c(n)s(x,n)=2^{-E[h(x)]/c(n)}

Values of ss closer to 1 indicate points isolated with fewer splits. In the formula, nn is the subsample size used to build each tree. contamination=0.05 sets a decision boundary from the training score distribution to flag approximately 5%; it does not estimate a failure rate. Tied scores and new observations need not yield a 5% alert rate.

Check with Python

[IN: 4]
iso = IsolationForest(n_estimators=300, contamination=0.05, random_state=SEED)
df["score_iso"] = -iso.fit(X_scaled).score_samples(X_scaled)
df["pred_iso"] = (iso.predict(X_scaled) == -1).astype(int)
display(
    df.nlargest(8, "score_iso")[
        ["shot_id", *columns, "score_iso", "known_issue"]
    ].round(3)
)

plt.figure(figsize=(8, 4.5))
plt.scatter(
    df["mold_temp_c"], df["vibration_mm_s"], c=df["score_iso"], cmap="viridis", s=22
)
plt.colorbar(label="Isolation score")
plt.title("Isolation Forest anomaly score")
plt.xlabel("Mold temperature [C]")
plt.ylabel("Vibration [mm/s]")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
[OUT: 4]
shot_id mold_temp_c vibration_mm_s heater_current_a oil_pressure_mpa score_iso known_issue
918 S0919 223.604 3.687 21.653 7.238 0.696 1
126 S0127 220.592 3.633 37.847 11.397 0.672 0
900 S0901 213.905 6.052 37.836 8.578 0.651 1
912 S0913 215.102 5.515 33.103 10.246 0.630 1
907 S0908 214.681 5.932 34.262 9.830 0.629 1
910 S0911 206.332 5.110 36.330 9.347 0.621 1
916 S0917 221.522 2.150 22.260 8.207 0.620 1
926 S0927 223.477 2.998 24.598 8.074 0.620 1

No.051: Using Isolation Forest

Reading the results

The upper ranks include shots with high vibration or those whose temperature-current relationship is far from the normal group. Since the overall score based solely on tree branching cannot determine the cause, the original sensor values from the top row are also recorded so that maintenance personnel can compare them with equipment history.

No.052: Using the Local Outlier Factor

Practical purpose

LOF compares an observation’s local density with the density of its neighbors. It can identify unusual shots within one operating group when product types or loads create several normal groups.

Analysis and modeling

If the locally reachable density of point pp is lrdk(p)\mathrm{lrd}_k(p), then LOF can be expressed as the density ratio to neighbors.

LOFk(p)=1∣Nk(p)∣∑o∈Nk(p)lrdk(o)lrdk(p)\mathrm{LOF}_k(p)=\frac{1}{|N_k(p)|}\sum_{o\in N_k(p)}\frac{\mathrm{lrd}_k(o)}{\mathrm{lrd}_k(p)}

If it’s roughly 1, it’s about the same as the neighborhood, and if it’s well greater than 1, it’s a local outlier. Sensitivity analysis is performed based on process continuity and lot size.

Check with Python

[IN: 5]
lof = LocalOutlierFactor(n_neighbors=25, contamination=0.05)
df["pred_lof"] = (lof.fit_predict(X_scaled) == -1).astype(int)
df["score_lof"] = -lof.negative_outlier_factor_
display(
    df.nlargest(8, "score_lof")[
        ["shot_id", *columns, "score_lof", "known_issue"]
    ].round(3)
)

plt.figure(figsize=(8, 4.5))
plt.scatter(
    df["vibration_mm_s"], df["oil_pressure_mpa"], c=df["score_lof"], cmap="plasma", s=22
)
plt.colorbar(label="LOF score")
plt.title("Local Outlier Factor score")
plt.xlabel("Vibration [mm/s]")
plt.ylabel("Oil pressure [MPa]")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
[OUT: 5]
shot_id mold_temp_c vibration_mm_s heater_current_a oil_pressure_mpa score_lof known_issue
922 S0923 229.429 2.657 27.339 8.708 2.082 1
918 S0919 223.604 3.687 21.653 7.238 2.026 1
126 S0127 220.592 3.633 37.847 11.397 2.018 0
901 S0902 211.210 6.057 34.612 8.425 1.901 1
900 S0901 213.905 6.052 37.836 8.578 1.887 1
478 S0479 204.327 4.137 36.266 9.103 1.858 0
903 S0904 209.262 5.785 32.073 8.304 1.836 1
907 S0908 214.681 5.932 34.262 9.830 1.832 1

No.052: Using the Local Outlier Factor

Reading the results

High LOF scores identify observations with lower density than their neighbors. Compare rankings across n_neighbors settings because normal boundary points may also be flagged. To score new data, fit with novelty=True and apply predict or score_samples only to unseen observations. Their treatment differs from negative_outlier_factor_ on the training data.

No.053: Using One-Class SVM

Practical purpose

One-Class SVM learns boundaries surrounding the majority of conventional data. Using the RBF kernel, it is possible to represent nonlinear normal regions and detect relationship breakdowns that are difficult to detect with simple upper and lower limits.

Analysis and modeling

We obtain a hyperplane that separates normal data from the origin in feature space. The conceptual optimization problem is as follows.

min⁡w,ρ,ξ12∥w∥2+1νn∑iξi−ρ\min_{w,\rho,\xi}\frac{1}{2}\|w\|^2+\frac{1}{\nu n}\sum_i\xi_i-\rho s.t. w⊤ϕ(xi)≥ρ−ξi,ξi≥0\text{s.t. } w^\top\phi(x_i)\geq\rho-\xi_i,\quad \xi_i\geq0

ν\nu relates to the upper limit of the fraction of training errors and the lower limit of the proportion of support vectors. Because it is sensitive to scale, standardization is essential.

Check with Python

[IN: 6]
ocsvm = OneClassSVM(kernel="rbf", gamma="scale", nu=0.05)
df["score_ocsvm"] = -ocsvm.fit(X_scaled).decision_function(X_scaled)
df["pred_ocsvm"] = (ocsvm.predict(X_scaled) == -1).astype(int)
display(
    df.nlargest(8, "score_ocsvm")[
        ["shot_id", *columns, "score_ocsvm", "known_issue"]
    ].round(3)
)

plt.figure(figsize=(8, 4.5))
plt.hist(
    df.loc[df.known_issue.eq(0), "score_ocsvm"],
    bins=35,
    alpha=0.65,
    label="audit: normal",
)
plt.hist(
    df.loc[df.known_issue.eq(1), "score_ocsvm"],
    bins=20,
    alpha=0.65,
    label="audit: issue",
)
plt.axvline(0, color="black", linestyle="--", label="model boundary")
plt.title("One-Class SVM score distribution")
plt.xlabel("Anomaly score (higher = more anomalous)")
plt.ylabel("Shots")
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
[OUT: 6]
shot_id mold_temp_c vibration_mm_s heater_current_a oil_pressure_mpa score_ocsvm known_issue
126 S0127 220.592 3.633 37.847 11.397 1.347 0
922 S0923 229.429 2.657 27.339 8.708 1.345 1
918 S0919 223.604 3.687 21.653 7.238 1.174 1
157 S0158 211.063 2.070 38.227 10.885 0.730 0
900 S0901 213.905 6.052 37.836 8.578 0.668 1
939 S0940 206.474 3.786 29.456 4.601 0.452 1
933 S0934 214.528 3.633 31.250 5.200 0.419 1
34 S0035 209.778 1.705 28.220 9.928 0.408 0

No.053: Using One-Class SVM

Reading the results

Zero is the decision boundary, and positive values of the negated decision function indicate candidates for review. The normal and issue score distributions overlap. Excessively large gamma can make the boundary too irregular and sensitive to ordinary variation.

No.054: Using Elliptic Envelope

Practical purpose

Elliptic Envelope is suitable for processes where normal data is generally distributed in a single-peak, elliptical pattern. To account for covariance, even when temperature and current are within their respective ranges, it can detect states that deviate from normal correlation.

Analysis and modeling

Mahalanobis distance using center μ\mu and covariance matrix Σ\Sigma

DM(x)=(x−μ)⊤Σ−1(x−μ)D_M(x)=\sqrt{(x-\mu)^\top\Sigma^{-1}(x-\mu)}

Estimate the center and covariance robustly and flag observations far from that center. The code uses -decision_function, which is squared distance minus the fitted decision threshold, rather than the distance shown above. Positive scores lie on the outlier side. Reconsider the single-ellipse assumption when normal data form multiple groups.

Check with Python

[IN: 7]
ell = EllipticEnvelope(contamination=0.05, random_state=SEED)
df["score_elliptic"] = -ell.fit(X_scaled).decision_function(X_scaled)
df["pred_elliptic"] = (ell.predict(X_scaled) == -1).astype(int)
display(
    df.nlargest(8, "score_elliptic")[
        ["shot_id", *columns, "score_elliptic", "known_issue"]
    ].round(3)
)

plt.figure(figsize=(8, 4.5))
plt.scatter(
    df["mold_temp_c"],
    df["heater_current_a"],
    c=df["score_elliptic"],
    cmap="cividis",
    s=22,
)
plt.colorbar(label="Robust distance score")
plt.title("Elliptic Envelope score")
plt.xlabel("Mold temperature [C]")
plt.ylabel("Heater current [A]")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
[OUT: 7]
shot_id mold_temp_c vibration_mm_s heater_current_a oil_pressure_mpa score_elliptic known_issue
900 S0901 213.905 6.052 37.836 8.578 36.750 1
901 S0902 211.210 6.057 34.612 8.425 36.431 1
903 S0904 209.262 5.785 32.073 8.304 30.518 1
918 S0919 223.604 3.687 21.653 7.238 28.626 1
922 S0923 229.429 2.657 27.339 8.708 28.577 1
905 S0906 211.985 5.871 31.745 8.877 27.169 1
907 S0908 214.681 5.932 34.262 9.830 24.147 1
916 S0917 221.522 2.150 22.260 8.207 20.851 1

No.054: Using Elliptic Envelope

Reading the results

Candidates whose temperature and current combinations deviate from the usual band score high. While the assumptions are clear and easy to explain, if the focus is divided by product type, the model is divided by product type or alternative methods are considered.

No.055: Calculating Anomaly Using the k-Nearest-Neighbor Method

Practical purpose

k-nearest-neighbor distance directly shows how close a similar past shot is. The algorithm is relatively transparent, making it easy to present similar shots to maintenance personnel.

Analysis and modeling

For a standardized observation xx, use its Euclidean distance to the kkth nearest other observation x(k)x_{(k)} as the anomaly score:

sk(x)=∥x−x(k)∥2s_k(x)=\|x-x_{(k)}\|_2

When the kk is small, it is sensitive to local noise, and when large, it is easier to handle abnormalities even in small normal groups. Here, we use the 10th Nearest Neighbor Distance and consider the top 5% as candidates.

Check with Python

[IN: 8]
# The query includes the point itself at distance zero; request 11 to obtain the tenth other neighbor.
knn = NearestNeighbors(n_neighbors=11).fit(X_scaled)
distances, _ = knn.kneighbors(X_scaled)
df["score_knn"] = distances[:, -1]
knn_threshold = df["score_knn"].quantile(0.95)
df["pred_knn"] = (df["score_knn"] >= knn_threshold).astype(int)
display(
    df.nlargest(8, "score_knn")[
        ["shot_id", *columns, "score_knn", "known_issue"]
    ].round(3)
)

sorted_score = np.sort(df["score_knn"])
plt.figure(figsize=(8, 4.5))
plt.plot(np.arange(1, len(df) + 1), sorted_score)
plt.axhline(knn_threshold, color="tab:red", linestyle="--", label="95th percentile")
plt.title("Sorted 10-nearest-neighbor distance")
plt.xlabel("Shots sorted by score")
plt.ylabel("10-NN distance")
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
[OUT: 8]
shot_id mold_temp_c vibration_mm_s heater_current_a oil_pressure_mpa score_knn known_issue
922 S0923 229.429 2.657 27.339 8.708 3.021 1
918 S0919 223.604 3.687 21.653 7.238 2.670 1
900 S0901 213.905 6.052 37.836 8.578 2.457 1
126 S0127 220.592 3.633 37.847 11.397 2.433 0
910 S0911 206.332 5.110 36.330 9.347 2.355 1
903 S0904 209.262 5.785 32.073 8.304 2.189 1
912 S0913 215.102 5.515 33.103 10.246 2.164 1
911 S0912 210.178 5.224 30.045 10.242 2.156 1

No.055: Calculating Anomaly Using the k-Neighborhood Method

Reading the results

The point where the distance suddenly increases at the far right is a candidate with few similar histories. While quantile thresholds make it easier to control the number of inspection items, they extract a certain number even if all processes are normal. First, it is positioned as the purpose of creating a candidate list.

No.056: Finding Outliers with Clustering

Practical purpose

By using clustering, you can organize major operating states into groups and investigate points far from the center or extremely small groups. However, a small group does not necessarily mean abnormality; it may be a small variety or a legitimate operating condition right after setup.

Analysis and modeling

There are mainly two ways to use it for anomaly detection.

  1. Set the anomaly level as the distance from each point to the center of the affiliated cluster
  2. Review clusters with few members as rare operating conditions

Clustering is not a fault detector but a tool for organizing data structures. It is necessary to assign meanings including process conditions.

Check with Python

[IN: 9]
kmeans_review = KMeans(n_clusters=4, n_init=20, random_state=SEED).fit(X_scaled)
df["cluster_review"] = kmeans_review.labels_
cluster_review = (
    df.groupby("cluster_review")
    .agg(
        shots=("shot_id", "size"),
        issue_rate=("known_issue", "mean"),
        temp_mean=("mold_temp_c", "mean"),
        vibration_mean=("vibration_mm_s", "mean"),
        pressure_mean=("oil_pressure_mpa", "mean"),
    )
    .round(3)
)
display(cluster_review)

plt.figure(figsize=(8, 4.5))
for c in sorted(df["cluster_review"].unique()):
    part = df[df["cluster_review"].eq(c)]
    plt.scatter(
        part["vibration_mm_s"],
        part["oil_pressure_mpa"],
        s=22,
        alpha=0.6,
        label=f"cluster {c}",
    )
plt.title("Operating-state clusters")
plt.xlabel("Vibration [mm/s]")
plt.ylabel("Oil pressure [MPa]")
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.show()
[OUT: 9]
shots issue_rate temp_mean vibration_mean pressure_mean
cluster_review
0 213 0.038 207.104 1.957 6.738
1 211 0.066 212.647 3.125 9.144
2 254 0.071 213.386 2.451 7.632
3 262 0.000 207.592 2.367 8.339

No.056: Finding Outliers with Clustering

Reading the results

By looking at the number of cases by cluster and the rate requiring audit confirmation, you can organize which operating groups should be prioritized for review. However, instead of making the group with a high confirmation rate the stop condition as is, we first check whether the product type, mold, and working conditions are uneven.

No.057: Extracting anomalous candidates with KMeans

Practical purpose

KMeans represents operating states with cluster centroids. A shot far from every centroid is a candidate for review. With several normal operating modes, distance to the nearest centroid may be more informative than distance to the overall mean.

Analysis and modeling

KMeans sum of squares within the cluster

∑i=1nmin⁡j∈{1,…,K}∥xi−μj∥22\sum_{i=1}^{n}\min_{j\in\{1,\ldots,K\}}\|x_i-\mu_j\|_2^2

is minimized during fitting. The anomaly score is then s(x)=min⁡j∥x−μj∥2s(x)=\min_j\|x-\mu_j\|_2. Choose the number of clusters and the threshold using operating modes and inspection capacity.

Check with Python

[IN: 10]
kmeans = KMeans(n_clusters=4, n_init=20, random_state=SEED).fit(X_scaled)
distance_matrix = kmeans.transform(X_scaled)
df["score_kmeans"] = distance_matrix.min(axis=1)
kmeans_threshold = df["score_kmeans"].quantile(0.95)
df["pred_kmeans"] = (df["score_kmeans"] >= kmeans_threshold).astype(int)
display(
    df.nlargest(8, "score_kmeans")[
        ["shot_id", *columns, "score_kmeans", "known_issue"]
    ].round(3)
)

plt.figure(figsize=(8, 4.5))
plt.scatter(
    df["mold_temp_c"], df["vibration_mm_s"], c=df["score_kmeans"], cmap="magma", s=22
)
plt.colorbar(label="Distance to nearest centroid")
plt.title("KMeans distance-based anomaly score")
plt.xlabel("Mold temperature [C]")
plt.ylabel("Vibration [mm/s]")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
[OUT: 10]
shot_id mold_temp_c vibration_mm_s heater_current_a oil_pressure_mpa score_kmeans known_issue
918 S0919 223.604 3.687 21.653 7.238 4.484 1
900 S0901 213.905 6.052 37.836 8.578 4.456 1
922 S0923 229.429 2.657 27.339 8.708 4.304 1
901 S0902 211.210 6.057 34.612 8.425 4.285 1
907 S0908 214.681 5.932 34.262 9.830 4.114 1
903 S0904 209.262 5.785 32.073 8.304 4.020 1
905 S0906 211.985 5.871 31.745 8.877 4.001 1
916 S0917 221.522 2.150 22.260 8.207 3.709 1

No.057: Extracting anomalous candidates with KMeans

Reading the results

Shots far from their nearest centroid rank highest. Because centroids are means, outliers can affect them. Exclude confirmed sensor failures before fitting, and reassess centroids when the product mix changes.

No.058: Detecting Noise Points with DBSCAN

Practical purpose

DBSCAN classifies clusters of sufficiently dense areas and judges them as noise if they do not belong to any densely packed area. It is characterized by the ability to handle non-spherical operating areas without pre-deciding on the number of clusters.

Analysis and modeling

Points with min_samples or more points within radius ε\varepsilon are considered core points, and density-reachable points are grouped into the same cluster. If the eps is too small, it triggers excessive alerts; if too large, abnormalities are absorbed into the normal group. Set at the standardized distance.

Check with Python

[IN: 11]
dbscan = DBSCAN(eps=0.62, min_samples=12).fit(X_scaled)
df["dbscan_label"] = dbscan.labels_
df["pred_dbscan"] = (df["dbscan_label"] == -1).astype(int)
dbscan_summary = (
    df.groupby("dbscan_label")
    .agg(shots=("shot_id", "size"), issue_rate=("known_issue", "mean"))
    .round(3)
)
display(dbscan_summary)

plt.figure(figsize=(8, 4.5))
plot_colors = np.where(df["pred_dbscan"].eq(1), "tab:red", "tab:blue")
plt.scatter(
    df["vibration_mm_s"], df["oil_pressure_mpa"], c=plot_colors, alpha=0.6, s=22
)
plt.title("DBSCAN noise detection (red = noise)")
plt.xlabel("Vibration [mm/s]")
plt.ylabel("Oil pressure [MPa]")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()
[OUT: 11]
shots issue_rate
dbscan_label
-1 503 0.08
0 398 0.00
1 26 0.00
2 8 0.00
3 5 0.00

No.058: Detecting Noise Points with DBSCAN

Reading the results

The red noise point is outside the dense normal area. DBSCAN does not return direct continuous scores and has high parameter sensitivity, allowing it to check whether the number of noise incidents meets the on-site inspection capacity and whether known rare normal operations are involved.

No.059: Comparing Anomaly Detection Results by Model

Practical purpose

Model selection compares not only accuracy but also the number of alerts, missed cases, agreement between models, and ease of explanation. When evaluation labels are limited, it is practical to prioritize auditing candidates commonly cited by multiple models rather than treating numerical rankings as absolute.

Analysis and modeling

For audited data, we will check the following:

Precision=TPTP+FP,Recall=TPTP+FN,F1=2PRP+R\mathrm{Precision}=\frac{TP}{TP+FP},\quad \mathrm{Recall}=\frac{TP}{TP+FN},\quad F_1=\frac{2PR}{P+R}

With rare anomalies, accuracy alone can be misleading. Also, even models with the same number of cases may not always match the candidates.

Check with Python

[IN: 12]
pred_cols = {
    "Isolation Forest": "pred_iso",
    "LOF": "pred_lof",
    "One-Class SVM": "pred_ocsvm",
    "Elliptic Envelope": "pred_elliptic",
    "kNN distance": "pred_knn",
    "KMeans distance": "pred_kmeans",
    "DBSCAN": "pred_dbscan",
}
rows = []
for name, col in pred_cols.items():
    rows.append(
        {
            "model": name,
            "alerts": int(df[col].sum()),
            "precision": precision_score(df["known_issue"], df[col], zero_division=0),
            "recall": recall_score(df["known_issue"], df[col], zero_division=0),
            "f1": f1_score(df["known_issue"], df[col], zero_division=0),
        }
    )
comparison = pd.DataFrame(rows).set_index("model").sort_values("f1", ascending=False)
display(comparison.round(3))

comparison[["precision", "recall", "f1"]].plot(
    kind="bar", figsize=(10, 4.8), ylim=(0, 1)
)
plt.title("Model comparison on audited labels")
plt.xlabel("Model")
plt.ylabel("Metric")
plt.grid(True, axis="y", alpha=0.3)
plt.legend(loc="lower right")
plt.tight_layout()
plt.show()
[OUT: 12]
alerts precision recall f1
model
Elliptic Envelope 47 0.830 0.975 0.897
KMeans distance 47 0.745 0.875 0.805
LOF 47 0.702 0.825 0.759
kNN distance 47 0.660 0.775 0.713
Isolation Forest 47 0.638 0.750 0.690
One-Class SVM 50 0.340 0.425 0.378
DBSCAN 503 0.080 1.000 0.147

No.059: Comparing Anomaly Detection Results by Model

[IN: 13]
df["model_votes"] = df[list(pred_cols.values())].sum(axis=1)
vote_summary = (
    df.groupby("model_votes")
    .agg(
        shots=("shot_id", "size"),
        audited_issues=("known_issue", "sum"),
        issue_rate=("known_issue", "mean"),
    )
    .round(3)
)
display(vote_summary)
display(
    df.nlargest(10, "model_votes")[
        ["shot_id", "model_votes", *columns, "known_issue"]
    ].round(3)
)
[OUT: 13]
shots audited_issues issue_rate
model_votes
0 437 0 0.000
1 421 1 0.002
2 20 2 0.100
3 16 4 0.250
4 4 1 0.250
5 7 3 0.429
6 17 14 0.824
7 18 15 0.833
shot_id model_votes mold_temp_c vibration_mm_s heater_current_a oil_pressure_mpa known_issue
126 S0127 7 220.592 3.633 37.847 11.397 0
157 S0158 7 211.063 2.070 38.227 10.885 0
478 S0479 7 204.327 4.137 36.266 9.103 0
900 S0901 7 213.905 6.052 37.836 8.578 1
901 S0902 7 211.210 6.057 34.612 8.425 1
903 S0904 7 209.262 5.785 32.073 8.304 1
910 S0911 7 206.332 5.110 36.330 9.347 1
911 S0912 7 210.178 5.224 30.045 10.242 1
912 S0913 7 215.102 5.515 33.103 10.246 1
916 S0917 7 221.522 2.150 22.260 8.207 1

Reading the results

Within this dataset, Elliptic Envelope has the highest F1 at 0.897. DBSCAN labels 503 of 940 observations as noise: it detects all 40 injected points but creates a large inspection workload. These are comparisons on the data used for fitting, so validate candidate models on a separate period before deployment.

No.060: Using anomaly scores to adjust thresholds

Practical purpose

Thresholds are not determined by statistics alone; they reflect the number of inspections you can inspect in a day and the losses missed in a day. Lowering the value increases Recall but also increases false positives; raising it reduces inspection load but increases missed detections.

Analysis and modeling

For example, let’s set the cost of confirming one false positive case at 2,000 yen, and the expected loss per missed case requiring confirmation at 50,000 yen.

C(t)=2,000 FP(t)+50,000 FN(t)C(t)=2{,}000\,FP(t)+50{,}000\,FN(t)

This is an explanatory assumption. During production, we estimate stoppage losses, defect outflows, maintenance workload, and safety impacts across departments. Also, the threshold values optimized with evaluation labels are not used directly in production but are verified during separate periods.

Check with Python

[IN: 14]
score = df["score_iso"]
quantiles = np.arange(0.85, 0.996, 0.005)
threshold_rows = []
for q in quantiles:
    threshold = score.quantile(q)
    pred = (score >= threshold).astype(int)
    fp = int(((pred == 1) & (df["known_issue"] == 0)).sum())
    fn = int(((pred == 0) & (df["known_issue"] == 1)).sum())
    threshold_rows.append(
        {
            "quantile": q,
            "threshold": threshold,
            "alerts": int(pred.sum()),
            "precision": precision_score(df["known_issue"], pred, zero_division=0),
            "recall": recall_score(df["known_issue"], pred, zero_division=0),
            "expected_cost_yen": 2_000 * fp + 50_000 * fn,
        }
    )
threshold_table = pd.DataFrame(threshold_rows)
best = threshold_table.loc[threshold_table["expected_cost_yen"].idxmin()]
display(threshold_table.sort_values("expected_cost_yen").head(8).round(3))
print(f"Lowest Cost Quantile: {best['quantile']:.3f}")
print(
    f"Number of alerts: {int(best['alerts'])} / Expected Costs: {best['expected_cost_yen']:,.0f}jpy"
)

fig, ax1 = plt.subplots(figsize=(9, 4.8))
ax1.plot(
    threshold_table["alerts"],
    threshold_table["expected_cost_yen"],
    marker="o",
    label="Expected cost",
)
ax1.scatter(
    [best["alerts"]],
    [best["expected_cost_yen"]],
    color="tab:red",
    s=80,
    zorder=3,
    label="Minimum cost",
)
ax1.set_title("Threshold trade-off: workload and expected cost")
ax1.set_xlabel("Number of alerts")
ax1.set_ylabel("Expected cost [JPY]")
ax1.grid(True, alpha=0.3)
ax1.legend()
fig.tight_layout()
plt.show()
[OUT: 14]
quantile threshold alerts precision recall expected_cost_yen
4 0.870 0.507 123 0.317 0.975 218000
3 0.865 0.506 127 0.307 0.975 226000
2 0.860 0.504 132 0.295 0.975 236000
1 0.855 0.503 137 0.285 0.975 246000
12 0.910 0.526 85 0.435 0.925 246000
0 0.850 0.501 141 0.277 0.975 254000
11 0.905 0.523 90 0.411 0.925 256000
5 0.875 0.508 118 0.322 0.950 260000
Lowest Cost Quantile: 0.870
Number of alerts: 123 / Expected Costs: 218,000jpy

No.060: Using anomaly scores to adjust thresholds

Reading the results

In this assumption, since missed losses outweigh the cost of false positives, the side that issues a certain number of alerts is chosen. However, if the minimum cost point exceeds the on-site inspection limit, priority is determined by two-stage judgment or by votes. Sensitivity analysis with varying cost factors is also presented at the decision-making meeting.

Practical points to check

  1. Defining decision-making before methods: The required Recall and explanation granularity changes depending on whether the output is used for stop decisions, inspection candidates, or quality checks.
  2. Abnormality rate is a prerequisite for the model: Just because you extracted 5% doesn’t mean the failure rate is 5%. Updates are made based on inspection capabilities and audit results.
  3. Using multiple methodologies for audits: Agreement between models does not establish ground truth, but it can help prioritize observations for initial review.
  4. Separation of standardization and operating conditions is important: When varieties and equipment are mixed, mere differences in conditions are detected as abnormalities.
  5. Show both scores and supporting values simultaneously: Present not only rankings but also which sensors have deviated from their usual range.

Requirements for deployment

  • Combine equipment ID, type, mold, startup/regular operation, and maintenance history to the analysis unit
  • Monitor missing sensor readings, sticking, and calibration misalignment at the front end of the model
  • Establish a timeline verification period to prevent the mixing of future information.
  • Record the “confirmation results, causes, and responses” for each alert and accumulate audit labels
  • Number of alerts, confirmation rate, missed detection, average detection time, and distribution changes as operational KPIs
  • Manage the model, standardizer, feature definitions, thresholds, and reasons for change
  • For safety-related equipment, the model is used as auxiliary information rather than replacing existing protection circuits.

Conclusion

In No.051 to No.060, multivariate anomalies were viewed from different perspectives: isolation, local density, boundary, covariance, near-neighborhood distance, and cluster density. In practice, it is important not to blindly trust the score of a single model, but to compare it with audit labels and adjust thresholds to match inspection capability and loss structure. For small-scale PoCs, starting with short cycles of candidate selection→ on-site inspection→ label accumulation→ and re-evaluation can bring you closer to equipment-specific “operational anomalies.”

Consulting for businesses

At Surikobo, we support everything from organizing manufacturing data, anomaly detection PoC, evaluation design, to alert design tailored to on-site operations. From stages such as “data available but few fault labels” or “high false positives making operation unsustainable,” challenges and decision-making can be organized.

📩 Contact Us: www.surikobo.co.jp/contact
Contact us to discuss your project.