r - R:缩放 Data.Table 中的每一列
问题描述
我有一个包含 37000 行和 27000 列的 data.table。我想在将数据用于预测任务之前对每一列进行预处理和缩放。
我正在使用这篇文章中提到的方法,但发现它工作得非常缓慢,甚至使 R Studio 崩溃。我附上了下面的方法,供参考。是否有更快的方法来缩放大型 data.table 的所有列?
scale.cols <- colnames(DT)
DT[, (scale.cols) := lapply(.SD, scale), .SDcols = scale.cols]
解决方案
data.table
假设您可以首先以矩阵格式获取数据,因为对于大量列来说它不会很快,那么一种可能性是使用RcppArmadillo
scale.cpp
:
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
using namespace Rcpp;
// [[Rcpp::export]]
arma::mat armaScale(arma::mat Z) {
unsigned int j, n = Z.n_rows, k = Z.n_cols;
double avg, sd;
arma::colvec z;
arma::mat res = arma::zeros(n, k);
for (j=0; j<k; j++) {
z = Z.col(j);
avg = arma::mean(z);
sd = arma::stddev(z);
res.col(j) = (z - avg) / sd;
}
return res;
}
代码:
set.seed(0L)
#using a smaller dataset
s <- 2e3
nr <- 3*s
nc <- 2*s
mat <- matrix(rnorm(nr*nc), ncol=nc)
library(RcppArmadillo)
library(Rcpp)
sourceCpp("scale.cpp")
library(microbenchmark)
microbenchmark(armaScale(mat), scale(mat), times=3L)
时间:
Unit: milliseconds
expr min lq mean median uq max neval cld
armaScale(mat) 272.4988 290.1339 303.5027 307.7689 319.0047 330.2404 3 a
scale(mat) 1290.9581 1400.7916 1445.8927 1510.6251 1523.3600 1536.0950 3 b
推荐阅读
- c++ - substring() 抛出未处理的异常
- prototypejs - 有没有办法让这个“未定义”的对象安全?
- javascript - 在这个 js 示例中如何从 api 调用中获取密钥
- python - 编辑类以兼容 JSON
- jquery - 在 Primefaces/JSF 中看不到与单选按钮和复选框相关的图标(部署在 Tomee 服务器中)
- r - 是否有可以根据元素长度复制条目的 R 函数
- plugins - 创建 Flutter 插件时如何修改类名
- javascript - 使用 Promises 时的清除代码
- angular - 更改 nativescript-mapbox 自定义标记的大小
- java - 宁静报告仪表板