Lime:glmnet(x[,c(features,j))中的错误,x应该是具有2列或更多列的矩阵

Lime:glmnet(x[,c(features,j))中的错误,x应该是具有2列或更多列的矩阵,r,text,xgboost,text-analysis,lime,R,Text,Xgboost,Text Analysis,Lime,我遵循这个例子在一个有监督的文本模型上使用lime 我刚刚更改了get_matrix函数以创建dtm。此新函数适用于此链接中示例中的数据,但不适用于我的真实数据。我得到以下错误: Error in glmnet(x[, c(features, j), drop = FALSE], y, weights = weights, : x should be a matrix with 2 or more columns 我使用的代码如下-数据和分析仅用于此目的,但复制了我在真实数据上得到的错误(我

我遵循这个例子在一个有监督的文本模型上使用lime

我刚刚更改了get_matrix函数以创建dtm。此新函数适用于此链接中示例中的数据,但不适用于我的真实数据。我得到以下错误:

Error in glmnet(x[, c(features, j), drop = FALSE], y, weights = weights,  : x should be a matrix with 2 or more columns
我使用的代码如下-数据和分析仅用于此目的,但复制了我在真实数据上得到的错误(我有1000个文本文档,而不是10个):


data我也遇到了同样的问题。似乎错误来自n_功能。我增加了n_功能,这对我很有效。请为n_功能尝试不同的数字,例如>6,因为它使用了不同的功能选择方法,如中所述


n\u features是用于每个解释的功能数。请参阅。

我得到了相同的错误,并且没有在xgboost模型上运行lime解释程序。希望有人能提供帮助。
 data<-data.frame(articles = c("Prince Harry proposed to Meghan", "Football transfer rumours Chelsea David Luiz", "Football transfer rumours Chelsea David Luiz", 
                    "World Cup team by team guide", "Destiny free trial goes live today", "What happens today ahead of crucial vote",
                    "Story image for sport news football from BBC Sport", "Premier League news conferences", "What is Meghan Markles engagement ring", "Harry and Megan")
       , topic = c("other", "sport", "sport", "sport", "other", "other", "sport", "sport", "other", "other"))


      data$articles<-as.character(data$articles)
      data$topic<-as.character(data$topic)
      data_train<-data[1:6,]
      data_test<-data[6:10,]

      my_stop_word <-c (stopwords(), "one", "two", "three")
      get_matrix <- function(text) {
        it <- itoken(text, tolower, progressbar = FALSE)
        vocab2 = create_vocabulary(it, stopwords = my_stop_word)
        vectorizer = vocab_vectorizer(vocab2)
        create_dtm(it, vectorizer = vectorizer)
      }
      dtm_train = get_matrix(data_train$articles)
      xgb_model <- xgb.train(list(max_depth = 7, eta = 0.1, objective = "binary:logistic",
                                  eval_metric = "error", nthread = 1),
                             xgb.DMatrix(dtm_train, label = data_train$topic == "sport"),
                             nrounds = 50)

      sentences <- head(data_test[data_test$topic == "sport", "articles"], 1)
      explainer <- lime(data_test$articles, xgb_model, get_matrix)
      explanations <- explain(sentences, explainer, n_labels = 1, n_features = 2)