Changed centroid calculation to use weighted average based on mean inverse
square distance to n-nearest neighbours.