k表示用r中的原始数据绘图
目前我正在探索k表示用r中的原始数据绘图,r,plot,k-means,R,Plot,K Means,目前我正在探索kmeans函数。我有一个简单的文本文件(test.txt),其中包含以下条目。数据可以分为两个簇 1 2 3 8 9 10 如何将kmeans函数的结果(使用plot函数)与原始数据一起绘制?我还感兴趣的是观察集群如何与其质心一起分布?这是示例(kmeans)中的示例: #这只是为了生成示例数据 谢谢Mehdi!但是我需要一个使用基本“plot”函数的解决方案。请尝试plot(test$V1,col=kmeans(test$V1,
kmeans
函数。我有一个简单的文本文件(test.txt
),其中包含以下条目。数据可以分为两个簇
1
2
3
8
9
10
如何将
kmeans
函数的结果(使用plot
函数)与原始数据一起绘制?我还感兴趣的是观察集群如何与其质心一起分布?这是示例(kmeans)
中的示例:
#这只是为了生成示例数据
谢谢Mehdi!但是我需要一个使用基本“plot”函数的解决方案。请尝试plot(test$V1,col=kmeans(test$V1,2)$cluster)
(假设您的列名为V1
)谢谢David!。。。。它起了作用,帮助了我!。。。。但是如何突出显示集群的中心呢?但是你能用我的test.txt文件告诉我步骤吗!你的数据只有一个维度,所以你最终会得到一个奇怪的图。为此,您必须将第三次和第四次出现的test
更改为test[,1]
谢谢Mehdi!但随着变化,我并没有得到集群的中心。test[,1]
与test有何不同?@soumait我对我的答案进行了编辑,解释了它和你的情况
# This is just to generate example data
test <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(test) <- c("V1", "V2")
#store the kmeans in a variable called cl
(cl <- kmeans(test, 2))
# plot it and also plot the points of the centeroids
plot(test, col = cl$cluster)
points(cl$centers, col = 1:2, pch = 8, cex = 2)
(cl <- kmeans(test, 2))
plot(test, col = cl$cluster)
points(cl$centers, col = 1:2, pch = 8, cex = 2)
(cl <- kmeans(test[,1], 2))
plot(test[,1], col = cl$cluster)
points(cl$centers, col = 1:2, pch = 8, cex = 2)
(cl <- kmeans(test[,1], 2))
plot(cbind(0,test[,1]), col = cl$cluster)
points(cbind(0,cl$centers), col = 1:2, pch = 8, cex = 2)