我使用来自xgb.train
包的xgboost
使用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
答案 0 :(得分:2)
这通常对我有用:
importance <- xgb.importance(dimnames(train)[[2]], model = myXGBfit)
答案 1 :(得分:0)
将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)。希望它在同样的情况下帮助任何人!