Skip to content
← All posts
Machine learningAnomaly detection

Detecting the Odd One Out: A Guide to One-Class Classification

Jan 25, 2025 · 6 min read

Originally published on Medium

Imagine a security analyst monitoring a high-value corporate network. One day, a software process starts behaving unpredictably. It mimics normal operations but deviates just enough to raise suspicion. No malware signature matches, and traditional classifiers fail to detect it. This is the everyday challenge of anomaly detection: spotting the "odd one out" in a sea of normal data. This is where One-Class Classifiers (OCCs) come in. These models are trained to deeply understand normal behavior, so anything abnormal stands out like a sore thumb.

In this post, we'll explore what One-Class Classifiers are, how they work, when to use them, and where they're applied. From cybersecurity to healthcare and image analysis, OCCs are essential for identifying critical anomalies.

What is a One-Class Classifier?

A One-Class Classifier is a machine learning algorithm that decides whether a data point belongs to the same distribution as the training data (the normal class) or deviates from it (an anomaly). Unlike traditional classification models, which need examples of every category, an OCC learns only the characteristics of the "normal" class, which makes it well suited to finding anomalies.

How does a One-Class Classifier work?

During training, a One-Class Classifier learns the boundary of the normal data distribution. At inference time, it checks whether new data falls inside that boundary (normal) or outside it (anomalous). Popular OCC algorithms include:

One-Class SVM: hyperplanes and support vectors defining the decision boundary for anomaly detection.
One-Class SVM: hyperplanes and support vectors defining the decision boundary for anomaly detection.
  • One-Class SVM (Support Vector Machine): uses hyperplanes to separate normal data from potential anomalies.
Autoencoder architecture: compressing input images into a latent space and reconstructing them for anomaly detection.
Autoencoder architecture: compressing input images into a latent space and reconstructing them for anomaly detection.
  • Autoencoder: a neural network trained to reconstruct its input; a high reconstruction error signals a potential anomaly.
Isolation Forest: isolating outliers with fewer splits than regular data points.
Isolation Forest: isolating outliers with fewer splits than regular data points.
  • Isolation Forest: randomly splits the data into trees; anomalies tend to be isolated with fewer splits.

Why choose a One-Class Classifier?

  1. Limited anomalous data. In applications such as fraud detection or medical diagnosis, anomalous examples are scarce, which makes traditional classifiers ineffective.
  2. Anomalies keep changing. Anomalies are often unpredictable and diverse, so labeling all of them for training is impractical.
  3. Efficiency. OCC models focus on the normal class, which simplifies training and makes them suitable for real-time applications.

With OCCs in your toolkit, you can tackle anomaly detection across domains, from cybersecurity to predictive maintenance.

When should you use a One-Class Classifier?

One-Class Classifiers shine when anomalies are too rare or unpredictable to model directly. Common use cases:

A red node flags unusual activity in a high-value corporate network.
A red node flags unusual activity in a high-value corporate network.
  1. Cybersecurity. OCCs power anomaly detection systems that spot unusual network traffic, access patterns or file activity that deviates from a system's normal operational data and could indicate a breach.
  2. Fraud detection. Financial institutions train OCCs on normal transactions and flag the ones that deviate significantly as potential fraud. This matters because fraud techniques evolve constantly, and there's rarely an up-to-date example of every type.
An MRI scan with highlighted irregularities for early diagnosis.
An MRI scan with highlighted irregularities for early diagnosis.
  1. Healthcare diagnostics. Detecting abnormalities in medical images or patient data, where healthy cases are well documented but anomalies are scarce.
  2. Image analysis. Telling normal images apart from manipulated ones in computer vision tasks.

Is a One-Class Classifier right for your data?

An OCC is highly effective, but it isn't always the best fit. Here's how to tell whether it suits your data:

In an imbalanced dataset the majority class dominates, which makes rare anomalies in the minority class harder to detect.
In an imbalanced dataset the majority class dominates, which makes rare anomalies in the minority class harder to detect.
  1. Imbalanced datasets. The data is mostly "normal," with very few or no labeled anomalies. OCC models need only normal data to train, so large class imbalances don't hold them back. Example: fraud detection, where most transactions are legitimate and fraudulent ones are rare.
  2. Limited or no anomalous data. Anomalies are unavailable, hard to define, or too few to train a supervised model. Training only on normal data removes the need for labeled anomalies. Example: industrial equipment monitoring, where failures are rare and unpredictable.
  3. Dynamic or undefined anomalies. Anomalies don't follow a consistent pattern and change over time. Because OCCs detect deviations from normal behavior, they adapt to new and unseen anomalies. Example: network intrusion detection, where attackers constantly change their strategies.
  4. Continuous or real-time monitoring. Your application watches data streams for unusual behavior. OCC models are lightweight and efficient enough for real-time detection. Example: patient monitoring, where vitals must be analyzed in real time.
  5. Unlabeled or partially labeled data. Labeling anomalies is expensive and slow. OCCs skip that step and save time and resources. Example: image analysis for rare manufacturing defects.

If you answer "yes" to most of these questions, a One-Class Classifier is likely a strong candidate:

  • Is the dataset predominantly normal, with rare or undefined anomalies?
  • Do you have limited or no labeled anomalous data?
  • Are anomalies diverse or unpredictable?
  • Does your application need real-time or continuous anomaly detection?
  • Are the consequences of missing an anomaly significant?

How to implement a One-Class Classifier

Let's walk through an example that uses a One-Class SVM to detect anomalies.

Step 1: Prepare your data

Data preparation plays a vital role in OCC:

  • Train only on normal data, and exclude anomalies.
  • Normalize or scale features, for example with min-max scaling.
  • Split the data into training and test sets.

Step 2: Choose an algorithm

  • One-Class SVM: great for small to medium datasets with clear boundaries.
  • Isolation Forest: ideal for high-dimensional data or sparse anomalies.
  • Autoencoders: best for complex data such as images or sequences.

Step 3: Train the model

Here's an example of training a One-Class SVM for anomaly detection:

python
import numpy as np
from sklearn.svm import OneClassSVM
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt

# Normal training data
np.random.seed(42)
normal_data = 0.3 * np.random.randn(100, 2)

# Scale the data
scaler = StandardScaler()
normal_data_scaled = scaler.fit_transform(normal_data)

# Train the One-Class SVM
model = OneClassSVM(kernel="rbf", gamma=0.1, nu=0.05)
model.fit(normal_data_scaled)

# Predict on test data
test_data = scaler.transform([[0.1, 0.2], [-3, 3]])
predictions = model.predict(test_data)

# Visualize
plt.scatter(normal_data_scaled[:, 0], normal_data_scaled[:, 1], label="Normal data", alpha=0.7)
plt.scatter(test_data[:, 0], test_data[:, 1], c=["green" if p == 1 else "red" for p in predictions],
            label="Test data (green: normal, red: anomaly)", edgecolor="black", s=100)
plt.title("One-Class SVM example")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.legend()
plt.grid()
plt.show()
Output of the example: normal data in blue and a detected anomaly in red, in a 2D feature space.
Output of the example: normal data in blue and a detected anomaly in red, in a 2D feature space.

Step 4: Evaluate and deploy

  • Evaluate the model with precision, recall and F1-score, and tune hyperparameters such as kernel, gamma and nu.
  • Save the trained model with a library like Pickle or Joblib, then deploy it for real-time or batch anomaly detection.
  • Keep monitoring the model for data drift, and retrain it as patterns evolve.

Conclusion

Exploring One-Class Classifiers showed me how important they are to anomaly detection. They've proven instrumental across domains such as cybersecurity, healthcare and image analysis.

In my research, OCCs were critical for detecting anomalies in real-world scenarios. Training systems with One-Class SVMs and autoencoders deepened my understanding of OCCs and showed how well they handle dynamic, imbalanced datasets.

As anomaly detection grows in importance, OCCs remain a powerful and essential part of the machine learning toolbox.