text2vec中只有指定的单词列表才能实现文本矢量化和实现dtm的语法是什么?
如何仅在指定的特征上矢量化和生成文档术语矩阵?如果这些特性没有出现在文本中,变量应该保持为空。
我需要生成与我运行建模的dtm中的列完全相同的术语文档矩阵,否则我不能在新文档上使用随机森林模型。
发布于 2017-07-28 14:05:28
只能根据特定的功能集创建文档术语矩阵:
v = create_vocabulary(c("word1", "word2"))
vectorizer = vocab_vectorizer(v)
dtm_test = create_dtm(it, vectorizer)但是,我不建议在这样稀疏的数据上使用随机森林--它不会很好地执行您所描述的特性选择方法--您可能会过度适应。
发布于 2017-07-28 14:35:19
我需要生成与我运行建模的dtm中的列完全相同的术语文档矩阵,否则我不能在新文档上使用随机森林模型。
在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需要输入的内容)来说,这是行不通的,因为协同出现(至少是那些用窗口而不是文档上下文计算的)跨功能是相互依赖的,所以不能简单地以相同的方式添加和删除它们。
https://stackoverflow.com/questions/45373699
复制相似问题