Skip to main content

k-Medoids Clustering in Python with FasterPAM

Project description

k-Medoids Clustering in Python with FasterPAM

PyPI version Conda Version Conda Platforms

This python package implements k-medoids clustering with PAM and variants of clustering by direct optimization of the (Medoid) Silhouette. It can be used with arbitrary dissimilarites, as it requires a dissimilarity matrix as input.

This software package has been introduced in JOSS:

Erich Schubert and Lars Lenssen
Fast k-medoids Clustering in Rust and Python
Journal of Open Source Software 7(75), 4183
https://doi.org/10.21105/joss.04183 (open access)

For further details on the implemented algorithm FasterPAM, see:

Erich Schubert, Peter J. Rousseeuw
Fast and Eager k-Medoids Clustering:
O(k) Runtime Improvement of the PAM, CLARA, and CLARANS Algorithms
Information Systems (101), 2021, 101804
https://doi.org/10.1016/j.is.2021.101804 (open access)

an earlier (slower, and now obsolete) version was published as:

Erich Schubert, Peter J. Rousseeuw:
Faster k-Medoids Clustering: Improving the PAM, CLARA, and CLARANS Algorithms
In: 12th International Conference on Similarity Search and Applications (SISAP 2019), 171-187.
https://doi.org/10.1007/978-3-030-32047-8_16
Preprint: https://arxiv.org/abs/1810.05691

This is a port of the original Java code from ELKI to Rust. The Rust version is then wrapped for use with Python.

For further details on medoid Silhouette clustering with automatic cluster number selection (FasterMSC, DynMSC), see:

Lars Lenssen, Erich Schubert:
Medoid silhouette clustering with automatic cluster number selection
Information Systems (120), 2024, 102290
https://doi.org/10.1016/j.is.2023.102290
Preprint: https://arxiv.org/abs/2309.03751

the basic FasterMSC method was first published as:

Lars Lenssen, Erich Schubert:
Clustering by Direct Optimization of the Medoid Silhouette
In: 15th International Conference on Similarity Search and Applications (SISAP 2022)
https://doi.org/10.1007/978-3-031-17849-8_15

If you use this code in scientific work, please cite above papers. Thank you.

Documentation

Full python documentation is included, and available on python-kmedoids.readthedocs.io

Installation

Installation with pip or conda

Pre-built packages for many Linux, Windows, and OSX systems are available in PyPI and conda-forge can be installed with

  • pip install kmedoids respectively
  • conda install -c conda-forge kmedoids.

On uncommon architectures, you may need to first install Cargo (i.e., the Rust programming language) first, and a subsequent pip install kmedoids will try to compile the package for your CPU architecture and operating system.

Compilation from source

You need to have Python 3 installed.

Unless you already have Rust, install Rust/Cargo.

Installation uses maturin for compiling and installing the Rust extension. Maturin is best used within a Python virtual environment:

# activate your desired virtual environment first, then:
pip install maturin
git clone https://github.com/kno10/python-kmedoids.git
cd python-kmedoids
# build and install the package:
maturin develop --release

Integration test to validate the installation.

pip install numpy
python -m unittest discover tests

This procedure uses the latest git version from https://github.com/kno10/rust-kmedoids. If you want to use local modifications to the Rust code, you need to provide the source folder of the Rust module in Cargo.toml by setting the path= option of the kmedoids dependency.

Example

Given a distance matrix distmatrix, cluster into k = 5 clusters:

import kmedoids
c = kmedoids.fasterpam(distmatrix, 5)
print("Loss is:", c.loss)

Using the sklearn-compatible API

Note that KMedoids defaults to the "precomputed" metric, expecting a pairwise distance matrix. If you have sklearn installed, you can also use metric="euclidean" and other distances supported by sklearn.

import kmedoids
km = kmedoids.KMedoids(5, method='fasterpam')
c = km.fit(distmatrix)
print("Loss is:", c.inertia_)

MNIST (10k samples)

import kmedoids, numpy, time
from sklearn.datasets import fetch_openml
from sklearn.metrics.pairwise import euclidean_distances
X, _ = fetch_openml('mnist_784', version=1, return_X_y=True, as_frame=False)
X = X[:10000]
diss = euclidean_distances(X)
start = time.time()
fp = kmedoids.fasterpam(diss, 100)
print("FasterPAM took: %.2f ms" % ((time.time() - start)*1000))
print("Loss with FasterPAM:", fp.loss)
start = time.time()
pam = kmedoids.pam(diss, 100)
print("PAM took: %.2f ms" % ((time.time() - start)*1000))
print("Loss with PAM:", pam.loss)

Choose the optimal number of clusters

This package includes DynMSC, an algorithm that optimizes the Medoid Silhouette, and chooses the "optimal" number of clusters in a range of kmin..kmax. Beware that if you allow a too large kmax, the optimum result will likely have many one-elemental clusters. A too high kmax may mask more desirable results, hence it is recommended that you choose only 2-3 times the number of clusters you expect as maximum.

import kmedoids, numpy
from sklearn.datasets import fetch_openml
from sklearn.metrics.pairwise import euclidean_distances
X, _ = fetch_openml('mnist_784', version=1, return_X_y=True, as_frame=False)
X = X[:10000]
diss = euclidean_distances(X)
kmin, kmax = 10, 20
dm = kmedoids.dynmsc(diss, kmax, kmin)
print("Optimal number of clusters according to the Medoid Silhouette:", dm.bestk)
print("Medoid Silhouette over range of k:", dm.losses)
print("Range of k:", dm.rangek)

Full Colab notebook example.

Memory Requirements

Because the algorithms require a distance matrix as input, you need O(N²) memory to use these implementations. With single precision, this matrix needs 4·N² bytes, so a typical laptop with 8 GB of RAM could handle data sets of over 40.000 instances, but if your computation of the distance matrix incurs copying the matrix, only 30.000 or less may be feasible.

The majority of run time usually is the distance matrix computation, so it is recommended you only compute it once, then experiment with different algorithm settings. Avoid recomputing it repeatedly.

For larger data sets, it is recommended to only cluster a representative sample of the data. Usually, this will still yield sufficient result quality.

Implemented Algorithms

  • FasterPAM (Schubert and Rousseeuw, 2020, 2021)
  • FastPAM1 (Schubert and Rousseeuw, 2019, 2021)
  • PAM (Kaufman and Rousseeuw, 1987) with BUILD and SWAP
  • Alternating optimization (k-means-style algorithm)
  • Silhouette index for evaluation (Rousseeuw, 1987)
  • FasterMSC (Lenssen and Schubert, 2022)
  • FastMSC (Lenssen and Schubert, 2022)
  • DynMSC (Lenssen and Schubert, 2023)
  • PAMSIL (Van der Laan and Pollard, 2003)
  • PAMMEDSIL (Van der Laan and Pollard, 2003)
  • Medoid Silhouette index for evaluation (Van der Laan and Pollard, 2003)

Note that the k-means-like algorithm for k-medoids tends to find much worse solutions.

Contributing to python-kmedoids

Third-party contributions are welcome. Please use pull requests to submit patches.

Reporting issues

Please report errors as an issue within the repository's issue tracker.

Support requests

If you need help, please submit an issue within the repository's issue tracker.

License: GPL-3 or later

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

kmedoids-0.5.1-cp312-none-win_amd64.whl (367.7 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

kmedoids-0.5.1-cp312-cp312-manylinux_2_28_x86_64.whl (451.4 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

kmedoids-0.5.1-cp312-cp312-manylinux_2_28_aarch64.whl (418.4 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.28+ ARM64

kmedoids-0.5.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (451.1 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

kmedoids-0.5.1-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.whl (418.1 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

kmedoids-0.5.1-cp312-cp312-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (783.6 kB view hashes)

Uploaded CPython 3.12 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

kmedoids-0.5.1-cp311-none-win_amd64.whl (368.0 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

kmedoids-0.5.1-cp311-cp311-manylinux_2_28_x86_64.whl (454.0 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

kmedoids-0.5.1-cp311-cp311-manylinux_2_28_aarch64.whl (421.4 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.28+ ARM64

kmedoids-0.5.1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (453.6 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

kmedoids-0.5.1-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.whl (421.0 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

kmedoids-0.5.1-cp311-cp311-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (790.2 kB view hashes)

Uploaded CPython 3.11 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

kmedoids-0.5.1-cp310-none-win_amd64.whl (368.1 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

kmedoids-0.5.1-cp310-cp310-manylinux_2_28_x86_64.whl (454.6 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

kmedoids-0.5.1-cp310-cp310-manylinux_2_28_aarch64.whl (421.2 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.28+ ARM64

kmedoids-0.5.1-cp310-cp310-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (454.3 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

kmedoids-0.5.1-cp310-cp310-manylinux2014_aarch64.manylinux_2_17_aarch64.whl (420.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

kmedoids-0.5.1-cp310-cp310-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (789.7 kB view hashes)

Uploaded CPython 3.10 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

kmedoids-0.5.1-cp39-none-win_amd64.whl (368.4 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

kmedoids-0.5.1-cp39-cp39-manylinux_2_28_x86_64.whl (454.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

kmedoids-0.5.1-cp39-cp39-manylinux_2_28_aarch64.whl (421.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.28+ ARM64

kmedoids-0.5.1-cp39-cp39-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (454.2 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

kmedoids-0.5.1-cp39-cp39-manylinux2014_aarch64.manylinux_2_17_aarch64.whl (421.2 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

kmedoids-0.5.1-cp39-cp39-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (789.6 kB view hashes)

Uploaded CPython 3.9 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

kmedoids-0.5.1-cp38-none-win_amd64.whl (368.1 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

kmedoids-0.5.1-cp38-cp38-manylinux_2_28_x86_64.whl (454.5 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.28+ x86-64

kmedoids-0.5.1-cp38-cp38-manylinux_2_28_aarch64.whl (421.0 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.28+ ARM64

kmedoids-0.5.1-cp38-cp38-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (454.2 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

kmedoids-0.5.1-cp38-cp38-manylinux2014_aarch64.manylinux_2_17_aarch64.whl (420.7 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

kmedoids-0.5.1-cp38-cp38-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (789.8 kB view hashes)

Uploaded CPython 3.8 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

Supported by

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