问题描述
关于kmeans聚类的Wikibook( http://en.wikibooks.org /wiki/Data_Mining_Algorithms_In_R/Clustering/K-Means )提供了一个示例聚类分析:
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)
推荐答案
正如我在评论中提到的,群集已经用颜色标记",其中不同的颜色与群集成员身份相关联.要绘制集群标签",可以使用:
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.
这篇关于如何在R中标记k均值聚类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!