R kmeans final distance to to centroid

独自空忆成欢 提交于 2019-12-24 00:39:32

问题


I have run a kmeans algorithm on the iris dataset in R using the command kmeans_iris <- kmeans(iris[,1:4], centers=3). I now want to know the distance from a given observation in the iris dataset to its corresponding cluster's centroid. I could write code to manually calculate the Euclidean distance from an observation to the centers corresponding to its cluster, but is there not an easy, built-in way to do this?


回答1:


As far as I can tell, there isn't a method for extracting the per case distance. If I understand what you want correctly, you could code your own like:

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 ...

...for a Euclidean distance.



来源:https://stackoverflow.com/questions/40498110/r-kmeans-final-distance-to-to-centroid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!