R 中的 K 均值聚类与图

时间:2021-06-22 09:01:27

标签: r k-means unsupervised-learning

我写信是为了索取下面的代码

set.seed(4)
x=matrix(rnorm(50*2),ncol=2)
km.out<-kmeans(x,3,nstart=20)
km.out
plot(x, col=(km.out$cluster +1),main='K-Means Clustering Results with K=3', pch=20, cex=2)

我的问题是为什么要放km.out$cluster +1,为什么是'''+1''',而且情节丰富多彩,是不是默认设置的?

在线性回归或广义线性回归中,通过使用plot,使用结果只显示图片,没有颜色。

1 个答案:

答案 0 :(得分:1)

这纯粹是关于颜色的选择。如果您从 1 开始使用颜色,您的图表中将会有黑色的副本:

barplot(1:10, col=1:10)

enter image description here

虽然从 2 开始使用颜色向量不会有重复:

barplot(1:10, col=(1:10)+1)

enter image description here

从长远来看,我建议改用 ggplot,因为它对颜色的选择更加透明。

相关问题