我正在尝试创建我的数据的单热表示。这是我的方法:
data(iris)
iris = as.data.frame(apply(iris, 2, function(x) as.factor(x)))
head(iris)
iris_ohe <- data.frame(model.matrix(~.-1, iris))
head(iris_ohe)
dim(iris_ohe)
Run Code Online (Sandbox Code Playgroud)
问题是,我正在处理的数据有超过 100 万行,在进行编码时,我得到了一个超过 100 列的矩阵。这太多了R,我的内存不足:
Error: cannot allocate vector of size 10204.5 Gb
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法可以尝试?
尝试使用 mltools::one_hot
require(mltools)
require(data.table)
n <- 1e6
df1 <- data.table( ID= seq(1:n), replicate(99, sample(0:1,n,TRUE)))
one_hot(df1)
Run Code Online (Sandbox Code Playgroud)
对我来说没有内存问题,它几乎立即运行