如何仅从存储的单词列表中生成 text2vector 中的文档术语矩阵

How to produce document term matrix in text2vector only from stored list of words

text2vec 中用于向量化文本并仅使用指示的单词列表实现 dtm 的语法是什么?

如何仅在指示的特征上向量化和生成文档术语矩阵?如果这些特征没有出现在文本中,变量应该保持为空。

我需要生成与我 运行 建模的 dtm 中的列完全相同的术语文档矩阵,否则我无法在新文档上使用随机森林模型。

您只能根据特定的一组特征创建文档术语矩阵:

v = create_vocabulary(c("word1", "word2"))
vectorizer = vocab_vectorizer(v)
dtm_test = create_dtm(it, vectorizer)

但是我不建议 1) 在这种稀疏数据上使用随机森林 - 它不会很好地工作 2) 执行你描述的特征选择方式 - 你可能会过度拟合。

I need to produce term document matrices with exactly the same columns as in the dtm I run the modelling on, otherwise I cannot use random forest model on new documents.

quanteda 中,您可以使用 dfm_select() 将测试集的特征设置为与训练集的特征相同。例如,要使下面的 dfm1 具有与 dfm2 相同的特征:

txts <- c("a b c d", "a a b b", "b c c d e f")

(dfm1 <- dfm(txts[1:2]))
## Document-feature matrix of: 2 documents, 4 features (25% sparse).
## 2 x 4 sparse Matrix of class "dfmSparse"
##        features
## docs    a b c d
##   text1 1 1 1 1
##   text2 2 2 0 0
(dfm2 <- dfm(txts[2:3]))
## Document-feature matrix of: 2 documents, 6 features (41.7% sparse).
## 2 x 6 sparse Matrix of class "dfmSparse"
##        features
## docs    a b c d e f
##   text1 2 2 0 0 0 0
##   text2 0 1 2 1 1 1

dfm_select(dfm1, dfm2, valuetype = "fixed", verbose = TRUE)
## kept 4 features, padded 2 features
## Document-feature matrix of: 2 documents, 6 features (50% sparse).
## 2 x 6 sparse Matrix of class "dfmSparse"
##        features
## docs    a b c d e f
##   text1 1 1 1 1 0 0
##   text2 2 2 0 0 0 0

然而,对于特征上下文矩阵(text2vec 需要输入),这将不起作用,因为同现(至少那些用 window 而不是文档上下文)在功能之间是相互依赖的,因此您不能简单地以相同的方式添加和删除它们。