R:使用bigmemory库进行randomForest分类

时间:2012-04-29 05:29:13

标签: r machine-learning data-mining r-bigmemory

有人能够使用randomForest和bigmemory库设置分类(不是回归)。我知道'公式方法'不能被使用,我们不得不求助于“x =预测器,y =响应方法”。看来大内存库无法处理具有分类值的响应向量(毕竟它是一个矩阵。在我的情况下,我有两个级别,都表示为字符。

根据bigmemory文档......“数据框将字符向量转换为因子,然后将所有因子转换为数字因子级别”

任何建议的解决方法,以使randomForest分类与bigmemory一起使用?

#EXAMPLE to problem
library(randomForest)
library(bigmemory)
# Removing any extra objects from my workspace (just in case)
rm(list=ls())

#first small matrix
small.mat <- matrix(sample(0:1,5000,replace = TRUE),1000,5)
colnames(small.mat) <- paste("V",1:5,sep = "")
small.mat[,5] <- as.factor(small.mat[,5]) 
small.rf <- randomForest(V5 ~ .,data = small.mat, mtry=2, do.trace=100)
print(small.rf)
small.result <- matrix(0,1000,1)
small.result <- predict(small.rf, data=small.mat[,-5])

#now small dataframe Works!
small.mat <- matrix(sample(0:1,5000,replace = TRUE),1000,5)
colnames(small.mat) <- paste("V",1:5,sep = "")
small.data <- as.data.frame(small.mat)

small.data[,5] <- as.factor(small.data[,5]) 
small.rf <- randomForest(V5 ~ .,data = small.data, mtry=2, do.trace=100)
print(small.rf)
small.result <- matrix(0,1000,1)
small.result <- predict(small.rf, data=small.data[,-5])


#then big matrix Classification Does NOT Work :-(
#----------------****************************----
big.mat <- as.big.matrix(small.mat, type = "integer")
#Line below throws error, "cannot coerce class 'structure("big.matrix", package = "bigmemory")' into a data.frame"
big.rf <- randomForest(V5~.,data = big.mat, do.trace=10)

#Runs without error but only regression
big.rf <- randomForest(x = big.mat[,-5], y = big.mat[,5], mtry=2, do.trace=100)
print(big.rf)
big.result <- matrix(0,1000,1)
big.result <- predict(big.rf, data=big.mat[,-5])

1 个答案:

答案 0 :(得分:1)

bigrf包可能有所帮助。目前,它支持使用有限数量的功能进行分类。