1. Model Training (Normal Vibration Simulation)
The script begins by simulating “normal” vibration data to train the machine learning model.- This generates 500 vibration samples between 0 and 5 mm/s.
- These represent healthy/normal machine behaviour.
Scaling the Data
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
- 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)
This endpoint accepts a POST request with a vibration value and returns whether it is normal or anomalous.
Example request:
- 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
3. Retraining Endpoint (/retrain)
This optional endpoint allows you to retrain the model using new samples, for example from live machine data.
Example request:
- The data is reshaped and scaled again.
- A new Isolation Forest model is trained.
- The global
scalerandmodelvariables are updated.
- Machine behaviour changes over time
- You want to build a personalized model based on real data
- You need continuous improvement of detection accuracy
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.