Skip to content
tkdyun.xyz

Imbalanced Anomaly Detector

SMOTE, cost-sensitive learning and gradient-boosted ensembles

Machine Learning

Year
2025
Role
Solo
Stack
Python · Scikit-learn · PyOD · Pandas
View on GitHub

Overview

When the number of normal instances significantly outweighs the abnormal ones, models become biased toward the majority class. Inspired by manufacturing anomaly detection, I conducted a personal project identifying low-quality wine by comparing anomaly detection techniques on a highly imbalanced dataset.

Dataset

The Wine Quality Dataset (Red Wine) was reframed as anomaly detection:

  • Normal Class (0): Wine quality score ≤ 6 — 1,382 instances
  • Abnormal Class (1): Wine quality score > 6 — 217 instances

This yields a roughly 85:15 class imbalance.

Class distribution

EDA

Variable distributionsPairwise distributions

Evaluation — Equal Error Rate

Accuracy is misleading with severe class imbalance — predicting everything as Normal yields 86%. Instead, we use the Equal Error Rate (EER): the point on the ROC curve where FAR (False Acceptance Rate) = FRR (False Rejection Rate). A lower EER indicates a more stable model.

Results

EER comparisonConfusion matrices
ModelEERResult
Weighted KNN0.1915Best
KNN0.2170Excellent
Isolation Forest0.4432Good
PCA (Autoencoder)0.4468Good
KMeans0.5092Average
One-Class SVM0.5314Average
ABOD0.6264Poor
Mixture of Gaussians0.7656Worst

Key Takeaways

  • Distance-Based Methods Won: KNN and Weighted KNN dominated. The local distance weighting of Weighted KNN pushed EER below 0.20.
  • Dedicated Outlier Detectors Struggled: One-Class SVM and ABOD performed averagely or poorly on this dataset.
  • Isolation Forest achieved a modest 0.4432, consistent with its reputation as a robust baseline.

Anomaly detection requires significant trial and error. Complex specialized models do not always outperform simpler distance-based models depending on the data distribution.

Search the site

Choose a destination, or type to search.