k-Medoids Clustering in Python with FasterPAM
Project description
k-Medoids Clustering in Python with FasterPAM
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
respectivelyconda 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)
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for kmedoids-0.5.2-cp312-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2037a8765de45b07219700afeb283cb96d8635a93bccad7ba399a034fa4f5d9a |
|
MD5 | 56397d49ef867fb9749be938b15c016f |
|
BLAKE2b-256 | a8edd78eb48b33457538e8845f1fb1065ced8f13515b2033bdab74a52f00274d |
Hashes for kmedoids-0.5.2-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a10b720114057886632452499808b912313acb25ba702344672b5b43e66d0ec1 |
|
MD5 | b164bf5d62880a801e53535e1178226f |
|
BLAKE2b-256 | eeaf41c23ddefec79618725342e1eefcc7b2a45a7b39ee70ecff7183576895d0 |
Hashes for kmedoids-0.5.2-cp312-cp312-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf97552b000a3f96e942594c6c9ecbc015ca55f780c06ec83382a59eec412f9e |
|
MD5 | acc69767a53aabf22e97f75e5201dce7 |
|
BLAKE2b-256 | d4de3850597003c08b6d7489844f215c801a872f91c21927a849d042c3d080e8 |
Hashes for kmedoids-0.5.2-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 890f3c272a243fca2d0a7446fc2451d404c53df156468c03b5424f2e9f1d9d2e |
|
MD5 | 6f72adaf12b45ac83bd59f89c58f5ea7 |
|
BLAKE2b-256 | 380e661dc553ffae837c7431210c0a8eaec883496b293c0dde35b34093102a11 |
Hashes for kmedoids-0.5.2-cp312-cp312-manylinux_2_28_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 828d84a02c8afb63f05b537cad01c9b3884d28fb1f3d8aeea880317641c8cd73 |
|
MD5 | c46e6e39eb1be51b54d99ce95453d326 |
|
BLAKE2b-256 | 12326595204945f844403d5fcb074addb0076fe349b45b5ef2890ea3059e25b9 |
Hashes for kmedoids-0.5.2-cp312-cp312-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | da10babd4b25c53942bba9a90b1ca2e35923e482806b4bc040da56ca2f01c1ec |
|
MD5 | 705174344eac9afa0eedfc1bf9f5895e |
|
BLAKE2b-256 | 45ed625ea6f18458be03a35697b1dbcd30309fb1dcdc4b122f63fd1fb7133cc0 |
Hashes for kmedoids-0.5.2-cp311-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d098ef8497e4c93454ee5c767e09a416407af465c37f667feecaac741a97dbb |
|
MD5 | 377bc7c633eb416867880701cfc831d8 |
|
BLAKE2b-256 | ce51f2c537391dc204b6ce6d8f1efb3b3e00913aa3288ce7cd1af6c863ddb3cd |
Hashes for kmedoids-0.5.2-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ddd708843b09b200f389df4749157d6b897cf45f75ed875133c16c4a5ccf30dd |
|
MD5 | 1f4aba839337fe76ef9beb9f1bc40627 |
|
BLAKE2b-256 | 96eab0decd98b37e9057c7a5f4464f482a2ab444f583e91e47d5125ac17e863c |
Hashes for kmedoids-0.5.2-cp311-cp311-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8cf665fcab14f3f3b98a22ad03b8010953f39fcd5335e2ffcca3773c2ac3f702 |
|
MD5 | f1e88b8157cf21598fab8e82e99ae810 |
|
BLAKE2b-256 | 2395cc6b49d0da86f60694f1bc640491133ef59128451869695319ee2f0dbad6 |
Hashes for kmedoids-0.5.2-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a8fd269f5fe5fdd3b714d11181ed08c5a05b3589939afcd69699fbf27ef39d9 |
|
MD5 | 4b7fd1f20e3bc2487b47b1c35ed6689c |
|
BLAKE2b-256 | 9c83f0cb981bdd88e0b5bb74feac5b8414571a3e9730e7130d3257f13a24cb18 |
Hashes for kmedoids-0.5.2-cp311-cp311-manylinux_2_28_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94754ae8b6cb38482d1cf679cd01def4c07f4e6c3add8107d6726215544d62f0 |
|
MD5 | 37a0e8d4b8833f8f7022be57431cafb0 |
|
BLAKE2b-256 | 87ddbab4c2cd8c11588233998c23b3237cc49c3c17018fce6f49f7e37b742159 |
Hashes for kmedoids-0.5.2-cp311-cp311-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d5fd7d2cf9fbe18ad54f8860d271c0eee118d2680273dc63ee83279c06c2ec3 |
|
MD5 | ec826fc3f96d2ed96342ab0ff382dd07 |
|
BLAKE2b-256 | d07a7eabc0f77391198170ff008b542a3c96d85e334f74166e7a1c7d35b60327 |
Hashes for kmedoids-0.5.2-cp310-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c7657a309847dd1138e4e2fa10ed8f481bf82265269c2235f1f5fe9650c7aec |
|
MD5 | 476523e5ea7aead21b027398909b913d |
|
BLAKE2b-256 | c0e3bde9c250f7a76606c1da5e217f02de2d86ef748556dfa29c7e6729c72e4a |
Hashes for kmedoids-0.5.2-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32e1325fca5b0e2c44b755f73fc3eefab8c4ffb2a2cb03c4ca84a878599ef0b1 |
|
MD5 | bb5fa801a19a291b4024090be8badd30 |
|
BLAKE2b-256 | eb3016b420af97350df6f57779da9f52e4c5a730af2af2fcca0c8c75397d0528 |
Hashes for kmedoids-0.5.2-cp310-cp310-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ea47d7a1b7949f188c5abea53615e3e83f10222c5e35f57402bf793ecbbc52f |
|
MD5 | 673b868a970dda5353179be8788d157d |
|
BLAKE2b-256 | f3e413571e93401cab9752cd7396629b3717a5bf21cc9405462429e66a01fc93 |
Hashes for kmedoids-0.5.2-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b040787199ee9d69822530a122fb972f81ba3e18f350d5e81a29c4d25427b6ea |
|
MD5 | 55e4f779fb205460335eef173d9e4ca3 |
|
BLAKE2b-256 | 203b793c5307b9b6d9e7d8427d8ccfd3672249c657623ceb6f27f44bd6905891 |
Hashes for kmedoids-0.5.2-cp310-cp310-manylinux_2_28_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49a338150e8e37019f51d839ce8c0c5b4db848a6967c275a1df5a288c6f22d9b |
|
MD5 | 99a3c4208442a9f304f7e177b5f78422 |
|
BLAKE2b-256 | a0bdfba2f0d66362e27f308082f2d0a1686abb1fe44023bb8d9c478f7155bd87 |
Hashes for kmedoids-0.5.2-cp310-cp310-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36071107572045707f34cb17aae2614c7e7ca042c4472f0f0541739b183e50dc |
|
MD5 | 38fe4ff5358a7d8111c0de994cd8418d |
|
BLAKE2b-256 | a15f5cc74813adf0eb6be469454f0d53518c498cb91d1cf6857b9346d5d1dcd9 |
Hashes for kmedoids-0.5.2-cp39-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | af4bc03bedd37c1d49d8342353d7710d9d7cb82e166251523cc628f35b02ac92 |
|
MD5 | bfb0408ccc1fd0f0bd88290fbdf3dd43 |
|
BLAKE2b-256 | 68cbb46807842ecbf8db976626fe31adff16de0361031f1e1d9f0183f2d90a0e |
Hashes for kmedoids-0.5.2-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 782cc0f0bcac2a9e562fe728d31ad26fe9470e3b0b390ffdbca787f5f51ba57a |
|
MD5 | d6ec5f8fdf3148ee04084477c84fcf23 |
|
BLAKE2b-256 | 993f71c0beb616380a912011dcbe5248340377c0f2ed7048babc033b9d7804b2 |
Hashes for kmedoids-0.5.2-cp39-cp39-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22b34e535e212040f88a0e1b559435d506423e35b2e2896f52286cda381e2b6b |
|
MD5 | e7d6b5a8f798a3d89f616d1534d360dc |
|
BLAKE2b-256 | ca295a631ec51befd26817207fcb341512d5edf4b30d617e6866e9f27f857bef |
Hashes for kmedoids-0.5.2-cp39-cp39-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 001df761702d3306b786a3ad79cd3e44a0777bcb197fe90ce54b1f59be0fc9f2 |
|
MD5 | 3a6e63f041c45a2cf9aeebf16dc9c996 |
|
BLAKE2b-256 | f5f93596c47c417891d882521cf0ad2c95344f2684972f5000eafd22ec719f71 |
Hashes for kmedoids-0.5.2-cp39-cp39-manylinux_2_28_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca607836b7a3f23ac85aac6ac50301ffa3a9a7a5364fed273f04adcae8750acf |
|
MD5 | 02f867119c71e5c39f979ad979029325 |
|
BLAKE2b-256 | 2930df734b134503c6b71199039be2dd27033066eabcb6340d3ca01a3ea8def7 |
Hashes for kmedoids-0.5.2-cp39-cp39-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b130677de20b3e54fd02ee3034e98efa9a9a181236598250c722690812e7c685 |
|
MD5 | 026c7eb08394d79e4d2496c9be6b8251 |
|
BLAKE2b-256 | 783b2e163ba8a898f06e24b0b56a1e6f68f7f819a5e9d2a62c51b95ed194df58 |
Hashes for kmedoids-0.5.2-cp38-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30af53d11365b01b2e59d28da02ef047cfad68a800dca0f5d75f7ba758b0b636 |
|
MD5 | e5d971544769dededb9bb4a9567dd67d |
|
BLAKE2b-256 | d060f9c2a6db2ff7a1c9e27d264b05793be21eb46912546f230ee03a07c868d2 |
Hashes for kmedoids-0.5.2-cp38-cp38-musllinux_1_2_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38a12f06e23a02af3b9540d774940f5941756415b9a241c81cfe194b6fed1e65 |
|
MD5 | a2dc6937f15d1c4208ea3ea03681b8b8 |
|
BLAKE2b-256 | 2eeb5aa8023e613d4ad069df6ccb613f16382e51f12be0c5d99cf77e1e033a4e |
Hashes for kmedoids-0.5.2-cp38-cp38-musllinux_1_2_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2823f1195cb5eed011d6b09e25a539cd3c156e1e9d32f74b124af58fdccbef9c |
|
MD5 | 437d341d630ce1791b028509970b482b |
|
BLAKE2b-256 | 165d7a56e610a5a1185225c38b93de7ddb67aa895bb7e888de87b132aebf2f4a |
Hashes for kmedoids-0.5.2-cp38-cp38-manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16407ed2726084307b0fc028bc19668fb36689a80aa61a8903e08740a8c0438c |
|
MD5 | 262bfcf94f0fd4c9680fc171f00bf356 |
|
BLAKE2b-256 | 6fcb43511e7812f4c713295e815b0ceee226630ab14d230d24eb7a40fbc1ceb5 |
Hashes for kmedoids-0.5.2-cp38-cp38-manylinux_2_28_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb928555b46eefc9c87bd8e51a8462bbe0967c4480279b0d55a7ebce747c974e |
|
MD5 | 524ac624ff328502c7abd9e9610ac20f |
|
BLAKE2b-256 | 6d0713fc45444f9ab528e58a1bb3e4728ac14c0b0cba7a151bc853cb269e91aa |
Hashes for kmedoids-0.5.2-cp38-cp38-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ce62eebb9d3cb53b17e7789492d129e136c77ea2a7da4f5f0a2781c52e86302 |
|
MD5 | 8c9c51b9b47ab5ebf93bf3fa8743bbcf |
|
BLAKE2b-256 | 0666e9013f18a2238002cb0115edbaaac7427431413c6af73bc1f25ee1cd7ebb |