在R上对大数据进行单次热编码的有效方法 [英] Efficient way to do one-hot encoding in R on large data

查看:99
本文介绍了在R上对大数据进行单次热编码的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建我的数据的一站式表示形式.这是我的方法:

I'm trying to create a one-hot representation of my data. This is my approach:

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)

问题是,我正在处理的数据超过一百万行,并且进行编码后,我得到了一个包含100列以上的矩阵.对于R来说太多了,我的内存用完了:

The thing is, the data I'm working on has over 1 million rows, and doing the encoding, I get a matrix with over 100 columns. This is too much for R and I run out of memory:

Error: cannot allocate vector of size 10204.5 Gb

有没有更好的方法可以尝试?

Is there a better approach I could try?

推荐答案

尝试使用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)

对我来说,没有内存问题,它几乎可以立即运行

No memory issues for me and it runs almost instantly

这篇关于在R上对大数据进行单次热编码的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆