Rowwise matrix multiplication in R

后端 未结 4 564
别跟我提以往
别跟我提以往 2021-01-18 23:05

I have a matrix with the dimension of 100 million records and 100 columns.

Now I want to multiply that matrix by rowwise.

My sample code for matrix multiplic

4条回答
  •  渐次进展
    2021-01-18 23:36

    If you have a matrix that is too large to fit in memory, you can use package bigstatsr (disclaimer: I'm the author) to use data stored on your disk (instead of the RAM). Using function big_apply enables you to apply standard R functions on data blocks (and to combine them).

    library(bigstatsr)
    fbm <- FBM(10e6, 100)
    # inialize with random numbers
    system.time(
      big_apply(fbm, a.FUN = function(X, ind) {
        print(min(ind))
        X[, ind] <- rnorm(nrow(X) * length(ind))
        NULL
      }, a.combine = 'c')
    ) # 78 sec
    
    # compute row prods, possibly in parallel
    system.time(
      prods <- big_apply(fbm, a.FUN = function(X, ind) {
        print(min(ind))
        matrixStats::rowProds(X[ind, ])
      }, a.combine = 'c', ind = rows_along(fbm),
      block.size = 100e3, ncores = nb_cores())  
    ) # 22 sec with 1 core and 18 sec with 6 cores
    

提交回复
热议问题