QGIS Analysis – Find Mean Distance Between Cluster Center and Points

clusteringdistanceMeasurementspointqgis

I have done the following in QGIS: I used the integrated k-means clustering tool to cluster around 160k data points into 50 clusters. Using the mean coordinate tool (Vector analysis > Mean coordinates) I found the center point of each cluster.

I now want to find the overall mean distance from all points in a respective cluster to the center point of that cluster; so in the end, I want 50 mean distances, one for each cluster. I currently have these two-point sets (clusters and center points) saved as two separate shapefiles.

How do I do this?

Best Answer

On the layer with the center point, you can use QGIS expressions. Get an array of the points from the same cluster using array_foreach and then for each of these points create create a line to the center and get it's length. You get an array of lengths. With array_mean, calculate the mean. This is how the expression looks like:

array_mean (
    array_foreach (
        overlay_nearest('points', $geometry, limit:=-1),
        length (
            make_line (
                $geometry,
                @element
            )
        )
    )
)

Based on the data you have and how it is structured, you have to include a condition so that only points belonging to the same center are considered.

From the red center point, the expression generates a line to each of the white dots from the points layer, measures the length and gets the mean. The lines itself that you see here are generated with an expression based on this one with Geometry generator, see on the right side:

enter image description here