首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用R在randomForest上执行交叉验证

用R在randomForest上执行交叉验证
EN

Stack Overflow用户
提问于 2014-06-23 11:11:26
回答 1查看 3.2K关注 0票数 3

我正在使用randomForestR来训练分类模型。为了将其与其他分类器进行比较,我需要一种方法来显示Weka中相当冗长的交叉验证方法所提供的所有信息。因此,R脚本应该输出一些类似于from Weka的内容。

  1. 是否有方法通过RWeka验证R模型以生成这些度量?
  2. 如果不是,随机森林上的交叉验证是如何在R中完成的?
  3. 这里是否可以使用rfcv包中的randomForest?我没能让它开始工作。

我确实知道,在randomForest中使用的OOB是某种交叉验证。但我需要一个合适的比较的全部信息。

到目前为止,我尝试使用R的是b。但是,由于缺少值,代码也会在我的安装程序c上产生一个错误。

所以,你能帮我做交叉验证吗?

附录

A Weka:

代码语言:javascript
复制
=== Stratified cross-validation ===
=== Summary ===

Correctly Classified Instances        3059               96.712  %
Incorrectly Classified Instances       104                3.288  %
Kappa statistic                          0.8199
Mean absolute error                      0.1017
Root mean squared error                  0.1771
Relative absolute error                 60.4205 %
Root relative squared error             61.103  %
Coverage of cases (0.95 level)          99.6206 %
Mean rel. region size (0.95 level)      78.043  %
Total Number of Instances             3163     

=== Detailed Accuracy By Class ===

                 TP Rate  FP Rate  Precision  Recall   F-Measure  MCC      ROC Area  PRC Area  Class
                 0,918    0,028    0,771      0,918    0,838      0,824    0,985     0,901     sick-euthyroid
                 0,972    0,082    0,991      0,972    0,982      0,824    0,985     0,998     negative
Weighted Avg.    0,967    0,077    0,971      0,967    0,968      0,824    0,985     0,989     

=== Confusion Matrix ===

    a    b   <-- classified as
  269   24 |    a = sick-euthyroid
   80 2790 |    b = negative

B迄今的“守则”:

代码语言:javascript
复制
library(randomForest) #randomForest() and rfImpute()
library(foreign) # read.arff()
library(caret) # train() and trainControl()

nTrees <- 2 # 200
myDataset <- 'D:\\your\\directory\\SE.arff' # http://hakank.org/weka/SE.arff

mydb = read.arff(myDataset)
mydb.imputed <- rfImpute(class ~ ., data=mydb, ntree = nTrees, importance = TRUE)
myres.rf <- randomForest(class ~ ., data=mydb.imputed, ntree = nTrees, importance = TRUE)
summary(myres.rf)

# specify type of resampling to 10-fold CV
fitControl <- trainControl(method = "rf",number = 10,repeats = 10)
set.seed(825)

# deal with NA | NULL values in categorical variables
#mydb.imputed[is.na(mydb.imputed)] <- 1
#mydb.imputed[is.null(mydb.imputed)] <- 1

rfFit <- train(class~ ., data=mydb.imputed,
             method = "rf",
             trControl = fitControl,
             ## This last option is actually one
             ## for rf() that passes through
             ntree = nTrees, importance = TRUE, na.action = na.omit)
rfFit

错误是:

代码语言:javascript
复制
Error in names(resamples) <- gsub("^\\.", "", names(resamples)) : 
  attempt to set an attribute on NULL

使用traceback()

代码语言:javascript
复制
5: nominalTrainWorkflow(x = x, y = y, wts = weights, info = trainInfo, 
       method = models, ppOpts = preProcess, ctrl = trControl, lev = classLevels, 
       ...)
4: train.default(x, y, weights = w, ...)
3: train(x, y, weights = w, ...)
2: train.formula(class~ ., data = mydb.imputed, method = "rf", 
       trControl = fitControl, ntree = nTrees, importance = TRUE, 
       sampsize = rep(minorityClassNum, 2), na.action = na.omit)
1: train(class~ ., data = mydb.imputed, method = "rf", trControl = fitControl, 
       ntree = nTrees, importance = TRUE, sampsize = rep(minorityClassNum, 
           2), na.action = na.omit) at #39

通过sessionInfo()提供的c版本信息

代码语言:javascript
复制
R version 3.1.0 (2014-04-10)
Platform: i386-w64-mingw32/i386 (32-bit)

[...]

other attached packages:
 [1] e1071_1.6-3        caret_6.0-30       ggplot2_1.0.0      foreign_0.8-61     randomForest_4.6-7 DMwR_0.4.1        
 [7] lattice_0.20-29    JGR_1.7-16         iplots_1.1-7       JavaGD_0.6-1       rJava_0.9-6
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-23 11:59:17

我不知道weka,但我在R中做了randomForest建模,我一直用R中的预测函数来做这件事。

试着使用这个函数

代码语言:javascript
复制
predict(Model,data)

将输出与原始值绑定,并使用table命令获取混淆矩阵。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24364430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档