【问题标题】:Error with caret package - classification v regression插入符号包出错 - 分类 v 回归
【发布时间】:2019-04-19 23:21:04
【问题描述】:

我是一名精算学生,正在为即将在 12 月举行的预测分析考试做准备。练习的一部分是使用 caretxgbTree 的提升来构建模型。见下面代码,caravan 数据集来自ISLR 包:

library(caret)
library(ggplot2)
set.seed(1000)
data.Caravan <- read.csv(file = "Caravan.csv")


data.Caravan$Purchase <- factor(data.Caravan$Purchase)
levels(data.Caravan$Purchase) <- c("No", "Yes")


data.Caravan.train <- data.Caravan[1:1000, ]
data.Caravan.test <- data.Caravan[1001:nrow(data.Caravan), ]
grid <- expand.grid(max_depth = c(1:7),
                    nrounds = 500,
                    eta =  c(.01, .05, .01),
                    colsample_bytree = c(.5, .8),
                    gamma = 0,
                    min_child_weight = 1,
                    subsample = .6)

control <- trainControl(method = "cv", 
                        number = 4,
                        classProbs = TRUE,
                        sampling = c("up", "down"))
              
caravan.boost <- train(formula = Purchase ~ .,
                       data =  data.Caravan.train, 
                       method = "xgbTree", 
                       metric = "Accuracy",
                       trControl = control, 
                       tuneGrid = grid)

expand.gridtrainControl 中的定义由问题指定,但我不断收到错误消息:

错误:采样方法仅用于分类问题

如果我从 trainControl 中删除采样方法,我会收到一个新错误,指出“度量精度不适用于回归模型”。如果我删除准确度指标,我会收到一条错误提示

无法计算回归的类概率”和“名称错误(res$trainingData) %in% as.character(form[[2]]):参数“form”丢失,没有默认值”

最终的问题是插入符号将问题定义为回归,而不是分类,即使目标变量设置为因子变量并且classProbs 设置为 TRUE。有人能解释一下如何告诉插入符号运行分类而不是回归吗?

【问题讨论】:

  • 您能否将dput(head(data.Caravan, 20)) 的结果添加到您的问题中?这将为我们提供源数据的前 20 条记录。这样我们就可以使用您的数据运行您的代码。欲了解更多信息,请阅读reproducible examples上的这篇文章
  • @phiver 感谢您的回复。数据集有 86 个字段。这可能太多了,无法粘贴在这里。大篷车数据集在 ISLR 包中可用。
  • @missuse 感谢您的评论。我不确定你不使用公式界面是什么意思,所以我尝试了几次不同的迭代,发现了一些有用的东西。我唯一改变的是“公式=购买〜”。到“购买~。”它奏效了。我不知道为什么。你怎么知道这样做的?

标签: r r-caret


【解决方案1】:

caret::train没有formula参数,而是一个form参数,在其中指定公式。所以例如这个工作:

caravan.boost <- train(form = Purchase ~ .,
                       data =  data.Caravan.train, 
                       method = "xgbTree", 
                       metric = "Accuracy",
                       trControl = control, 
                       tuneGrid = grid)

#output:
eXtreme Gradient Boosting 

1000 samples
  85 predictor
   2 classes: 'No', 'Yes' 

No pre-processing
Resampling: Cross-Validated (4 fold) 
Summary of sample sizes: 751, 749, 750, 750 
Addtional sampling using up-sampling

Resampling results across tuning parameters:

  eta   max_depth  colsample_bytree  Accuracy   Kappa     
  0.01  1          0.5               0.7020495  0.10170007
  0.01  1          0.8               0.7100335  0.09732773
  0.01  2          0.5               0.7730581  0.12361444
  0.01  2          0.8               0.7690620  0.11293561
  0.01  3          0.5               0.8330506  0.14461709
  0.01  3          0.8               0.8290146  0.06908344
  0.01  4          0.5               0.8659949  0.07396586
  0.01  4          0.8               0.8749790  0.07451637
  0.01  5          0.5               0.8949792  0.07599005
  0.01  5          0.8               0.8949792  0.07525191
  0.01  6          0.5               0.9079873  0.09766492
  0.01  6          0.8               0.9099793  0.10420720
  0.01  7          0.5               0.9169833  0.11769151
  0.01  7          0.8               0.9119753  0.10873268
  0.05  1          0.5               0.7640699  0.08281792
  0.05  1          0.8               0.7700580  0.09201503
  0.05  2          0.5               0.8709909  0.09034807
  0.05  2          0.8               0.8739990  0.10440898
  0.05  3          0.5               0.9039792  0.12166348
  0.05  3          0.8               0.9089832  0.11850402
  0.05  4          0.5               0.9149793  0.11602447
  0.05  4          0.8               0.9119713  0.11207786
  0.05  5          0.5               0.9139633  0.11853793
  0.05  5          0.8               0.9159754  0.11968085
  0.05  6          0.5               0.9219794  0.11744643
  0.05  6          0.8               0.9199794  0.12803204
  0.05  7          0.5               0.9179873  0.08701058
  0.05  7          0.8               0.9179793  0.10702619

Tuning parameter 'nrounds' was held constant at a value of 500
Tuning parameter 'gamma' was held constant
 at a value of 0
Tuning parameter 'min_child_weight' was held constant at a value of 1
Tuning
 parameter 'subsample' was held constant at a value of 0.6
Accuracy was used to select the optimal model using the largest value.
The final values used for the model were nrounds = 500, max_depth = 6, eta = 0.05, gamma =
 0, colsample_bytree = 0.5, min_child_weight = 1 and subsample = 0.6.

您还可以使用其中的非公式界面在其中指定xy单独指定:

caravan.boost <- train(x = data.Caravan.train[,-ncol(data.Caravan.train)],
                       y =  data.Caravan.train$Purchase, 
                       method = "xgbTree", 
                       metric = "Accuracy",
                       trControl = control, 
                       tuneGrid = grid)

请注意,这两种规范的方式并不总是在x以来的因子变量中产生相同的结果,因为大多数算法为987654329 @ 987654329。

以获取数据:

library(ISLR)
data(Caravan)

【讨论】:

    猜你喜欢
    • 2018-04-17
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 2019-01-16
    • 2020-10-22
    • 2018-06-08
    • 2021-06-29
    相关资源
    最近更新 更多