I'm looking for a clustering algorithm (ideally density based) that allows me to specify the maximum number of clusters (but not the exact number). All points must be assigned to a cluster, so I can't just ignore the smallest/least dense clusters.

Can anyone suggest an algorithm that may be suitable for this for this purpose, or think of a way of adapting an existing algorithm?

DbScan and variants are not appropriate, as they have no way of limiting the number of clusters. They also wont classify every point.

K-Means requires the exact value of k to be specified, and also is not density based, so works poorly with my data.

  • 1
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask
    – gnat
    Commented Jul 18, 2018 at 13:45

1 Answer 1


In a density-based algorithm like DBSCAN or OPTICS it doesn't make sense to limit the number of clusters, as the samples are not assigned to specific clusters but are linked to samples in their neighborhood. Each connected component of samples then forms a cluster.

You could in principle tune the neighborhood-distance epsilon or the neighborhood-density MinPts in order to change which clusters are found. However, tuning these parameters in either direction has no clear connection to the number of clusters. E.g. increasing the epsilon-parameter might increase the number of clusters by finding another less dense cluster in the noise, or decrease the number of clusters by merging two nearby clusters.

This is not a constraint of those specific algorithms, but a constraint of hierarchical/agglomerative density-based clustering in general. Note that the OPTICS variant of DBSCAN will assign all input points to a cluster, although weakly linked samples that represent noise are usually cut off in a postprocessing step.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.