Package: size-constrained-clustering
Size Constrained Clustering SolverImplementation of Size Constrained Clustering.
Size constrained clustering can be treated as an optimization problem. Details could be found in a set of reference paper.InstallationRequirement Python >= 3.6, Numpy >= 1.13, Cython >= 0.29install from PyPIpipinstallsize-constrained-clusteringMethodsFuzzy C-means Algorithm: Similar to KMeans, but use membership probability, not 0 or 1Same Size Contrained KMeans Heuristics: Use Heuristics methods to reach same size clusteringSame Size Contrained KMeans Inspired by Minimum Cost Flow ProblemMinimum and Maximum Size Constrained KMeans Inspired by Minimum Cost Flow ProblemDeterministic Annealling Algorithm: Input target cluster distribution, return correspondent clustersShrinkage Clustering: base algorithm and minimum size constraintsUsage:# setupfromsize_constrained_clusteringimportfcm,equal,minmax,shrinkage# by default it is euclidean distance, but can select othersfromsklearn.metrics.pairwiseimporthaversine_distancesimportnumpyasnpFuzzy C-meansn_samples=2000n_clusters=4centers=[(-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 Constraintn_samples=2000n_clusters=3X=np.random.rand(n_samples,2)# use minimum cost flow framework to solvemodel=equal.SameSizeKMeansMinCostFlow(n_clusters)# use heuristics method to solvemodel=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 Constraintn_samples=2000n_clusters=3X=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 Annealingn_samples=2000n_clusters=3X=np.random.rand(n_samples,2)# distribution is the distribution of cluster sizesmodel=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 ClusteringThe result might be not available.n_samples=1000n_clusters=4centers=[(-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_CopyrightCopyright (c) 2020 Jing Wang. Released under the MIT License.Third-party copyright in this distribution is noted where applicable.ReferenceClustering with Capacity and Size Constraints: A Deterministic
ApproachDeterministic Annealing, Clustering and OptimizationDeterministic Annealing, Constrained Clustering, and OpthiieationShrinkage ClusteringClustering with size constraintsData Clustering with Cluster Size Constraints Using a Modified k-means AlgorithmKMeans Constrained Clustering Inspired by Minimum Cost Flow ProblemSame Size Kmeans Heuristics MethodsGoogle's Operations Research tools'sSimpleMinCostFlowCluster KMeans Constrained