k-Medoids clustering with the FasterPAM algorithm
Project description
k-Medoids Clustering in Python with FasterPAM
This python package implements k-medoids clustering with PAM. It can be used with arbitrary dissimilarites, as it requires a dissimilarity matrix as input.
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.
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
Pre-built packages are on PyPi https://pypi.org/project/kmedoids/ and can be installed with pip install kmedoids
.
Compilation from source
You need to have Rust and Python 3 installed.
Installation uses maturin for compiling and installing Rust extensions. Maturin is best used within a Python virtual environment.
# activate your desired virtual environment first
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.
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
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 use metric="euclidean"
.
import kmedoids
km = kmedoids.KMedoids(5, method='fasterpam')
c = km.fit(distmatrix)
print("Loss is:", c.inertia_)
MNIST (10k samples)
import kmedoids
import 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)
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)
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)
Note that the k-means-like algorithm for k-medoids tends to find much worse solutions.
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/.
FAQ: Why GPL and not Apache/MIT/BSD?
Because copyleft software like Linux is what built the open-source community.
Tit for tat: you get to use my code, I get to use your code.
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 Distribution
Built Distributions
Hashes for kmedoids-0.3.0-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb66819a73b5b4441657f90118e42c68e82c40e5192db4df617973a07571621a |
|
MD5 | b5b049afb219afd5ceb9674ca3479af9 |
|
BLAKE2b-256 | af995f9e028d8c134ea8d11fd0cc8876387e6732888107bfaf78e9a31f54d92e |
Hashes for kmedoids-0.3.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6629cf6e2748eff00331435461c9f87a20e8a470317bb5767efa504440acb541 |
|
MD5 | 9151509e78f3685ef6f76a25e5c1b576 |
|
BLAKE2b-256 | 6cbfcf41aba477bdeb6bf0e4f0323824a7213177fb8c64ab48577db5180a8151 |
Hashes for kmedoids-0.3.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 014fd70f99e7b1f9320e4dfdc533919150915a7be99f4f35cc95d14c84d4d435 |
|
MD5 | c3d6a8c3d8aa74d797f415286f98c4dd |
|
BLAKE2b-256 | 0a19efc7f894ef7f5eda1f5a76fcf35354094c3720fe2a4088ccce33b0fd54e4 |
Hashes for kmedoids-0.3.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00838b57ac1a318fb5499ad2ea049545f951f2369a16edd5c45a0ac3314c9ad6 |
|
MD5 | 5095e5e64fb5a91968aa5e7314bc24e8 |
|
BLAKE2b-256 | 8d99f0e0c618c60198ffaec9b4b1220e6c296f10b58687fae2c8c4ede9a95202 |
Hashes for kmedoids-0.3.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c530fccb906a033530f3a5106f53caf17943ad0db3eca6393d70a3fa91df40af |
|
MD5 | 6b2468104e0b79b9ed96ff3ddd14a82c |
|
BLAKE2b-256 | b22a4e304bbc37e6609fb2ea9520ac12fc3b8def69f1eca27b9cf69ca4558e97 |