使用dplyr和扫帚计算训练和测试集上的kmeans

时间:2016-10-18 04:40:04

标签: r dplyr k-means broom

我正在使用dplyr和broom为我的数据计算kmeans。我的数据包含一个X和Y坐标的测试和训练集,并按一些参数值(在这种情况下为lambda)分组:

mds.test = data.frame()
for(l in seq(0.1, 0.9, by=0.2)) {
  new.dist <- run.distance.model(x, y, lambda=l)
  mds <- preform.mds(new.dist, ndim=2)
  mds.test <- rbind(mds.test, cbind(mds$space, design[,c(1,3,4,5)], lambda=rep(l, nrow(mds$space)), data="test"))
}

> head(mds.test)
                        Comp1       Comp2 Transcripts Genes Timepoint Run lambda data
7A_0_AAGCCTAGCGAC -0.06690476 -0.25519106       68125  9324     Day 0  7A    0.1 test
7A_0_AAATGACTGGCC -0.15292848  0.04310200       28443  6746     Day 0  7A    0.1 test
7A_0_CATCTCGTTCTA -0.12529445  0.13022908       27360  6318     Day 0  7A    0.1 test
7A_0_ACCGGCACATTC -0.33015913  0.14647857       23038  5709     Day 0  7A    0.1 test
7A_0_TATGTCGGAATG -0.25826098  0.05424976       22414  5878     Day 0  7A    0.1 test
7A_0_GAAAAAGGTGAT -0.24349387  0.08071162       21907  6766     Day 0  7A    0.1 test

head上面的测试数据集,但我也有一个名为mds.train,其中包含我的训练数据坐标。我的最终目标是为lambda分组的两个集合运行k-means,然后计算训练中心测试数据的within.ss,between.ss和total.ss 。感谢a great resource扫帚,我可以通过简单地执行以下操作为测试集运行每个lambda的kmeans:

test.kclusts  = mds.test %>% 
  group_by(lambda) %>% 
  do(kclust=kmeans(cbind(.$Comp1, .$Comp2), centers=length(unique(design$Timepoint))))

然后我可以计算每个lambda中每个簇的数据中心:

test.clusters = test.kclusts %>% 
  group_by(lambda) %>%  
  do(tidy(.$kclust[[1]])) 

这是我被困的地方。如何计算功能分配,如reference page所示(例如kclusts %>% group_by(k) %>% do(augment(.$kclust[[1]], points.matrix))),points.matrixmds.test length(unique(mds.test$lambda))glance()test.kclusts = mds.test %>% group_by(lambda) %>% do(kclust=kmeans(cbind(.$Comp1, .$Comp2), centers=length(unique(design$Timepoint)))) test.clusters = test.kclusts %>% group_by(lambda) %>% do(tidy(.$kclust[[1]])) test.clusterings = test.kclusts %>% group_by(lambda) %>% do(glance(.$kclust[[1]])) test.assignments = left_join(test.kclusts, mds.test) %>% group_by(lambda) %>% do(augment(.$kclust[[1]], cbind(.$Comp1, .$Comp2))) train.kclusts = mds.train %>% group_by(lambda) %>% do(kclust=kmeans(cbind(.$Comp1, .$Comp2), centers=length(unique(design$Timepoint)))) train.clusters = train.kclusts %>% group_by(lambda) %>% do(tidy(.$kclust[[1]])) train.clusterings = train.kclusts %>% group_by(lambda) %>% do(glance(.$kclust[[1]])) train.assignments = left_join(train.kclusts, mds.train) %>% group_by(lambda) %>% do(augment(.$kclust[[1]], cbind(.$Comp1, .$Comp2))) test.assignments$data = "test" train.assignments$data = "train" merge.assignments = rbind(test.assignments, train.assignments) merge.assignments %>% filter(., data=='test') %>% group_by(lambda) ... ? 行的数量应该是多少?有没有办法以某种方式使用训练集中的中心根据测试任务计算Error:Execution failed for task ':app:processDebugResources'. > com.android.ide.common.process.ProcessException: Failed to execute aapt统计数据?

任何帮助将不胜感激!谢谢!

编辑:更新进度。我已经弄清楚如何聚合测试/培训任务,但仍然有问题尝试从两组计算kmeans统计数据(测试中心的培训任务和培训中心的测试任务)。更新后的代码如下:

config.forcePasteAsPlainText = false;
config.pasteFromWordRemoveFontStyles = false;
config.pasteFromWordRemoveStyles = false;
config.allowedContent = true;

我附上了一张图,说明了我在这一点上的进展。重申一下,我想计算测试任务/坐标(中心看不见的图)的训练数据中心的kmeans统计数据(在平方和,平方和之间以及平方和之间): enter image description here

0 个答案:

没有答案