簡體   English   中英

插入符號包錯誤 - 分類 v 回歸

[英]Error with caret package - classification v regression

我是一名精算專業的學生,​​正在為即將在 12 月舉行的預測分析考試做准備。 練習的一部分是使用帶有caretxgbTree boosting 來構建模型。 看下面的代碼,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。 有人可以解釋如何告訴插入符號運行分類而不是回歸嗎?

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有因子變量時,這兩種規范方式並不總是產生相同的結果,因為對於大多數算法,公式接口調用model.matrix

要獲取數據:

library(ISLR)
data(Caravan)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM