选择最小化R中的条件的列子集

更亮

我有一个data.frame看起来像这样的稀疏二进制文件

set.seed(123)
dat <- as.data.frame(matrix(rep(round(runif(40,0,0.9),0),5),ncol = 20))

#  > dat
#    V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20
# 1   0  0  1  1  0  0  1  1  0   0   1   1   0   0   1   1   0   0   1   1
# 2   0  0  0  1  0  0  0  1  0   0   0   1   0   0   0   1   0   0   0   1
# 3   0  1  0  1  0  1  0  1  0   1   0   1   0   1   0   1   0   1   0   1
# 4   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0
# 5   0  1  1  0  0  1  1  0  0   1   1   0   0   1   1   0   0   1   1   0
# 6   0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0
# 7   0  0  1  0  0  0  1  0  0   0   1   0   0   0   1   0   0   0   1   0
# 8   0  1  1  1  0  1  1  1  0   1   1   1   0   1   1   1   0   1   1   1
# 9   0  1  1  0  0  1  1  0  0   1   1   0   0   1   1   0   0   1   1   0
# 10  1  0  0  0  1  0  0  0  1   0   0   0   1   0   0   0   1   0   0   0

我需要找到3列,这些列可以最大程度地减少我在调用rowSums这些列时获得的零

例子:

 # > rowSums(dat[,1:3])
 # [1] 2 2 2 3 2 2 0 2 0 1
 # 
 # > rowSums(dat[,2:4])
 # [1] 3 2 3 3 1 2 1 1 0 1

在这里,当我调用rowSums前3列时,我得到2个零,而当我调用rowSums列时,2:4我只得到1 0,因此第二个解决方案将是首选。

当然,在应用时rowSums,我不需要将各列彼此相邻,因此,我需要探索所有可能的组合(例如:我rowSums也要考虑ov V1+V5+V17,...的情况),如果有的话多个“最佳”解决方案,我可以保留其中一个。

请注意,我的实际数data.frame是220.000行x 200列,所以我需要一种有效的方法来消耗时间/内存。

bgoldst

这是最明显的解决方案,尽管可能无法很好地扩展:

which.min(combn(dat,3L,function(x) sum(rowSums(x)==0)));
## [1] 2

可以将输出值2视为组合索引。通过combn()在输入对象的完整列索引集上运行并为该索引的特定索引编制索引,可以获取属于该组合的列:

cis <- combn(seq_along(dat),3L)[,2L];
cis;
## [1] 1 2 4

然后获取列名很容易:

names(dat)[cis];
## [1] "V1" "V2" "V4"

您可以按以下方式获得解决方案中的零个数:

sum(rowSums(dat[,cis])==0);
## [1] 1

我用Rcpp编写了一个更快的解决方案。

为了使该函数更通用,我编写了一个逻辑矩阵,而不是data.frame,并设计了查找具有最少真实行的列组合的设计。因此,对于您的情况,您可以将参数计算为dat==0我还将组合中的列数参数化为第二个参数r,对于您的情况将为3。

library(Rcpp);
Sys.setenv('PKG_CXXFLAGS'='-std=c++11');

cppFunction('
    IntegerVector findColumnComboWithMinimumAllTrue(LogicalMatrix M,int r) {
        std::vector<int> rzFull(M.nrow()); std::iota(rzFull.begin(),rzFull.end(),0);
        std::vector<int> rzErase;
        std::vector<std::vector<int>> rzs(M.ncol(),std::vector<int>(M.nrow()));
        std::vector<std::vector<int>*> rzps(M.ncol());
        std::vector<int>* rzp = &rzFull;
        std::vector<int> com(r);
        int bestAllTrueCount = M.nrow()+1;
        std::vector<int> bestCom(r);
        int pmax0 = M.ncol()-r;
        int p = 0;
        while (true) {
            rzErase.clear();
            for (int rzi = 0; rzi < rzp->size(); ++rzi)
                if (!M((*rzp)[rzi],com[p])) rzErase.push_back(rzi);
            if (p+1==r) {
                if (rzp->size()-rzErase.size() < bestAllTrueCount) {
                    bestAllTrueCount = rzp->size()-rzErase.size();
                    bestCom = com;
                }
                if (com[p]==pmax0+p) {
                    do {
                        --p;
                    } while (p >= 0 && com[p]==pmax0+p);
                    if (p==-1) break;
                    ++com[p];
                    rzp = p==0 ? &rzFull : rzps[p-1];
                } else {
                    ++com[p];
                }
            } else {
                if (rzErase.empty()) {
                    rzps[p] = rzp;
                } else {
                    rzs[p].clear();
                    int rzi = -1;
                    for (int ei = 0; ei < rzErase.size(); ++ei)
                        for (++rzi; rzi < rzErase[ei]; ++rzi)
                            rzs[p].push_back((*rzp)[rzi]);
                    for (++rzi; rzi < rzp->size(); ++rzi)
                        rzs[p].push_back((*rzp)[rzi]);
                    rzp = rzps[p] = &rzs[p];
                }
                ++p;
                com[p] = com[p-1]+1;
            }
        }
        IntegerVector res(bestCom.size());
        for (int i = 0; i < res.size(); ++i)
            res[i] = bestCom[i]+1;
        return res;
    }
');

这是您的示例输入的演示:

set.seed(123L);
dat <- as.data.frame(matrix(rep(round(runif(40,0,0.9),0),5),ncol=20L));
findColumnComboWithMinimumAllTrue(dat==0,3L);
## [1] 1 2 4

这是一个全尺寸测试,在我的系统上需要将近10分钟的时间:

set.seed(1L); NR <- 220e3L; NC <- 200L;
dat <- as.data.frame(matrix(sample(0:1,NR*NC,T),NR,NC));
system.time({ findColumnComboWithMinimumAllTrue(dat==0,3L); });
##    user  system elapsed
## 555.641   0.328 556.401
res;
## [1] 28 64 89

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章