R:每次我运行时,聚类结果都是不同的 [英] R: Clustering results are different everytime I run

查看:1424
本文介绍了R:每次我运行时,聚类结果都是不同的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

library(amap)
set.seed(5)
Kmeans(mydata, 5, iter.max=500, nstart=1, method="euclidean")

在"amap"程序包中运行了几次,但是即使参数和种子值始终相同,但每次运行Kmeans或其他聚类方法时,聚类结果都是不同的.

in 'amap' package and run several times, but even though the parameters and seed value are always the same, the clustering results are different every time I run Kmeans, or other cluster methods.

我在不同的程序包中尝试了另一个kmeans函数,但仍然相同...

I tried another kmeans function in different packages, but still the same...

事实上,我想同时使用Weka和R,所以我也在RWeka包中尝试了SimpleKMeans,这总是提供相同的值.但是,问题是我不知道如何在RWeka中存储来自SimpleKmeans的聚类数据以及聚类编号,因此我被卡住了.

In fact, I want to use the Weka and R together, so I also tried SimpleKMeans in RWeka package, and this gives always the same value. However, the problem is that I do not know how to store the clustered data along with the cluster number from SimpleKmeans in RWeka so I'm stuck...

无论如何,如何使聚类结果始终相同?或如何将SimpleKmeans的聚类结果存储到R中?

Anyhow, how can I keep the clustering result always the same? or How can I store the clustering result from SimpleKmeans into R?

推荐答案

您必须做错了什么.每次运行以下代码,只要在每次调用Kmeans()之前设置种子,我都会得到可重复的结果:

You must be doing something wrong. I get reproducible results each time I run the following code, as long as I set the seed before each call to Kmeans():

library(amap)

out <- vector(mode = "list", length = 10)
for(i in seq_along(out)) {
    set.seed(1)
    out[[i]] <- Kmeans(iris[, -5], 3, iter.max=500, nstart=1, method="euclidean")
}

for(i in seq_along(out[-1])) {
    print(all.equal(out[[i]], out[[i+1]]))
}

最后一个for循环打印:

The last for loop prints:

[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE

每次指示结果完全相同.

Indicating the results are exactly the same each time.

这篇关于R:每次我运行时,聚类结果都是不同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆