How to label k-means clusters in r

不羁的心 提交于 2019-12-07 17:21:19

问题


The wikibook on kmeans clustering (http://en.wikibooks.org/wiki/Data_Mining_Algorithms_In_R/Clustering/K-Means) gives an example cluster analysis :

Can the code be amended so that a label is generated from each cluster? Below graph does not indicate what is being compared. There are three clusters but what are the names of each cluster ?

Here is the code that generates the graph :

# import data (assume that all data in "data.txt" is stored as comma separated values)
x <- read.csv("data.txt", header=TRUE, row.names=1)

# run K-Means
km <- kmeans(x, 3, 15)

# print components of km
print(km)

# plot clusters
plot(x, col = km$cluster)
# plot centers
points(km$centers, col = 1:2, pch = 8)

回答1:


As I mentioned in the comments, the clusters are already "labelled" by colour, where different colours are associated with cluster membership. To plot the "cluster labels" instead, you can use:

plot(x, type='n')
text(x, labels=km$cluster, col=km$cluster)

This should plot the "cluster name" instead of the points, and also colour the labels by the clusters.



来源:https://stackoverflow.com/questions/17557984/how-to-label-k-means-clusters-in-r

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