4

我有一个非常不平衡的数据集。为了解决这个问题,我分别尝试了不同的类不平衡技术:downSample类权重阈值调整。其中,阈值调整效果最差。单独使用 downSample 或单独使用类权重,我没有设法获得足够好的结果:要么有太多的 FalsePositives 要么 FalseNegatives。所以我想将这两种技术结合起来。这是我累的:

# produce some re-producible imbalanced data
set.seed(12345)
y <- as.factor(sample(c("M", "F"),
                      prob = c(0.1, 0.9),
                      size = 10000,
                      replace = TRUE))


x <- rnorm(10000)


DATA <- data.frame(y = as.factor(y), x)

set.seed(12345)
folds <- createFolds(dataSet$y, k = 10, 
                     list = TRUE, returnTrain = TRUE)

# class weights 
k <- 0.5
classWeights <- ifelse(DATA$y == "M",
                       (1/table(DATA$y)[1]) * k,
                       (1/table(DATA$y)[2]) * (1-k))

所以当我不把sampling论点放在controlTrain

# select algorithm
algorithm <- "bayesglm"

# train parameters
set.seed(12345)
traincontrol <- trainControl(method = "loocv", # resampling method
                             number = 10,
                             index = folds,
                             classProbs = TRUE, 
                             summaryFunction = twoClassSummary,
                             savePredictions = TRUE,
                             # sampling = "down"
                             )

fitModel <- train(y ~ .,
                  data = DATA, 
                  trControl = traincontrol,
                  method = algorithm,
                  metric = "ROC",
                  weights = classWeights,
                  )

它有效并且没有错误。但是当我将 trainControl 的采样参数添加为

# train parameters
set.seed(12345)
traincontrol <- trainControl(method = "loocv", # resampling method
                             number = 10,
                             index = folds,
                             classProbs = TRUE, 
                             summaryFunction = twoClassSummary,
                             savePredictions = TRUE,
                             sampling = "down"
                             )

fitModel <- train(y ~ .,
                  data = DATA, 
                  trControl = traincontrol,
                  method = algorithm,
                  metric = "ROC",
                  weights = classWeights,
                  )

我得到这个可以理解的错误:

Error in model.frame.default(formula = .outcome ~ ., data = list(x = c(-0.0640913631047556,  : 
  variable lengths differ (found for '(weights)')
In addition: There were 11 warnings (use warnings() to see them)
Timing stopped at: 0.112 0.001 0.115

有没有办法做到这一点caret?提前谢谢了。

4

0 回答 0