我有一个数据框,df
包含一堆点的x
和坐标。y
这是一段摘录:
> tail(df)
x y
1495 0.627174 0.120215
1496 0.616036 0.123623
1497 0.620269 0.122713
1498 0.630231 0.110670
1499 0.611844 0.111593
1500 0.412236 0.933250
我正在尝试找出最合适的集群数量。最终的目标是用数以万计的这些数据帧来做到这一点,所以选择的方法必须是快速的并且不能是可视化的。基于这些要求,RWeka 包似乎是要走的路。
我设法成功加载了 RWeka 包(我必须先在我的计算机上安装 Java SE Runtime)以及 RWeka 的包 XMeans,然后运行它:
library("RWeka") # requires Java SE Runtime
WPM("refresh-cache") # Build Weka package metadata cache
WPM("install-package", "XMeans") # Install XMeans package if not previously installed
weka_ctrl <- Weka_control( # Create a Weka control object to specify our parameters
I = 100, # max no iterations overall
M = 100, # max no iterations in the kmeans loop
L = 2, # min no clusters
H = 5, # max no clusters
D = "weka.core.EuclideanDistance", # distance metric
C = 0.4, S = 1)
x_means <- XMeans(df, control = weka_ctrl) # run algorithm on data
这正是我想要的结果:
XMeans
======
Requested iterations : 100
Iterations performed : 1
Splits prepared : 2
Splits performed : 0
Cutoff factor : 0.4
Percentage of splits accepted
by cutoff factor : 0 %
------
Cutoff factor : 0.4
------
Cluster centers : 2 centers
Cluster 0
0.4197712002617799 0.9346986806282739
Cluster 1
0.616697959239131 0.11564350951086963
Distortion: 30.580934
BIC-Value : 2670.359509
我可以通过运行将数据框中的每个点分配给集群x_means$class_ids
。
但是,我想有一种方法来检索集群中心的坐标。我可以在输出中看到它们并手动将它们写下来,但如果我要运行数万个这样的,我需要能够有一段代码将它们保存到一个变量中。我似乎无法x_means
使用方括号进行子集化,所以我不知道还能做什么。
非常感谢您的帮助!