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.05 means 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:

Processing steps:

  1. Read the input vibration value.

  2. Scale the value using the same scaler as the training data.

  3. 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)

This optional endpoint allows you to retrain the model using new samples, for example from live machine data.

Example request:

What happens internally:

  1. The data is reshaped and scaled again.

  2. A new Isolation Forest model is trained.

  3. The global scaler and model variables 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.0 makes it accessible on your local network.

  • Ideal for integration with Node-RED, PLCs, or industrial dashboards.


Summary

  • Flask provides the API endpoints (/predict and /retrain).

  • PyOD’s Isolation Forest detects anomalies in vibration values.

  • StandardScaler normalizes the data for better model performance.

  • /predict checks if a vibration value is normal or abnormal.

  • /retrain lets 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?