Quanteda:如何从单词列表中创建具有相同功能的dfms

时间:2017-07-26 17:19:49

标签: r dictionary text-mining corpus quanteda

我在n-gram矩阵的文章上运行了一个随机森林,因为我想把它分为两类。由于RF,我收到了一个重要变量列表。

现在我想在选定的前n个特征上运行随机森林,然后使用相同的特征来预测新的分类。为了那个原因 我需要为最重要的变量(来自RF)创建dfm。 如何从这些重要变量列表中创建字典?

代码的相关部分......创建字典后,我只有一个条目。如何正确创建?

forestModel <-
  randomForest(x =  as.matrix(myStemMat),y=as.factor(classVect),
               ntree = 1000 )

impVariables <-
  data.frame(important = as.matrix(importance(forestModel)))

impVariables <-
  impVariables %>% mutate(impVar = row.names(impVariables)) %>% 
  arrange(desc(MeanDecreaseGini)) %>% 
  top_n(1000, wt = MeanDecreaseGini) %>% 
  select(impVar) %>% as.list() %>% dictionary()

myStemMat <-
  dfm(
    mycorpus,
    dictionary=impVariables,
    #   remove = stopwordsPL,
    stem = TRUE,
    remove_punct = TRUE,
    ngrams=c(1,2)
  )

简而言之,当我有一个字符串,单词,n-gram的列表时,如何创建一个字典,以便我可以在dfm()函数中使用它来生成术语矩阵?

这是完整代码“可重复示例”及其使用的数据的链接。 https://www.dropbox.com/s/3oe1tcfcauer0wf/text_data.zip?dl=0

1 个答案:

答案 0 :(得分:2)

你应该仔细阅读?dictionary,因为这不是设计用于特征选择的集合(虽然它可以是),而是在分配给字典键的值中创建等价类。

如果您的impVariables是要素的字符向量,那么您应该能够使用这些命令执行所需的选择:

toks <- 
    tokens(mycorpus, remove_punct = TRUE) %>%
    tokens_select(impVariables, padding = TRUE) %>%
    tokens_wordstem() %>%
    tokens_ngrams(n = 1:2)

dfm(toks)

其中,最后一个命令生成一个文档特征矩阵,该矩阵仅包含在随机森林模型的顶部特征中选择的词干ngram特征。请注意,padding = TRUE将阻止在原始文本中形成从未相邻的ngram。如果您不关心,请将其设置为FALSE(默认值)。

<强>增加:

要从选择词的字符向量中选择dfm的列,我们可以使用以下两种方法。

我们将使用这些示例对象:

# two sample texts and their dfm representations
txt1 <- c(d1 = "a b c f g h",
          d2 = "a a c c d f f f")
txt2 <- c(d1 = "c c d f g h",
          d2 = "b b d i j")
(dfm1 <- dfm(txt1))
# Document-feature matrix of: 2 documents, 7 features (28.6% sparse).
# 2 x 7 sparse Matrix of class "dfmSparse"
#     features
# docs a b c f g h d
#   d1 1 1 1 1 1 1 0
#   d2 2 0 2 3 0 0 1

(dfm2 <- dfm(txt2))
# Document-feature matrix of: 2 documents, 8 features (43.8% sparse).
# 2 x 8 sparse Matrix of class "dfmSparse"
#     features
# docs c d f g h b i j
#   d1 2 1 1 1 1 0 0 0
#   d2 0 1 0 0 0 2 1 1

impVariables <- c("a", "c", "e", "z")

第一种方法:创建一个dfm并使用dfm_select()

选择

在这里,我们正在从你的特征的角色向量创建一个dfm,这样我们就可以将它们注册为特征,因为当选择对象是dfm时dfm_select()的工作方式。

impVariablesDfm <- dfm(paste(impVariables, collapse = " "))
dfm_select(dfm1, impVariablesDfm)
# Document-feature matrix of: 2 documents, 4 features (50% sparse).
# 2 x 4 sparse Matrix of class "dfmSparse"
#     features
# docs a c e z
#   d1 1 1 0 0
#   d2 2 2 0 0

dfm_select(dfm2, impVariablesDfm)
# Document-feature matrix of: 2 documents, 4 features (87.5% sparse).
# 2 x 4 sparse Matrix of class "dfmSparse"
#     features
# docs a c e z
#   d1 0 2 0 0
#   d2 0 0 0 0

第二种方法:创建字典并使用dfm_lookup()

选择字典

让我们创建一个辅助函数来从字符向量创建字典:

# make a dictionary where each key = its value
char2dictionary <- function(x) {
    result <- as.list(x)  # make the vector into a list
    names(result) <- x
    dictionary(result)
}

现在使用dfm查找,我们只得到密钥,即使是未被观察到的密钥:

dfm_lookup(dfm1, dictionary = char2dictionary(impVariables))
# Document-feature matrix of: 2 documents, 4 features (50% sparse).
# 2 x 4 sparse Matrix of class "dfmSparse"
#     features
# docs a c e z
#   d1 1 1 0 0
#   d2 2 2 0 0

dfm_lookup(dfm2, dictionary = char2dictionary(impVariables))
# Document-feature matrix of: 2 documents, 4 features (87.5% sparse).
# 2 x 4 sparse Matrix of class "dfmSparse"
#     features
# docs a c e z
#   d1 0 2 0 0
#   d2 0 0 0 0

注意:(但第一个至少适用于v0.9.9.65):

packageVersion("quanteda")
# [1] ‘0.9.9.85’