R:LIME在不同的情况下返回不同特征号的错误

时间:2018-05-10 13:20:34

标签: r text-classification quanteda lime

我正在建立一个克林顿的文本分类器。特朗普推文(数据可在Kaggle上找到)。

我正在使用quanteda包进行EDA和建模:

library(dplyr)
library(stringr)
library(quanteda)
library(lime)

#data prep
tweet_csv <- read_csv("tweets.csv")
tweet_data <- tweet_csv %>% 
  select(author = handle,
     text,
     retweet_count,
     favorite_count,
     source_url,
     timestamp = time) %>% 
mutate(date = as_date(str_sub(timestamp, 1, 10)),
     hour = hour(hms(str_sub(timestamp, 12, 19))),
     tweet_num = row_number()) %>% 
select(-timestamp)

# creating corpus and dfm
tweet_corpus <- corpus(tweet_data)

edited_dfm <- dfm(tweet_corpus, remove_url = TRUE, remove_punct = TRUE,     remove = stopwords("english"))

set.seed(32984)
trainIndex <- sample.int(n = nrow(tweet_csv), size =     floor(.8*nrow(tweet_csv)), replace = F)

train_dfm <- edited_dfm[as.vector(trainIndex), ]
train_raw <- tweet_data[as.vector(trainIndex), ]
train_label <- train_raw$author == "realDonaldTrump"

test_dfm <- edited_dfm[-as.vector(trainIndex), ]
test_raw <- tweet_data[-as.vector(trainIndex), ]
test_label <- test_raw$author == "realDonaldTrump"

# making sure train and test sets have the same features
test_dfm <- dfm_select(test_dfm, train_dfm)

# using quanteda's NB model
nb_model <- quanteda::textmodel_nb(train_dfm, train_labels)
nb_preds <- predict(nb_model, test_dfm) 


# defining textmodel_nb as classification model
class(nb_model)

model_type.textmodel_nb_fitted <- function(x, ...) {
  return("classification")
}

# a wrapper-up function for data preprocessing

get_matrix <- function(df){
  corpus <- corpus(df)
  dfm <- dfm(corpus, remove_url = TRUE, remove_punct = TRUE, remove = stopwords("english"))
}

然后我定义了解释器 - 这里没有问题:

explainer <- lime(train_raw[1:5],
              model = nb_model,
              preprocess = get_matrix)

但是当我运行解释器时,即使在与explainer完全相同的数据集上,我也会收到错误:

explanation <- lime::explain(train_raw[1:5], 
                              explainer, 
                              n_labels = 1,
                              n_features = 6,
                              cols = 2,
                              verbose = 0)
  

predict.textmodel_nb_fitted(x,newdata = newdata,type = type,:     newdata中的功能集与训练集中的功能集不同

是否与quanteda和dfms有关?老实说,我不明白为什么会这样。任何帮助都会很棒,谢谢!

1 个答案:

答案 0 :(得分:2)

我们可以将错误跟踪到predict_model,调用predict.textmodel_nb_fitted(我只使用train_raw的前10行来加速计算):

traceback()
# 7: stop("feature set in newdata different from that in training set")
# 6: predict.textmodel_nb_fitted(x, newdata = newdata, type = type, 
#        ...)
# 5: predict(x, newdata = newdata, type = type, ...)
# 4: predict_model.default(explainer$model, case_perm, type = o_type)
# 3: predict_model(explainer$model, case_perm, type = o_type)
# 2: explain.data.frame(train_raw[1:10, 1:5], explainer, n_labels = 1, 
#        n_features = 5, cols = 2, verbose = 0)
# 1: lime::explain(train_raw[1:10, 1:5], explainer, n_labels = 1, 
#        n_features = 5, cols = 2, verbose = 0)

问题是predict.textmodel_nb_fitted需要dfm,而不是数据帧。例如,predict(nb_model, test_raw[1:5])在新数据中为您提供与训练集中不同的&#34;功能集&#34;错误。但是,explain将数据框作为其x参数。

解决方案是为textmodel_nb_fitted编写自定义predict_model方法,在调用predict.textmodel_nb_fitted之前执行必要的对象转换:

predict_model.textmodel_nb_fitted <- function(x, newdata, type, ...) {
  X <- corpus(newdata)
  X <- dfm_select(dfm(X), x$data$x)   
  res <- predict(x, newdata = X, ...)
  switch(
   type,
   raw = data.frame(Response = res$nb.predicted, stringsAsFactors = FALSE),
   prob = as.data.frame(res$posterior.prob, check.names = FALSE)
  )  
}

这给了我们

explanation <- lime::explain(train_raw[1:10, 1:5], 
                              explainer,
                              n_labels = 1,
                              n_features = 5,
                              cols = 2,
                              verbose = 0)
explanation[1, 1:5]
#       model_type case label label_prob    model_r2
# 1 classification    1 FALSE  0.9999986 0.001693861