ELSARA: EquaL-size SpectrAl clusteRing Algorithm
This is a modification of the spectral clustering algorithm that builds clusters balanced in the number of points. A detailed explanation of the model can be found in this Medium blog post.
Installation
Installation occurs through pip:
pip install elsara
Toy datasets
In the folder datasets we have provided you with a toy dataset
so you can run the clustering code right away.
- restaurants_in_amsterdam.csv: A table with locations of restaurants in the city of Amsterdam
- symmetric_distr_tr.npy: A file with the travel distance between the restaurants
You can find more specification on how to use these datasets in the project's blog post.
Examples
- example1.py: From a set of hyperparameters, you obtain clusters with sizes roughly equal to N /
nclusters - example2.py: From a range of cluster sizes, you obtain the clusters hyperparameters to run the clustering code.
Usage
Example 1: fixed hyperparameters
Provide nclusters, nneighbors, and equity_fraction directly. Each cluster will contain roughly N / nclusters points.
import pandas as pd
import numpy as np
from elsara import SpectralEqualSizeClustering, visualise_clusters
# coords is used only for visualization
coords = pd.read_csv("datasets/restaurants_in_amsterdam.csv")
dist_tr = np.load("datasets/symmetric_dist_tr.npy")
clustering = SpectralEqualSizeClustering(
nclusters=6, nneighbors=int(dist_tr.shape[0] * 0.1), equity_fraction=1, seed=1234
)
labels = clustering.fit(dist_tr)
coords["cluster"] = labels
clusters_figure = visualise_clusters(
coords,
longitude_colname="longitude",
latitude_colname="latitude",
label_col="cluster",
zoom=11,
)
clusters_figure.show()
Example 2: derive hyperparameters from a target size range
Specify the desired min/max cluster size and let the algorithm derive the hyperparameters automatically.
import pandas as pd
import numpy as np
from elsara import SpectralEqualSizeClustering, visualise_clusters
coords = pd.read_csv("datasets/restaurants_in_amsterdam.csv")
dist_tr = np.load("datasets/symmetric_dist_tr.npy")
min_range, max_range = 50, 70 # desired number of points per cluster
npoints = coords.shape[0]
avg_range = (max_range + min_range) / 2.0
nclusters = int(npoints / avg_range)
equity_fraction = 1 - ((avg_range - min_range) / avg_range)
nneighbors = int(npoints * (avg_range / npoints))
clustering = SpectralEqualSizeClustering(
nclusters=nclusters, nneighbors=nneighbors, equity_fraction=equity_fraction, seed=1234
)
labels = clustering.fit(dist_tr)
coords["cluster"] = labels
clusters_figure = visualise_clusters(
coords,
longitude_colname="longitude",
latitude_colname="latitude",
label_col="cluster",
zoom=11,
)
clusters_figure.show()
How to contribute
Prerequisities
- Python 3.13
- Poetry (in MAC:
brew install poetry)
Setup
Install dependencies and register the git hooks:
poetry install
make install-hooks
Code formatting
This project uses ruff and black for code formatting. To format all files manually, run:
make format
Formatting also runs automatically on every commit via pre-commit.
PR creation
- Create a branch using one of the following prefixes and open a PR to
main:fix/— bug fix, bumps the patch version (e.g. 0.2.0 → 0.2.1)feature/— new feature, bumps the minor version (e.g. 0.2.0 → 0.3.0)breaking/— breaking change, bumps the major version (e.g. 0.2.0 → 1.0.0)
Important: If the branch name doesn't follow this name convention, won't be accepted, as it won't update the package in PyPi.
- Create a PR to be reviewed by anamabo. She will suggest changes or approve your PR. In the later case, a new version of ELSARA is published to PyPI automatically.
License
This project is licensed under the MIT License.
Metadata
Release files for elsara 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| elsara-0.3.1.tar.gz | 11.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| elsara-0.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 21.8 kB
Release files / elsara-0.3.1.tar.gz
| Download URL | elsara-0.3.1.tar.gz |
|---|---|
| Size | 11.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
384760439fa7c1e4568dfd58461c1e10345e9ca6bd535588edde987bee58684d
|
|
BLAKE2b-256 checksum How to use checksums |
0529e8457d6c5c4a3552f776819016756198ab864dea69320aa5ac41d19aa5b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.13.5 Darwin/25.5.0
|
Release files / elsara-0.3.1-py3-none-any.whl
| Download URL | elsara-0.3.1-py3-none-any.whl |
|---|---|
| Size | 10.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0955fd0852c7938e5f786a7c8a332dfe8713604cc1f3c7f6920d46de5a4067d4
|
|
BLAKE2b-256 checksum How to use checksums |
64fdc1421c4480d83bbe5de0dc687c2fb5d42b5f565c9fe28489a6d6649186f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.1.3 CPython/3.13.5 Darwin/25.5.0
|
Security for elsara 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.