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.0.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.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: elsara-0.3.0.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.13.14 Linux/6.17.0-1018-azure

File hashes

Hashes for elsara-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c229ae5f392242996d8249d56f91a98be1e1a3259265898c7c8024052fc5f853
MD5 dec9f28505805c987144b6a8037c6934
BLAKE2b-256 8e6399448ae76b520e89a41511a25d52edaacf5927c3bd2e3cd4c77a4b10202b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: elsara-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.13.14 Linux/6.17.0-1018-azure

File hashes

Hashes for elsara-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 27a3ea20c0ffe74acba6485313a4bd287ba98371dc266aa18cfa262e9318f8dd
MD5 2bfe10824a580f9029aa9c083637edfe
BLAKE2b-256 b967bdd8d9004356888d2a23138a4617b74ecf1c88f7c69d179ddf5acc9f2abe

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