# Python: Clustering based on pairwise distance matrix [closed]

I have a matrix which represents the distances between every two relevant items. For example, M[i][j] holds the distance between items i and j. My next aim is to cluster items by these distances. I can provide some parameters: maximal number of clusters, maximal distance between two items in a cluster and minimal number of items in a cluster. I cannot, however, provide the "central items" which the algorithm should start with. I went through some of python's clustering packages, like scikit-learn, but did not find anything that matches.

Any ideas?

Thank you :)

## closed as unclear what you're asking by David Richerby, Evil, chi, D.W.♦Jun 13 '18 at 16:53

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• I can't understand what you are asking. What is your question? This is a question-and-answer site, so you need to articulate a question. A question should end with a question mark ("?"). It sounds like maybe you are looking for a clustering algorithm, but it's not clear what your requirements are. There are many clustering algorithms, and many of them don't require "central items". – D.W. Jun 13 '18 at 16:52
• When you say "which the algorithm should start with", it's not clear what algorithm you are referring to. Are you perhaps under the assumption that k-means is the only way to do clustering? I encourage you to do some research on clustering algorithms. There's lots written online. Take a look at standard algorithms and see if they meet your needs. If they don't, that should help you articulate your requirements more clearly in the question. It might also help to tell us what clustering algorithms you've already considered and why you've rejected them. – D.W. Jun 13 '18 at 16:53
• Note that Python-specific questions or requests for us to recommend a software library are off-topic here. – D.W. Jun 13 '18 at 16:53

For example, if the clustering you want is such that the maximum number of clusters is $k_{max}$ and the maximum distance between two elements in a cluster is $d_{max}$, one possible approach is to perform a Complete-linkage Hierarchical clustering of your items. Possible clusterings then correspond to cuts in the resulting dendogram. Most library include the possibility of finding a cut that correspond to $k$ clusters or at a "height" $d$. A cut at height $d_{max}$ corresponds to the smallest clustering that respect the distance constraint. If the latter is not possible with the library you are using, you can explore every cut from size $1$ to size $k_{max}$ and select one that respects the distance constraint (Note it may not always be possible to found one e.g. with $d_{max}$ close to $0$ and $k_{max}$ close to $1$.)