Skip to main content

scikit-clarans

A scikit-learn compatible implementation of the CLARANS (Clustering Large Applications based on RANdomized Search) algorithm.

License DOI Python 3.9+ Docs Build Test Suite Quality Check PyPI version Open In Colab

CLARANS acts as a bridge between the high quality of PAM (Partition Around Medoids) and the speed required for large datasets. By using randomized search instead of exhaustive search, it finds high-quality medoids efficiently without exploring the entire graph of solutions.


Features

  • Scikit-Learn Native: Use it just like KMeans or DBSCAN. Drop-in compatibility for pipelines and cross-validation.
  • Scalable: Designed to handle datasets where standard PAM/k-medoids is too slow.
  • Flexible: Choose from multiple initialization strategies (k-medoids++, build, etc.) and distance metrics (euclidean, manhattan, cosine, etc.).

Installation

Install simply via pip:

pip install scikit-clarans

Or install from source:

pip install .

For development

pip install -e ".[dev]"

Quick Start

CLARANS

from clarans import CLARANS
from sklearn.datasets import make_blobs

# 1. Create dummy data
X, _ = make_blobs(n_samples=1000, centers=5, random_state=42)

# 2. Initialize CLARANS
#    - n_clusters: 5 clusters
#    - numlocal: 3 restarts for better quality
#    - init: 'k-medoids++' for smart starting points
clarans = CLARANS(n_clusters=5, numlocal=3, init='k-medoids++', random_state=42)

# 3. Fit
clarans.fit(X)

# 4. Results
print("Medoid Indices:", clarans.medoid_indices_)
print("Labels:", clarans.labels_)

FastCLARANS

FastCLARANS implements the faster variant from Schubert & Rousseeuw (2021). It evaluates swaps with all k medoids simultaneously using FastPAM1 delta formulas, exploring k edges of the search graph in the time CLARANS explores one:

from clarans import FastCLARANS

# FastCLARANS computes distances on-the-fly (memory efficient)
# and samples max(250, 2.5% of non-medoid points) per iteration
fast_model = FastCLARANS(n_clusters=5, numlocal=3, random_state=42)
fast_model.fit(X)

Key differences from CLARANS:

  • Samples only non-medoid candidates (not medoid-candidate pairs)
  • Evaluates swap with all k medoids at once (O(k) speedup per evaluation)
  • Memory efficient: O(n) instead of O(n²)

Examples

This repository includes a number of runnable examples in the examples/ folder showing common usage patterns and integrations. Run any example with:

python examples/01_quick_start.py

Documentation

For full API reference and usage guides, please see the Documentation.

Contributing

Contributions are welcome! Please check out CONTRIBUTING.md for guidelines.

Citation

If you use scikit-clarans in your research, please cite:

@software{scikit_clarans,
  author       = {Nguyen, Ngoc Thien},
  title        = {scikit-clarans: A Python Library for CLARANS Clustering},
  year         = {2026},
  publisher    = {Zenodo},
  doi          = {10.5281/zenodo.18366801},
  url          = {https://github.com/ThienNguyen3001/scikit-clarans}
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Download files

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

Source Distribution

scikit_clarans-0.2.2.tar.gz (33.8 kB view details)

Uploaded Source

Built Distribution

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

scikit_clarans-0.2.2-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file scikit_clarans-0.2.2.tar.gz.

File metadata

  • Download URL: scikit_clarans-0.2.2.tar.gz
  • Upload date:
  • Size: 33.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for scikit_clarans-0.2.2.tar.gz
Algorithm Hash digest
SHA256 0c3ea30dde76754cf71ffba1c7b476e042df090e768e657ad6898a592bb37350
MD5 72421f07863728fe0c66b661415329c8
BLAKE2b-256 8606d56cc76606ae3a0cd63c0631611d8ea0a22dba8f9ce5da7e7d9d9df5b31c

See more details on using hashes here.

Provenance

The following attestation bundles were made for scikit_clarans-0.2.2.tar.gz:

Publisher: pypi-publish.yml on ThienNguyen3001/scikit-clarans

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file scikit_clarans-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: scikit_clarans-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for scikit_clarans-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ab16d6903ee9583023946af94f736fe12a799e754e066f51047c61a8e4b04947
MD5 c777f82ba32c52691754f4bad3cd06cc
BLAKE2b-256 5f44052ceed1609f59bbadf982ea938e82014b9e04e2e7dc01edfd433d5e3234

See more details on using hashes here.

Provenance

The following attestation bundles were made for scikit_clarans-0.2.2-py3-none-any.whl:

Publisher: pypi-publish.yml on ThienNguyen3001/scikit-clarans

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.2.2 This release

2 files

0.2.1

2 files

0.2.0

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page