Skip to main content

A package for clustering with cluster size constraints.

Project description

ccluster: a package for clustering with size constraints

ccluster is a library for performing clustering with exhaustive or partial cluster size constraints. It provides two constrained clustering algorithms: a constrained k-means suitable for euclidean data, and a constrained graph clustering algorithm based on spectral clustering.

k-means example 1 k-means example 2 k-means example 3
spectral clustering example 1 spectral clustering example 2 spectral clustering example 3

Quick-Start

Here is an example of using constrained $k$-means with exhaustive constraints i.e. giving a size constraint for each cluster:

>>> from ccluster.size import ConstrainedKMeans
>>> import numpy as np

>>> X = np.array([[1, 2], [1, 4], [1, 0],
...               [10, 2], [10, 4], [10, 0]])
>>> kmeans = ConstrainedKMeans(
...     n_clusters=2,
...     cluster_size=[2, 4],
...     random_state=0,
...     n_init=10).fit(X)
>>> kmeans.labels_
# array([1, 1, 1, 0, 0, 1])  
>>> kmeans.predict([[0, 0], [12, 3]], [1, 1])
# array([1, 0], dtype=int32)  
>>> kmeans.cluster_centers_
# array([[10. , 3. ],  
#        [ 3.25, 1.5 ]])  

It is also possible to specify partial constraints i.e. constraints on a subset of the clusters and leave the remaining ones free. Here is an example using constrained spectral clustering:

>>> from ccluster.size import ConstrainedSpectralClustering
>>> import numpy as np

>>> X = np.array([[1, 1], [2, 1], [1, 0],
...               [4, 7], [3, 5], [3, 6],
...               [9, 6], [5, 4], [2, 1]])

>>> spectral = ConstrainedSpectralClustering(
...     n_clusters=4, 
...     cluster_sizes=[2, 2],
...     random_state=0).fit(X)

>>> spectral.labels_
# array([2, 3, 2, 0, 3, 0, 1, 1, 3])

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

ccluster-0.1.1.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

ccluster-0.1.1-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file ccluster-0.1.1.tar.gz.

File metadata

  • Download URL: ccluster-0.1.1.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for ccluster-0.1.1.tar.gz
Algorithm Hash digest
SHA256 fd9a736017a8a2c4141611ca2b3424d2b2c47c08bb64c613a26b5eec286bd0d0
MD5 1552cf7a89c3569a07b02044e477dc2e
BLAKE2b-256 ca6aa5917a2c57c132359b6499e0f6197151a9af207c40acb304c741c5cdfde3

See more details on using hashes here.

File details

Details for the file ccluster-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: ccluster-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for ccluster-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 521e629b792de75fcc1f5ded694939f728428ab25f367d217748316a1f142af5
MD5 77673c3a6b3ebfe53044f497a0eb238a
BLAKE2b-256 eb32de9e7787bdd9311c9a0204dba9e5d8e3d427bff928a5e3f0c146d2c43b7e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page