Skip to main content

ClustAnom is a Scikit-Learn compatible, clustering based anomaly detection library.

Project description

ClustAnom - Clustering Based Anomaly Detection

ClustAnom is a scikit-learn compatible anomaly detection framework that utilizes user-defined clustering algorithms. It determines anomalies based on the distance of data points from their assigned cluster centers. This flexible approach allows integration with various clustering methods to detect outliers effectively.

🔍 Features

  • Supports user-defined clustering models.
  • Measures anomaly scores based on distance from cluster centroids.
  • Fully compatible with scikit-learn pipelines.
  • Easy integration with existing machine learning workflows.

🚀 Supported Clustering Algorithms

ClustAnom supports clustering models that implement fit, predict or fit_predict methods:

  • AffinityPropagation
  • AgglomerativeClustering
  • Birch
  • DBSCAN
  • KMeans
  • MiniBatchKMeans
  • MeanShift
  • OPTICS
  • SpectralClustering
  • GaussianMixture
  • BayesianGaussianMixture

❌ Unsupported Clustering Algorithms

Explicitly unsupported clustering algorithms include, but are not limited to:

  • FeatureAgglomeration
  • SpectralBiclustering
  • SpectralCoclustering

⚠️ Warnings

Clusterers which return anomaly labels (e.g. DBSCAN) will cause a discrepancy between fit and score_samples where in score_samples the anomaly score for already anomalous samples will be np.inf or self.scaler_.data_max_ based on self.scale_scores. Additionally, clusterers with no predict require double fitting in both fit and score_samples or predict which may cause additional discrepancies.

📦 Installation

You can install ClustAnom via PyPI:

pip install clustanom

🛠️ Usage Example

from clustanom.detector import ClusterAnomalyDetector
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs

# Sample data
X, _ = make_blobs(n_samples=1000, centers=3, random_state=42)

# Define clusterer
clusterer = KMeans(n_clusters=3, random_state=42)

# Initialize cluster anomaly detector
anomaly_detector = ClusterAnomalyDetector(clusterer)

# Fit, predict and score
anomaly_detector.fit(X)
predictions = anomaly_detector.predict(X)
anomaly_scores = anomaly_detector.score_samples(X)
print("Anomaly Scores:", anomaly_scores)

⚙️ Parameters

  • clusterer: Any scikit-learn compatible clustering model (e.g., KMeans, Birch, Gaussian Mixture Model).
  • contamination: The proportion of anomalies in the dataset, which determines the threshold for anomaly classification.
  • scale_scores: Whether to scale the distances using MinMax scaling for better interpretability (limits anomaly scores to [0, 1]).

⚙️ Methods

  • fit(X): Fits the clustering model and computes the anomaly detection threshold.
  • score_samples(X): Computes anomaly scores for each sample based on its distance to the nearest cluster centroid.
  • predict(X): Predicts whether each sample is an anomaly (-1) or normal (1) in accordance with scikit-learn conventions.

📜 License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

clustanom-0.1.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

clustanom-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file clustanom-0.1.0.tar.gz.

File metadata

  • Download URL: clustanom-0.1.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for clustanom-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8ffe8b1c5f353fd35eaf78c4079b368668d61ec4967be7be935f15af6bda179b
MD5 ee139d26e95b1e76161bc7908d56e278
BLAKE2b-256 372762a6e6fe9561dc9fa16b266c0d32c22e4a21243a2b19751d9f76ea14339d

See more details on using hashes here.

File details

Details for the file clustanom-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: clustanom-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for clustanom-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0d6e556e1ba980be30e7daa66a4697dbe68a9e28ab277946bb6eb900d50de62f
MD5 b1a0dc5744e7807c82558bce7b38299b
BLAKE2b-256 bbac2690f75e114fd83640bf2b2743887b2fd29dff0c9e3e60683cfe1162fa60

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page