我无法打开您为数据集和输出提供的链接。不过,我使用的是您提供的相同链接
https://cran.r-project.org/web/packages/lime/vignettes/Understanding_lime.html
. 我使用text2vec,因为它是在链接中,和xgboost包进行分类;这对我很有用。要显示更多功能,您可能需要在“解释”功能中增加n挈U功能的值,请参阅
https://www.rdocumentation.org/packages/lime/versions/0.4.0/topics/explain
.
library(lime)
library(xgboost) # the classifier
library(text2vec) # used to build the BoW matrix
# load data
data(train_sentences, package = "lime") # from lime
data(test_sentences, package = "lime") # from lime
# Tokenize data
get_matrix <- function(text) {
it <- text2vec::itoken(text, progressbar = FALSE)
# use the following lines if you want to prune vocabulary
# vocab <- create_vocabulary(it, c(1L, 1L)) %>%
# prune_vocabulary(term_count_min = 10, doc_proportion_max = 0.2)
# vectorizer <- vocab_vectorizer(vocab )
# there is no option to prune the vocabulary, but it is very fast for big data
vectorizer <- hash_vectorizer(hash_size = 2 ^ 10, ngram = c(1L, 1L))
text2vec::create_dtm(it,vectorizer = vectorizer) # hash_vectorizer())
}
# BoW matrix generation
# features should be the same for both dtm_train and dtm_test
dtm_train <- get_matrix(train_sentences$text)
dtm_test <- get_matrix(test_sentences$text)
# xgboost for classification
param <- list(max_depth = 7,
eta = 0.1,
objective = "binary:logistic",
eval_metric = "error",
nthread = 1)
xgb_model <-xgboost::xgb.train(
param,
xgb.DMatrix(dtm_train, label = train_sentences$class.text == "OWNX"),
nrounds = 100
)
# prediction
predictions <- predict(xgb_model, dtm_test) > 0.5
test_labels <- test_sentences$class.text == "OWNX"
# Accuracy
print(mean(predictions == test_labels))
# what are the most important words for the predictions.
n_features <- 5 # number of features to display
sentence_to_explain <- head(test_sentences[test_labels,]$text, 6)
explainer <- lime::lime(sentence_to_explain, model = xgb_model,
preprocess = get_matrix)
explanation <- lime::explain(sentence_to_explain, explainer, n_labels = 1,
n_features = n_features)
#
explanation[, 2:9]
# plot
lime::plot_features(explanation)
在您的代码中,当在train\u数据集上应用时,将在下面的行中创建NAs。请检查以下代码。
dataset$Liked = factor(dataset$Liked, levels = c(0, 1))
删除级别或将级别更改为标签对我很有用。
请检查您的数据结构,并确保您的数据不是由于那些NAs的零矩阵,或者它不是太稀疏。它也可能导致问题,因为它无法找到前n个功能。