插入符号XGBtree与稀疏矩阵

时间:2017-09-11 17:09:00

标签: r r-caret

我正在尝试将稀疏矩阵传递给使用xgb训练的插入符号。显然,这可以做到,例如。

library(tidyverse)
library(caret)
library(Matrix)

miris <- select(iris, -Species) %>% as.matrix
sparse_miris <- as(miris, "dgCMatrix")

# sparse matrix
xgb_sparse <- train(x = sparse_miris, y = iris$Species,
                method = "xgbTree", 
                trControl = trainControl(method = "cv", classProbs = TRUE))

这会计算并返回一个模型xgb_sparse。

当我在我的真实数据上尝试这个时,我在每个nround上都会出现重复错误:

Error in as.character(x) : 
  cannot coerce type 'closure' to vector of type 'character'

我的真实数据超过1M记录,而且我基于可共享的较小数据样本重新创建失败。我希望我尽可能地描述我的数据,并且有人可能会认识到这个错误。

# make a sparse matrix for xgb
predictors_sparse <- select(training_data, -c(id, cad, cluster)) %>% as.matrix %>% as("dgCMatrix")

> str(predictors_sparse)
Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
  ..@ i       : int [1:1474634] 47 51 268 360 561 902 1007 1258 1391 1402 ...
  ..@ p       : int [1:96] 0 8044 19465 68596 90202 135268 177588 199513 208843 236602 ...
  ..@ Dim     : int [1:2] 1000000 95
  ..@ Dimnames:List of 2
  .. ..$ : chr [1:1000000] "846520" "2320473" "1203874" "2599268" ...
  .. ..$ : chr [1:95] "patrol" "investig" "assault" "alarm" ...
  ..@ x       : num [1:1474634] 1 1 1 1 1 1 1 2 1 1 ...
  ..@ factors : list()

> str(training_data$cluster)
 Factor w/ 64 levels "cluster1","cluster10",..: 23 23 23 43 23 23 38 15 23 13 ...
> length(training_data$cluster)
[1] 1000000

所以我通过训练一个稀疏矩阵,其中包含1M观测值为x和y训练_data $ cluster,这是一个长度为1M的因子变量。

mod_xgb <- train(
  x = predictors_sparse, y = training_data$cluster,
  method = "xgbTree",
  trControl = train_control,
  na.action = na.pass,
  tuneGrid = xgb_grid,
  summaryFunction = "Kappa"
)

我认为没有必要粘贴我的train_control或tuneGrid对象但请告诉我如果被问到我可以将它们粘贴在这里。

我真的想要共享示例数据,但无法在示例上进行复制,只能在1M的完整数据上进行复制。

我很确定我传递的数据是完整的,没有NA值,我在创建变量predictors_sparse之前检查过。

在插入符号模型的上下文中,任何人都会看到错误吗?

Error in as.character(x) : 
      cannot coerce type 'closure' to vector of type 'character'

0 个答案:

没有答案
相关问题