I have a dataset of longitudes and latitudes for stores in New York City. The data consists of only three columns - longitude, latitude, and store ID.

I want to use python to cluster these stores by using longitude and latitude. Of course ID is not clusterable so I will remove it from the data before clustering.

The type of clusters I want… I want clusters that are density based. If there’s a large number of stores in an area, the algorithm should recognize that. I also want the algorithm to be able to recognize outliers. Third, I don’t want the clusters to be drastically different in size. They don’t need to be the exact same of course, because density will play a part in that. But at the same time, I don’t want a cluster with 100 stores when the average number of stores in a cluster should be around 20. Lastly. I need the algorithm to recognize that this is geospatial data. I need it to cluster accordingly.

What is the best clustering algorithm to use for this geospatial data?

I know the obvious answer seems like DBSCAN. But I promise I have tried it out and have done a lot of research on tuning those parameters. However it doesn’t meet the criteria I’m looking for.

What other algorithms will be worth a try?

  • 2
    $\begingroup$ I would consider Gaussian mixtures among others. Since there are multiple density-based methods, you may wish to further qualify what you mean by "best clustering algorithm". $\endgroup$
    – Galen
    Feb 10, 2022 at 23:27
  • $\begingroup$ What do you mean by "I want clusters that are density based". Density, as opposed to what? $\endgroup$ Feb 10, 2022 at 23:39
  • 2
    $\begingroup$ In many data sets, your density requirement is in conflict with clusters being somewhat balanced in size. Did you plot your data and are you sure that reasonable density-based clusters will not differ much in size? $\endgroup$ Feb 11, 2022 at 0:09
  • $\begingroup$ pdfcluster cran.r-project.org/web/packages/pdfCluster/index.html is another method for density-based clustering. Not sure whether it exists in python though. $\endgroup$ Feb 11, 2022 at 0:10
  • $\begingroup$ Could you post a visualization (something like a map) of your data, or of simulated data similar to the real data you're working with? $\endgroup$
    – Adrian
    Feb 11, 2022 at 0:31

1 Answer 1


I am not very familiar with the peculiarities of geospatial data. As a result, I'm not sure what you mean when you say "I need the algorithm to recognize that this is geospatial data".

This sounds like a perfect use case of K-means clustering to me. You essentially have an XY plane, and you need to group the points together based on their literal distances to each other.

I would try K-means, and adjusting the parameters (especially the "number of clusters/means") until you're either visually satisfied, or you can take advantage of some objective measure of clustering quality like the silhouette coefficient.


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.