Find K-Means average distance from points within a cluster to their respective centroid?

38 views (last 30 days)
I've run the k-means algorithm on my data and it returns "D" = distances from each point in the data set to every centroid, and "sumd" = within-cluster sums of point-to-centroid distances. However, I would like to find a way to calculate the mean distance from points within a cluster to their centroids. What is the best way to do this?

Accepted Answer

Image Analyst
Image Analyst on 16 Dec 2021
The second argument of kmeans is the cluster centroids. It's an easy matter to get the distance from each point in a class to the centroid. Let's say you have 3 classes and data has two coordinates, x, and y.
numClasses = 3;
[classIndexes, clusterCentroids] = kmeans(data, numClasses);
meanDistances = zeros(numClasses, 1);
for k = 1 : numClasses
% Get x and y of centroid.
xc = clusterCentroids(k, 1);
yc = clusterCentroids(k, 2);
% Get the x and y coordinates of points within this class only.
inClass = classIndexes == k; % Indexes of points that were assigned to this class.
x = data(inClass, 1); % Get not ALL x, but just x within this one class.
y = data(inClass, 2); % Get not ALL y, but just y within this one class.
% Get distances of all points in the class to the centroid for this class.
distances = sqrt((x - xc) .^ 2 + (y - yc) .^ 2);
meanDistances(k) = mean(distances);
end
The concept can be generalized to any number of features. Instead of 2 you can have 3 or 4 or 20. It doesn't matter - just make the obvious changes to generalize it. How many features do you have?
  10 Comments
D F
D F on 28 Dec 2021
Edited: D F on 28 Dec 2021
Oh okay, I see now, thank you. Also I was just curious based on your username if you have any experience with fMRI data. I have the locations of the 3 cluster centroids found from k-means, the MNI coordinates that were used to calculate the centroids, and the resultant p values and cluster size in voxels for each of the 3 clusters. I was wondering if you knew of a program/toolbox that would enable me to visualize these clusters of varying sizes on a "standard" brain? I don't have access to the imaging data/files currently. Thank you.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!