# how to calculate the distance between cluster center and datapoint in K-means

I'm learning about the K-means model. I'm going to detect the anomalies using the k-means for that I need to calculate the distance between centers and point how should I do that.

• By pythagorean theorem, if your input are the coordinates of data points plus of the centres. – ttnphns Aug 8 '17 at 11:47
• sorry, I didn't get it – Newbie Aug 8 '17 at 11:50
• (L2) norm(x-m) where m is the centre and x is the point. But this doesn't necessarily pick out real anomalies. For some clusters points 'far away' from the centre could be perfectly normal. You may do better using GMM and allowing each cluster its own covariance matrix. – conjectures Aug 8 '17 at 11:55

## 1 Answer

You can just compute the Euclidean distance of any points within one cluster with respect to that cluster's centroid. For categorical variable, it is preferable to use distance as either 1 or 0 (means if 2 observations of same features are having same class then take distance for that feature as 1,else 0). To get the K-means cluster(assuming that you are using some language for clustering), K-means generally have a attribute like "cluster_centers_" (available in python)