具有许多功能的 R xgboost 重要性图
R xgboost importance plot with many features
我正在尝试 Kaggle 房价挑战:https://www.kaggle.com/c/house-prices-advanced-regression-techniques
这是我写的脚本
train <- read.csv("train.csv")
train$Id <- NULL
previous_na_action = options('na.action')
options(na.action = 'na.pass')
sparse_matrix <- sparse.model.matrix(SalePrice~.-1,data = train)
options(na.action = previous_na_action)
model <- xgboost(data = sparse_matrix, label = train$SalePrice, missing = NA, max.depth = 6, eta = 0.3, nthread = 4, nrounds = 16, verbose = 2, objective = "reg:linear")
importance <- xgb.importance(feature_names = sparse_matrix@Dimnames[[2]], model = model)
print(xgb.plot.importance(importance_matrix = importance))
数据有 70 多个特征,我使用了 xgboost
,max.depth
= 6,nrounds
= 16。
我得到的重要性图非常混乱,我如何才能只查看前 5 个特征或其他内容。
查看 xgb.plot.importance
的 top_n
参数。它完全符合您的要求。
# Plot only top 5 most important variables.
print(xgb.plot.importance(importance_matrix = importance, top_n = 5))
编辑:仅适用于 xgboost 的开发版本。替代方法是这样做:
print(xgb.plot.importance(importance_matrix = importance[1:5]))
xgbImp1 <- xgb.importance(model = model)
这将确定您模型的重要特征。
xgbImp1 <- xgbImp1 %>% mutate(rank = dense_rank(desc(Gain)))
这将为每个功能提供排名,因此我们可以将其更改为前 5、10、15 和 20。
ggplot(data=xgbImp1[which(xgbImp1$rank <= 20),], aes(x = reorder(Feature, -Gain), y = Gain)) +
geom_bar(stat="identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(title = "XG Boosted Feature Importance (Top 20)", x = "Features", y = "Information Gain")
我正在尝试 Kaggle 房价挑战:https://www.kaggle.com/c/house-prices-advanced-regression-techniques
这是我写的脚本
train <- read.csv("train.csv")
train$Id <- NULL
previous_na_action = options('na.action')
options(na.action = 'na.pass')
sparse_matrix <- sparse.model.matrix(SalePrice~.-1,data = train)
options(na.action = previous_na_action)
model <- xgboost(data = sparse_matrix, label = train$SalePrice, missing = NA, max.depth = 6, eta = 0.3, nthread = 4, nrounds = 16, verbose = 2, objective = "reg:linear")
importance <- xgb.importance(feature_names = sparse_matrix@Dimnames[[2]], model = model)
print(xgb.plot.importance(importance_matrix = importance))
数据有 70 多个特征,我使用了 xgboost
,max.depth
= 6,nrounds
= 16。
我得到的重要性图非常混乱,我如何才能只查看前 5 个特征或其他内容。
查看 xgb.plot.importance
的 top_n
参数。它完全符合您的要求。
# Plot only top 5 most important variables.
print(xgb.plot.importance(importance_matrix = importance, top_n = 5))
编辑:仅适用于 xgboost 的开发版本。替代方法是这样做:
print(xgb.plot.importance(importance_matrix = importance[1:5]))
xgbImp1 <- xgb.importance(model = model)
这将确定您模型的重要特征。
xgbImp1 <- xgbImp1 %>% mutate(rank = dense_rank(desc(Gain)))
这将为每个功能提供排名,因此我们可以将其更改为前 5、10、15 和 20。
ggplot(data=xgbImp1[which(xgbImp1$rank <= 20),], aes(x = reorder(Feature, -Gain), y = Gain)) +
geom_bar(stat="identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
labs(title = "XG Boosted Feature Importance (Top 20)", x = "Features", y = "Information Gain")