【问题标题】:Errors with createGrid for rf (randomForest) when using caret使用插入符号时用于 rf (randomForest) 的 createGrid 出错
【发布时间】:2013-01-28 04:23:51
【问题描述】:

当我尝试使用caret 为训练创建一个参数网格时,我遇到了各种错误:

> my_grid <- createGrid("rf")
Error in if (p <= len) { : argument is of length zero

> my_grid <- createGrid("rf", 4)
Error in if (p <= len) { : argument is of length zero

> my_grid <- createGrid("rf", len=4)                                        
Error in if (p <= len) { : argument is of length zero

createGrid 的文档说:

This function creates a data frame that contains a grid of
     complexity parameters specific methods.
Usage:
       createGrid(method, len = 3, data = NULL)
Arguments:
  method: a string specifying which classification model to use. See
          'train' for a full list.
     len: an integer specifying the number of points on the grid for
          each tuning parameter.
    data: the training data (only needed in the case where the 'method'
          is 'cforest', 'earth', 'bagEarth', 'fda', 'bagFDA', 'rpart',
          'svmRadial', 'pam', 'lars2', 'rf' or 'pls'). The outcome
          should be in a column called '.outcome'.

并给出以下正确工作的例子:

 createGrid("rda", 4)
 createGrid("lm")
 createGrid("nnet")
 
 ## data needed for SVM with RBF:
 ## Not run:
 
 tmp <- iris
 names(tmp)[5] <- ".outcome"
 head(tmp)
 createGrid("svmRadial", data = tmp, len = 4)
 ## End(Not run)

有了这个,我做错了什么?

后续问题:

len 作为createGrid 的自变量和train 的自变量中的tuneLength 之间有什么联系? len 和tuneLength 可以一起使用吗?他们是什么关系?

其他有用的话题:

如果有帮助,这里有一个线程描述了如何在caret 中使用createGrid 和train:caret::train: specify model-generation-parameters

【问题讨论】:

    标签: r random-forest


    【解决方案1】:

    您从示例中提取的代码对我来说很好(并注意它解决了您在 Rhelp 上发布时存在的问题):

    tmp <- iris
     names(tmp)[5] <- ".outcome"
     head(tmp)
     createGrid("svmRadial", data = tmp, len = 4)
    #-------
         .sigma   .C
    1 0.7500934 0.25
    2 0.7500934 0.50
    3 0.7500934 1.00
    4 0.7500934 2.00
    

    编辑:

    >  createGrid("rf", data = tmp, len = 4)
    randomForest 4.6-7
    Type rfNews() to see new features/changes/bug fixes.
    
    Attaching package: ‘randomForest’
    
    The following object(s) are masked from ‘package:Hmisc’:
    
        combine
    
    note: only 3 unique complexity parameters in default grid. Truncating the grid to 3 .
    
      .mtry
    1     2
    2     3
    3     4
    

    我再说一遍:还有什么问题?

    【讨论】:

    • 谢谢。请参阅我在 OP 顶部尝试的示例。我没有说文档中提供的示例不起作用。
    • 好的(+1)谢谢!我不知道我应该将数据传递给 rf 。文档的使用部分说data=NULL,这让我觉得它是可选的。顺便说一句,你知道(1)train 中的参数tuneLength 和(2)通过tuneGrid 传递给train 的网格之间的联系吗? (见后续问题)
    • 当我阅读文档时,不会有任何连接。它说其中一个或另一个用于构造候选参数。 tuneGrid 可让您指定位置,而tuneLength 可让您指定其编号。
    • createGrid() 不再存在于插入符号中
    • 是的。这已经在 SO 上注明:stackoverflow.com/questions/22275173/… 但是,如果您想要代码,它仍然在这里:r-forge.r-project.org/scm/viewvc.php/pkg/caret/R/… ...或... github.com/dalpozz/caretUtils/blob/master/createGrid.R
    猜你喜欢
    • 2020-01-16
    • 2015-07-17
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    相关资源
    最近更新 更多