Skip to main content

EquaL-size SpectrAl clusteRing Algorithm

Project description

Logo

License: MIT

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

  1. 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.

  1. 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.

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

elsara-0.3.1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

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

elsara-0.3.1-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file elsara-0.3.1.tar.gz.

File metadata

  • Download URL: elsara-0.3.1.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.5 Darwin/25.5.0

File hashes

Hashes for elsara-0.3.1.tar.gz
Algorithm Hash digest
SHA256 384760439fa7c1e4568dfd58461c1e10345e9ca6bd535588edde987bee58684d
MD5 a44d760d2c65e03d5dec9ca2097f586f
BLAKE2b-256 0529e8457d6c5c4a3552f776819016756198ab864dea69320aa5ac41d19aa5b6

See more details on using hashes here.

File details

Details for the file elsara-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: elsara-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.5 Darwin/25.5.0

File hashes

Hashes for elsara-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0955fd0852c7938e5f786a7c8a332dfe8713604cc1f3c7f6920d46de5a4067d4
MD5 864ab031d5e22b17feb7b3a63ca3e317
BLAKE2b-256 64fdc1421c4480d83bbe5de0dc687c2fb5d42b5f565c9fe28489a6d6649186f9

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