# Regrouping points together based on a sum

I have a vector layer containing points. Each point has an attribute called 'number of houses'. I want to dissolve this vector layer in many areas, each area must contain between 643 and 653 house and points must be near to each other.

The result must be something like this:

I work with QGIS and Python.

I am new in QGIS and Python and I don't know how to enter my shapefile or the coordinates as an input in those algorithms. And I don't want to divide my set into equal clusters, but into clusters based on the numbers of houses which are written near the point.

• Welcome to gis.stackexchange! Please note that a good question on this site is expected to show some degree of research on your part, i.e. what you have tried and - if applicable - code so far. For more info, you can check our faq. – underdark Jun 8 '16 at 16:30

I'd like to suggest you a few clusterization algorithms to do this.

Of course for that you should use some python libraries for machine learning. One of the most popular is scikit learn: http://scikit-learn.org/stable/index.html

Here you have an overview of clustering methods. There are a lot of algorithms and each is different. But the result is pretty much the same. You can look for the algorithm that takes number of samples as an input (for your task it will be 643 to 653) and perform the clusterization. Samples are always near to each others, so this will be good result for you.

Other possibility is to take simple k-means alghorithm and calculate the number of clusters:

``````import sklearn
n = number_of_points/648
cls = sklearn.cluster.KMeans(n_clusters=n)
cls.fit(x) # x is a matrix with your points
centroids = cls.cluster_centers_ # coordinates of centers for each cluster
classification = cls.predict(x) # cluster number for each point
``````

It should divide your set to equal clusters, so just add a new column to your shapefile with prediction and dissolve this layer with this attribute.

Of course first you have to extract only coordinates, perform clusterization on that, and join classified coordinates with the shapefile.