我正在使用 Airbnb 的数据,可用 here在 Kaggle 上,使用 XGBoost 模型和 R 中的近 600 个特征来预测用户将预订第一次旅行的国家/地区。通过 50 轮 5 倍交叉验证运行该算法,每次都获得 100% 的准确率。将模型拟合到训练数据并在测试集上进行预测后,我也获得了 100% 的准确率。这些结果不可能是真实的。我的代码肯定有问题,但到目前为止我还没有弄清楚。我在下面包含了我的代码的一部分。是基于这个article 。按照这篇文章(使用文章的数据+复制代码),我收到了类似的结果。然而,将其应用于 Airbnb 的数据时,我始终获得 100% 的准确率。我不知道发生了什么事。我是否错误地使用了 xgboost 软件包?感谢您的帮助和时间。
# set up the data
# train is the data frame of features with the target variable to predict
full_variables <- data.matrix(train[,-1]) # country_destination removed
full_label <- as.numeric(train$country_destination) - 1
# training data
train_index <- caret::createDataPartition(y = train$country_destination, p = 0.70, list = FALSE)
train_data <- full_variables[train_index, ]
train_label <- full_label[train_index[,1]]
train_matrix <- xgb.DMatrix(data = train_data, label = train_label)
# test data
test_data <- full_variables[-train_index, ]
test_label <- full_label[-train_index[,1]]
test_matrix <- xgb.DMatrix(data = test_data, label = test_label)
# 5-fold CV
params <- list("objective" = "multi:softprob",
"num_class" = classes,
eta = 0.3,
max_depth = 6)
cv_model <- xgb.cv(params = params,
data = train_matrix,
nrounds = 50,
nfold = 5,
early_stop_round = 1,
verbose = F,
maximize = T,
prediction = T)
# out of fold predictions
out_of_fold_p <- data.frame(cv_model$pred) %>% mutate(max_prob = max.col(., ties.method = "last"),label = train_label + 1)
head(out_of_fold_p)
# confusion matrix
confusionMatrix(factor(out_of_fold_p$label),
factor(out_of_fold_p$max_prob),
mode = "everything")
通过运行以下代码可以找到我用于此目的的数据示例:
library(RCurl)
x < getURL("https://raw.githubusercontent.com/loshita/Senior_project/master/train.csv")
y <- read.csv(text = x)
最佳答案
如果您使用的是kaggle上提供的train_users_2.csv.zip
,那么问题是您没有从火车数据集中删除country_destination
,因为它位于位置16
而不是 1
。
which(colnames(train) == "country_destination")
#output
16
1
是 id
,它对于每个观察都是唯一的,也应该删除。
length(unique(train[,1)) == nrow(train)
#output
TRUE
当我通过以下修改运行您的代码时:
full_variables <- data.matrix(train[,-c(1, 16)])
library(xgboost)
params <- list("objective" = "multi:softprob",
"num_class" = length(unique(train_label)),
eta = 0.3,
max_depth = 6)
cv_model <- xgb.cv(params = params,
data = train_matrix,
nrounds = 50,
nfold = 5,
early_stop_round = 1,
verbose = T,
maximize = T,
prediction = T)
使用上述设置对 0.12 进行交叉验证时出现测试错误。
out_of_fold_p <- data.frame(cv_model$pred) %>% mutate(max_prob = max.col(., ties.method = "last"),label = train_label + 1)
head(out_of_fold_p[,13:14], 20)
#output
max_prob label
1 8 8
2 12 12
3 12 10
4 12 12
5 12 12
6 12 12
7 12 12
8 12 12
9 8 8
10 12 5
11 12 2
12 2 12
13 12 12
14 12 12
15 12 12
16 8 8
17 8 8
18 12 5
19 8 8
20 12 12
总而言之,您没有从 x
中删除 y
。
编辑:下载真实的训练集并进行测试后,我可以说 5 倍 CV 的准确度确实是 100%。这不仅仅是通过 22 个功能(甚至可能更少)实现的。
model <- xgboost(params = params,
data = train_matrix,
nrounds = 50,
verbose = T,
maximize = T)
该模型在测试集上也获得了 100% 的准确率:
pred <- predict(model, test_matrix)
pred <- matrix(pred, ncol=length(unique(train_label)), byrow = TRUE)
out_of_fold_p <- data.frame(pred) %>% mutate(max_prob = max.col(., ties.method = "last"),label = test_label + 1)
sum(out_of_fold_p$max_prob != out_of_fold_p$label) #0 errors
现在让我们检查哪些功能具有歧视性:
xgb.plot.importance(importance_matrix = xgb.importance(colnames(train_matrix), model))
现在,如果您运行仅具有以下功能的 xgb.cv:
train_matrix <- xgb.DMatrix(data = train_data[,which(colnames(train_data) %in% xgboost::xgb.importance(colnames(train_matrix), model)$Feature)], label = train_label)
set.seed(1)
cv_model <- xgb.cv(params = params,
data = train_matrix,
nrounds = 50,
nfold = 5,
early_stop_round = 1,
verbose = T,
maximize = T,
prediction = T)
您还将获得 100% 的测试折叠准确率
部分原因在于类别的巨大不平衡:
table(train_label)
train_label
0 1 2 3 4 5 6 7 8 9 10 11
3 10 12 13 36 16 19 856 7 73 3 451
事实上,次要类很容易通过 1 个虚拟变量来区分:
gg <- data.frame(train_data[,which(colnames(train_data) %in% xgb.importance(colnames(train_matrix), model)$Feature)], label = as.factor(train_label))
gg %>%
as.tibble() %>%
select(1:9, 11, 12, 15:21, 23) %>%
gather(key, value, 1:18) %>%
ggplot()+
geom_bar(aes(x = label))+
facet_grid(key ~ value) +
theme(strip.text.y = element_text(angle = 90))
基于 22 个最重要特征中 0/1 的分布,在我看来,任何树模型即使不是 100% 的准确率,也能达到相当好的准确率。
人们会认为 0 类和 10 类对于 5 倍 CV 来说是有问题的,因为所有受试者都有可能落入 1 倍,因此模型至少在这种情况下不会知道它们。如果通过随机抽样设计 CV,这将是一种可能性。 xgb.cv 不会发生这种情况:
lapply(cv_model$folds, function(x){
table(train_label[x])})
关于r - XGboost 模型始终获得 100% 的准确率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48697770/