Skip to main content

kMSR provides a selection of algorithms to solve the k-Min-Sum-Radii problem.

Project description

Build Status License: GPL v3 Supported Python version Stable Version

K-Min-Sum-Radii

kMSR provides various implementations to solve the k-Min-Sum-Radii problem. The k-Min-Sum-Radii problem is a clustering problem that aims to minimize the sum of the radii of the clusters. Given a set of points, the aim is to find $k$ balls such that the the sum of the radii of the balls is minimized. The package provides the following algorithms:

  • fpt-heuristic: The algorithm is an heuristic version of what is described in this paper. In practice, this works well for clusters that are not too separated. The algorithm uses the parameters epsilon, n_u, and num_test_radii to control the trade-off between the quality of the solution and the runtime. Increase n_u for a more accurate solution, which will however require more computation time.
  • heuristic: The algorithm is a simple heuristic that explores all possible combinations for the first cluster, and then selects the remaining centers as the points farthest from the radius of the first cluster. This algorithm works well in practice but it is not practical for large datasets.
  • gonzalez: This is the standard Gonzalez algorithm for $k$-center.
  • kmeans: This is the k-means++ algorithm for $k$-means.

Although the last two algorithms are not specifically designed for the k-Min-Sum-Radii problem, they serve as useful baselines for comparing the performance of other algorithms. Additionally, an optimization has been integrated into all algorithms: intersecting balls are merged at the end, resulting in a more cost-effective solution.

For computing the minimum enclosing circle of a set of points we use the miniball header for C++. Their code has not been changed.

You can try kMSR out on our Clustering Toolkit!

Installation

We highly recommend to install OpenMP. Parts of the code are parallelized and will be much faster. However, on Windows and MacOS the code also works without OpenMP. Nonetheless, the code was written for Linux and will achieve the best performance there.

On Linux, you can use the following command:

# Alpine
sudo apk add openmp-dev libgomp
# Ubuntu
sudo apt-get install libomp-dev libgomp1
# Debian
sudo apt-get install gcc libomp-dev libomp5 libgomp1
# ArchLinux
sudo pacman -S openmp
ln -s libomp.so libomp.so.5

We have tested this on docker, so it might be a little different on your system.

On MacOS, you can use the following command:

 brew install llvm libomp

However, it might be that MacOS does not find the installed library. In build_extension.py, the paths are set manually. If it does not work for you, please clone the repository and run poetry build. You will see a message in red if your OpenMP is not found.

Then, you can install the package via pip:

pip install kmsr

Example

from kmsr import KMSR
from kmsr.plot import plot_multiple_results
from sklearn.datasets import make_blobs
from time import time

points, ground_truth = make_blobs(
    n_samples=100,
    n_features=2,
    centers=2,
    cluster_std=0.6,
    shuffle=True,
    random_state=42,
)

labels = []
centers = []
radii = []
titles = []
for algo in ["FPT-heuristic", "Heuristic", "Gonzalez", "KMeans"]:
    kmsr = KMSR(
        n_clusters=5,
        algorithm=algo,
        epsilon=0.5,
        n_u=10000,
        n_test_radii=10,
        random_state=42,
    )
    start = time()
    kmsr.fit(points)
    end = time() - start
    labels.append(kmsr.labels_)
    centers.append(kmsr.cluster_centers_)
    radii.append(kmsr.cluster_radii_)
    titles.append(f"{algo}: {sum(kmsr.cluster_radii_):.3f}, Time: {end:.3f}s")

plot_multiple_results(
    points,
    clusters=labels,
    centers=centers,
    radii=radii,
    title=titles,
)

Comparison of the Different Methods for kMSR

Development

Install poetry

curl -sSL https://install.python-poetry.org | python3 -

Install clang

sudo apt-get install clang

Set clang variables

export CXX=/usr/bin/clang++
export CC=/usr/bin/clang

Install the package

poetry install

If the installation does not work and you do not see the C++ output, you can build the package to see the stack trace

poetry build

Run the tests

poetry run python -m unittest discover tests -v

Citation

If you use this code, please cite the following bachelor thesis:

N. Lenßen, "Experimentelle Analyse von Min-Sum-Radii Approximationsalgorithmen". Bachelorarbeit, Heinrich-Heine-Universität Düsseldorf, 2024.

Moreover, depending on the selection of the algorithm parameter, you should also cite the following paper for algorithm='fpt-heuristic':

L. Drexler, A. Hennes, A. Lahiri, M. Schmidt, and J. Wargalla, "Approximating Fair K-Min-Sum-Radii in Euclidean Space," in Lecture notes in computer science, 2023, pp. 119–133. doi: 10.1007/978-3-031-49815-2_9.

the following paper for algorithm='gonzalez':

T. F. Gonzalez, "Clustering to minimize the maximum intercluster distance," Theoretical Computer Science, vol. 38, pp. 293–306, Jan. 1985, doi: 10.1016/0304-3975(85)90224-5.

and the following paper for algorithm='kmeans':

D. Arthur and S. Vassilvitskii, "k-means++: the advantages of careful seeding," Symposium on Discrete Algorithms, pp. 1027–1035, Jan. 2007, doi: 10.5555/1283383.1283494.

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

kmsr-0.1.3.tar.gz (393.8 kB view details)

Uploaded Source

Built Distribution

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

kmsr-0.1.3-cp310-cp310-manylinux_2_39_x86_64.whl (731.4 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.39+ x86-64

File details

Details for the file kmsr-0.1.3.tar.gz.

File metadata

  • Download URL: kmsr-0.1.3.tar.gz
  • Upload date:
  • Size: 393.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.18 Linux/6.11.0-1018-azure

File hashes

Hashes for kmsr-0.1.3.tar.gz
Algorithm Hash digest
SHA256 607663f60c5c4b5259d41f5d5c62c9a14b7842c2252a9c9aa795f5ed763ca416
MD5 2afdee76fcbd6196026668a48f5b0974
BLAKE2b-256 149384aed3e27bec27bb47202d7d65d1ed07cec8b7ee464afb2f0e091fa7ab89

See more details on using hashes here.

File details

Details for the file kmsr-0.1.3-cp310-cp310-manylinux_2_39_x86_64.whl.

File metadata

  • Download URL: kmsr-0.1.3-cp310-cp310-manylinux_2_39_x86_64.whl
  • Upload date:
  • Size: 731.4 kB
  • Tags: CPython 3.10, manylinux: glibc 2.39+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.18 Linux/6.11.0-1018-azure

File hashes

Hashes for kmsr-0.1.3-cp310-cp310-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 fd214835ddddf3adef722aff90738784537a44e205531a80e71aca381fca214b
MD5 f4e877010816870f2b273e5e9dfbfc75
BLAKE2b-256 8716a7d94039113ab43cd3ad041485ee9b3b071903f1718fa138c79e62ac7c6d

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