Anomaly detection Code Explanation
Overview of the Anomaly Detection API (Flask + PyOD)
This page explains how the provided Python script works. The application uses Flask to expose an HTTP API and PyODβs Isolation Forest model to detect anomalies in vibration data. It is designed for learners, developers, and IIoT practitioners who want to integrate machine learning into their systems (PLC, Node-RED, edge devices, etc.).
1. Model Training (Normal Vibration Simulation)
The script begins by simulating βnormalβ vibration data to train the machine learning model.
normal_values = np.random.uniform(0.0, 5.0, 500)
normal_values = normal_values.reshape(-1, 1)This generates 500 vibration samples between 0 and 5 mm/s.
These represent healthy/normal machine behaviour.
Scaling the Data
scaler = StandardScaler()
normal_scaled = scaler.fit_transform(normal_values)The data is scaled using StandardScaler, which helps the model handle different ranges of vibration input by normalizing values to have:
mean = 0
standard deviation = 1
Training the Isolation Forest Model
model = IForest(contamination=0.05)
model.fit(normal_scaled)Isolation Forest is an unsupervised anomaly detection algorithm.
contamination=0.05means we expect approximately 5% of the data to be anomalies.This model learns what βnormalβ vibration looks like.
2. Prediction API Endpoint (/predict)
/predict)This endpoint accepts a POST request with a vibration value and returns whether it is normal or anomalous.
Example request:
Processing steps:
Read the input vibration value.
Scale the value using the same scaler as the training data.
Run the Isolation Forest model to compute:
prediction (0 = normal, 1 = anomaly)
anomaly score
Example returned JSON:
This makes it easy to connect the API to a PLC, Node-RED dashboard, or MQTT workflow.
3. Retraining Endpoint (/retrain)
/retrain)This optional endpoint allows you to retrain the model using new samples, for example from live machine data.
Example request:
What happens internally:
The data is reshaped and scaled again.
A new Isolation Forest model is trained.
The global
scalerandmodelvariables are updated.
This feature is useful when:
Machine behaviour changes over time
You want to build a personalized model based on real data
You need continuous improvement of detection accuracy
Response example:
4. Running the Flask Server
The server runs on port 5000.
0.0.0.0makes it accessible on your local network.Ideal for integration with Node-RED, PLCs, or industrial dashboards.
Summary
Flask provides the API endpoints (
/predictand/retrain).PyODβs Isolation Forest detects anomalies in vibration values.
StandardScaler normalizes the data for better model performance.
/predictchecks if a vibration value is normal or abnormal./retrainlets you update the model using new machine data.This setup is ideal for IIoT, predictive maintenance, and real-time monitoring.
Last updated
Was this helpful?