Is there a clustering method that allows me to indicate the number of points desired per cluster? I've looked into the various clustering algorithms and realize that specifying an exact cluster size up front sort of defeats the purpose of what clustering is intended to do - identify natural patterns in data. Imposing a size constraint up front might not be something anyone would want to do.
However, I do have a need for a method that allows me to specify the exact number of points allowed per cluster. Is there such an algorithm available or will I unfortunately have to try to write my own?
 A: Being some specific here, the question is not strictly speaking about clustering (i.e. discover underlying data structures) but rather for partitioning with general similarity constraints, to that extent this task is often referred at as balanced clustering. Finally to help one going forward terminology-wise: we care for "cluster cardinality", i.e. the number of elements in the cluster.
An approximate (almost) out-of-the-box solution can be to use $k$-means with some minor modifications; the ELKI data mining software has a great tutorial on how to perform same-size $k$-means variation, it contains example in Java. Without going into too much detail, we initialise our clustering our $k$-means variant with $k=\frac{n}{p}$ means/centroids, $p$ being the expected cluster cardinality. Then assign up to $p$ elements per cluster and iterate this procedure forward. This is essentially an E-M procedure same as the one done in "vanilla" $k$-means but with constraints during the $E$ step (Expectation - label assignment). The link present the whole procedure in with great care.
The above being said, the formal treatment of the problem is not trivial, there is a somewhat sparse technical work on the subject. Basically we need to reformulate this problem as an optimisation task with certain discrete constraints. For starters I  would recommend looking at: Balanced K-Means for Clustering (2014) by Malinen and Fränti and Balanced Clustering: A Uniform Model and Fast Algorithm (2019) by Lin et al. Unfortunately I have not seen any curated Python implementations on any of these papers; you might want to directly reach out to the authors.
A: @Eyal Shulman's python solution provides a K-means method that allows one to define cluster cardinality.
