【问题标题】:R xgboost importance plot with many features具有许多特征的 R xgboost 重要性图
【发布时间】:2017-01-15 06:04:32
【问题描述】:

我正在尝试 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 多个特征,我使用 xgboostmax.depth = 6 和 nrounds = 16。

我得到的重要性图非常混乱,我如何才能只查看前 5 个功能或其他东西。

【问题讨论】:

    标签: r ggplot2 xgboost kaggle


    【解决方案1】:

    查看xgb.plot.importancetop_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]))
    

    【讨论】:

    • 您使用的是哪个版本我收到错误未使用的参数 top_n = 5?
    • 貌似最近加的,CRAN版没有。如果您想获得最新版本,install from their github repository。我将编辑我的答案以获得替代方案。
    【解决方案2】:
    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")
    

    【讨论】:

      猜你喜欢
      • 2019-12-13
      • 1970-01-01
      • 2019-04-15
      • 2016-09-12
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      • 2020-03-17
      • 2022-01-19
      相关资源
      最近更新 更多