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.0.1.tar.gz (5.1 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.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for clustanom-0.0.1.tar.gz
Algorithm Hash digest
SHA256 2dd82cd04ecf5789c7a79366e5177425807b2423e0591c1349deb06f38b003b7
MD5 04f2b89f73fb529a1a5d5f5843b206f0
BLAKE2b-256 3a1d58a8fa4c9578aacb391b1ada8a20bd0a6e4c7cc5f546ddfa770772cb1c81

See more details on using hashes here.

File details

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

File metadata

  • Download URL: clustanom-0.0.1-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.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 afa5373d74943f110759d0d7691adfed68008eaeeb6e66e698628f494b05b3b3
MD5 e5e5d5ac7b9ffe844d39c9dccd08e0ce
BLAKE2b-256 dca4ff771438491452e31716ebd8ef8c3e6d274cc5a8f08a436385f4ded5aa27

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