尝试在 xgboost 包中的 r 中使用 xgb.importance 时出错
Error when trying to use xgb.importance in r from xgboost package
我使用 xgboost
包中的 xgb.train
使用 R 训练了一个模型。我正在尝试了解该模型的特征的重要性。我不断收到以下错误:
Error in fread(paste(longString, collapse = ""), sep = "\n", header = F) :
4 arguments passed to .Internal(nchar) which requires 3
我在这里分享我的代码以防有帮助:
# Make the xgb.DMatrix
train <- sparse.model.matrix(TARGET ~ ., data = train)
dtrain <- xgb.DMatrix(data=train, label=train.y)
# Set the desired parameters
myParams <- list(objective = "binary:logistic",
booster = "gbtree",
eval_metric = "auc",
eta = 0.02,
max_depth = 5,
min_child_weight = 12,
gamma = 0.1,
subsample = 0.7,
colsample_bytree = 0.7)
# Train model
myXGBfit <- xgb.train(params = myParams,
data = dtrain,
nrounds = 369,
verbose = 1,
watchlist = watchlist,
maximize = FALSE)
# Calculate feature importance matrix
importance <- xgb.importance(feature_names = train@Dimnames[[2]], model = myXGBfit)
我曾尝试使用 xgboost
而不是 xgb.train
来训练模型,但是在尝试获取特征重要性矩阵时我遇到了同样的错误。我还阅读了 xgboost
包的文档并在线搜索帮助但无法找到解决方案。我做错了什么?
如果有用,sessionInfo()
提供以下信息:
R version 3.2.0 (2015-04-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] pROC_1.8 Matrix_1.2-4 xgboost_0.4-3
loaded via a namespace (and not attached):
[1] plyr_1.8.3 magrittr_1.5 tools_3.2.0 yaml_2.1.13 Rcpp_0.12.3
[6] stringi_1.0-1 grid_3.2.0 knitr_1.12.3 data.table_1.9.6 stringr_1.0.0
[11] chron_2.3-47 lattice_0.20-33
这通常对我有用:
importance <- xgb.importance(dimnames(train)[[2]], model = myXGBfit)
将 R 版本从 3.2.0 更新到 3.2.4 后问题已解决。 运行 version
命令产生以下信息:
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status Revised
major 3
minor 2.4
year 2016
month 03
day 16
svn rev 70336
language R
version.string R version 3.2.4 Revised (2016-03-16 r70336)
nickname Very Secure Dishes
为了更新 R 版本,我使用了 installr
包和 运行 来自 R GUI 的 updateR()
命令(仅适用于 Windows)。希望能帮到和我一样情况的人!
我使用 xgboost
包中的 xgb.train
使用 R 训练了一个模型。我正在尝试了解该模型的特征的重要性。我不断收到以下错误:
Error in fread(paste(longString, collapse = ""), sep = "\n", header = F) :
4 arguments passed to .Internal(nchar) which requires 3
我在这里分享我的代码以防有帮助:
# Make the xgb.DMatrix
train <- sparse.model.matrix(TARGET ~ ., data = train)
dtrain <- xgb.DMatrix(data=train, label=train.y)
# Set the desired parameters
myParams <- list(objective = "binary:logistic",
booster = "gbtree",
eval_metric = "auc",
eta = 0.02,
max_depth = 5,
min_child_weight = 12,
gamma = 0.1,
subsample = 0.7,
colsample_bytree = 0.7)
# Train model
myXGBfit <- xgb.train(params = myParams,
data = dtrain,
nrounds = 369,
verbose = 1,
watchlist = watchlist,
maximize = FALSE)
# Calculate feature importance matrix
importance <- xgb.importance(feature_names = train@Dimnames[[2]], model = myXGBfit)
我曾尝试使用 xgboost
而不是 xgb.train
来训练模型,但是在尝试获取特征重要性矩阵时我遇到了同样的错误。我还阅读了 xgboost
包的文档并在线搜索帮助但无法找到解决方案。我做错了什么?
如果有用,sessionInfo()
提供以下信息:
R version 3.2.0 (2015-04-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] pROC_1.8 Matrix_1.2-4 xgboost_0.4-3
loaded via a namespace (and not attached):
[1] plyr_1.8.3 magrittr_1.5 tools_3.2.0 yaml_2.1.13 Rcpp_0.12.3
[6] stringi_1.0-1 grid_3.2.0 knitr_1.12.3 data.table_1.9.6 stringr_1.0.0
[11] chron_2.3-47 lattice_0.20-33
这通常对我有用:
importance <- xgb.importance(dimnames(train)[[2]], model = myXGBfit)
将 R 版本从 3.2.0 更新到 3.2.4 后问题已解决。 运行 version
命令产生以下信息:
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status Revised
major 3
minor 2.4
year 2016
month 03
day 16
svn rev 70336
language R
version.string R version 3.2.4 Revised (2016-03-16 r70336)
nickname Very Secure Dishes
为了更新 R 版本,我使用了 installr
包和 运行 来自 R GUI 的 updateR()
命令(仅适用于 Windows)。希望能帮到和我一样情况的人!