Size Constrained Clustering Solver
Implementation of Size Constrained Clustering. Size constrained clustering can be treated as an optimization problem. Details could be found in a set of reference paper.
Installation
Requirement Python >= 3.6, Numpy >= 1.13, Cython >= 0.29
- install from PyPI
pip install size-constrained-clustering
Methods
- Fuzzy C-means Algorithm: Similar to KMeans, but use membership probability, not 0 or 1
- Same Size Contrained KMeans Heuristics: Use Heuristics methods to reach same size clustering
- Same Size Contrained KMeans Inspired by Minimum Cost Flow Problem
- Minimum and Maximum Size Constrained KMeans Inspired by Minimum Cost Flow Problem
- Deterministic Annealling Algorithm: Input target cluster distribution, return correspondent clusters
- Shrinkage Clustering: base algorithm and minimum size constraints
Usage:
# setup
from size_constrained_clustering import fcm, equal, minmax, shrinkage
# by default it is euclidean distance, but can select others
from sklearn.metrics.pairwise import haversine_distances
import numpy as np
Fuzzy C-means
n_samples = 2000
n_clusters = 4
centers = [(-5, -5), (0, 0), (5, 5), (7, 10)]
X, _ = make_blobs(n_samples=n_samples, n_features=2, cluster_std=1.0,
centers=centers, shuffle=False, random_state=42)
model = fcm.FCM(n_clusters)
# use other distance function: e.g. haversine distance
# model = fcm.FCM(n_clusters, distance_func=haversine_distances)
model.fit(X)
centers = model.cluster_centers_
labels = model.labels_
Equal Size Constraint
n_samples = 2000
n_clusters = 3
X = np.random.rand(n_samples, 2)
# use minimum cost flow framework to solve
model = equal.SameSizeKMeansMinCostFlow(n_clusters)
# use heuristics method to solve
model = equal.SameSizeKMeansHeuristics(n_clusters)
model.fit(X)
centers = model.cluster_centers_
labels = model.labels_
Cluster size: 667, 667 and 666 in the figure above.
Minimum and Maximum Size Constraint
n_samples = 2000
n_clusters = 3
X = np.random.rand(n_samples, 2)
model = minmax.MinMaxKMeansMinCostFlow(n_clusters, size_min=400, size_max=800)
model.fit(X)
centers = model.cluster_centers_
labels = model.labels_
Cluster size: 753, 645 and 602 in the figure above.
Deterministic Annealing
n_samples = 2000
n_clusters = 3
X = np.random.rand(n_samples, 2)
# distribution is the distribution of cluster sizes
model = da.DeterministicAnnealing(n_clusters, distribution=[0.1, 0.6, 0.3])
model.fit(X)
centers = model.cluster_centers_
labels = model.labels_
Cluster size: 1200, 600 and 200 in the figure above, corresponding to distribution [0.6, 0.3, 0.1]
Shrinkage Clustering
The result might be not available.
n_samples = 1000
n_clusters = 4
centers = [(-5, -5), (0, 0), (5, 5), (7, 10)]
X, _ = make_blobs(n_samples=n_samples, n_features=2, cluster_std=1.0, centers=centers, shuffle=False, random_state=42)
model = shrinkage.Shrinkage(n_clusters, size_min=100)
model.fit(X)
centers = model.cluster_centers_
labels = model.labels_
Copyright
Copyright (c) 2020 Jing Wang. Released under the MIT License.
Third-party copyright in this distribution is noted where applicable.
Reference
- Clustering with Capacity and Size Constraints: A Deterministic Approach
- Deterministic Annealing, Clustering and Optimization
- Deterministic Annealing, Constrained Clustering, and Opthiieation
- Shrinkage Clustering
- Clustering with size constraints
- Data Clustering with Cluster Size Constraints Using a Modified k-means Algorithm
- KMeans Constrained Clustering Inspired by Minimum Cost Flow Problem
- Same Size Kmeans Heuristics Methods
- Google's Operations Research tools's
SimpleMinCostFlow - Cluster KMeans Constrained
Metadata
Release files for size-constrained-clustering 0.1.2
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 | Actions |
|---|---|---|---|---|
| size_constrained_clustering-0.1.2.tar.gz | 51.5 kB | Without provenance | Details for size_constrained_clustering-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| size_constrained_clustering-0.1.2-cp36-cp36m-macosx_10_7_x86_64.whl | CPython 3.6 | CPython 3.6 pymalloc | macOS 10.7+ x86-64 | Details for size_constrained_clustering-0.1.2-cp36-cp36m-macosx_10_7_x86_64.whl |
Total release size: 485.1 kB
Release files / size_constrained_clustering-0.1.2.tar.gz
| Download URL | size_constrained_clustering-0.1.2.tar.gz |
|---|---|
| Size | 51.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e952df49d3c746df1fa427b3d753d8c81eb825b26fe37dd540019c12aef13d36
|
|
BLAKE2b-256 checksum How to use checksums |
922e242ce69829c4713c88f7be0562ddf2bbf0accf3340040472cea6ca6fcf68
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.1.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.6
|
Release files / size_constrained_clustering-0.1.2-cp36-cp36m-macosx_10_7_x86_64.whl
| Download URL | size_constrained_clustering-0.1.2-cp36-cp36m-macosx_10_7_x86_64.whl |
|---|---|
| Size | 433.6 kB |
| Tags | CPython 3.6 CPython 3.6 pymalloc macOS 10.7+ x86-64 |
|
SHA-256 checksum How to use checksums |
babd1a14332464f502b0785614edc700c6d39fed7f4a27a19ea090130c95cd1a
|
|
BLAKE2b-256 checksum How to use checksums |
f919f4f1dab7ec5f0e882dfc22e146063a85a3981960af6fdb99a4ac52a3fc93
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.1.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.6
|
Security for size-constrained-clustering 0.1.2
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.