Support Vector Machines are very often used for classification tasks: but you may not know that they're so flexible they can be used for anomaly detection and novelty detection. Thanks to the micromlgen package, you can run One Class SVM on your Arduino microcontorller.

Novelty detection from sklearn documentation

What is anomaly / novelty detection useful for?

Detect noise or anomalies

As the name implies, anomaly detection can be used to monitor a stream of data and alert you when something unexpected happens.
Think of an Industrial IoT setup where you have a bunch of sensors monitoring the working state of a production plant: you want to know as soon as possible if something bad is gonna happen.

In this case, anomaly detection tells you if your machinery is acting in a different way from the normal state so you can take action.

Ignore irrelevant data

(This application was suggested from two of my readers)
Say you're developing a super simple word classification project: you want to distinguish door bell from fire alarm (as per one of the two readers).
So you train your SVM classifier and use micromlgen to run it on your Arduino microcontroller.

It works well, but we have a problem: you live in a noisy environment with many sounds, so not all of them will either be door bells or fire alarms. Since your classifier is binary, it has to classify all of the sounds as either A or B.

The solution will be novelty detection: before running the binary SVM, you run the OneClassSVM to filter known sounds (bell and alarm) from unknown ones (eg. dog barking).
If OneClassSVM predicts the sound as a novelty, you discard it since it's of no interest for you. If it predicts the sound as known, you run the binary SVM.

Want to learn more?

How to run anomaly / novelty detection on Arduino microcontroller via OneClassSVM

Porting a OneClassSVM from Python to plain C++ is as easy as a single command in the micromlgen package:

from sklearn.svm import OneClassSVM
from micromlgen import port

clf = OneClassSVM(kernel="rbf", nu=0.5, gamma=0.1)
clf.fit(X, y)
print(port(clf))
You will need micromlgen version 1.0.2 to port OneClassSVM. If you have an outdated version, please run pip install --upgrade micromlgen

If you read my previous posts about micromlgen and SVM the above snippet should be familiar: with the latest release, port is able to export either SVC, LinearSVC, OneClassSVC and RVC (Relevant Vector Machines) to object oriented C++.

Now you can embed the generated code in your Arduino sketch.

#include "OneClassSVM.h"

Eloquent::ML::Port::OneClassSVM clf;

void setup() {
  Serial.begin(115200);
  delay(2000);

  for (int i = 0; i < DATASET_SIZE; i++)
    clf.predict(X[i]);
}

void loop() {}

Troubleshooting

It can happen that when running micromlgen.port(clf) you get a TemplateNotFound error. To solve the problem, first of all uninstall micromlgen.

pip uninstall micromlgen

Then head to Github, download the package as zip and extract the micromlgen folder into your project.


I created an example sketch from a synthetic dataset for anomaly detection (copied from a scikit-learn example) you can run to get a feel of how it performs.

Go checkout the Github repo

Help the blow grow