R kmeans final distance to the center of gravity

I executed the algorithm kmeansin the dataset irisin Rusing the command kmeans_iris <- kmeans(iris[,1:4], centers=3). Now I want to know the distance from this observation in the data set iristo its corresponding clustered centroid. I could write code to manually calculate the Euclidean distance from the observation to centersthat corresponding to its cluster, but is there no simple, built-in way to do this?

+2
source share
2 answers

As far as I can tell, there is no way to extract the distance for each case. If I understand correctly what you want, you can encode your own:

sqrt(rowSums(iris[,1:4] - fitted(kmeans_iris)) ^ 2)
#[1] 0.058 0.642 0.742 0.742 0.058 1.258 0.442 0.042 1.242 0.542 ...

... .

+1

, , , :

sqrt(***^2) .

, :

sqrt(rowSums((iris[,1:4] - fitted(kmeans_iris))^ 2))
-1

Source: https://habr.com/ru/post/1663636/


All Articles