我试着做个指纹分析。
我必须使用下面的代码将其转换为适当的格式
然而,在这种情况下,我不知道为什么我从最初的输入中丢失了2-3个文档。
dtm <- convert(myDfm, to = "topicmodels")因此,我可以将主题与初始数据框合并
我想我可以使用dfm,但它在lda()中是不可接受的格式。
toks <- toks %>% tokens_wordstem()
myDfm <- dfm(toks, ngrams = 1)不幸的是,我不能提供一个示例输入,因为它大约有30000行。如果我对一个5行的小示例进行测试,则该解决方案工作得很好。
有什么建议吗?
发布于 2019-08-20 02:48:37
转换后的dfm将丢弃空的“文档”,这可能是因为通过频率调整或模式匹配删除了特征(例如删除停用字)。LDA无法处理空文档,因此默认情况下,空文档将从LDA格式(“topicmodel”、"stm“等)中删除。
从v1.5开始,convert()中有一个名为omit_empty = TRUE的选项,如果您希望保留零特征文档,可以将其设置为FALSE。
library("quanteda")
## Package version: 1.5.1
txt <- c("one two three", "and or but", "four five")
dfmat <- tokens(txt) %>%
tokens_remove(stopwords("en")) %>%
dfm()
dfmat
## Document-feature matrix of: 3 documents, 5 features (66.7% sparse).
## 3 x 5 sparse Matrix of class "dfm"
## features
## docs one two three four five
## text1 1 1 1 0 0
## text2 0 0 0 0 0
## text3 0 0 0 1 1这就是设置omit_empty = FALSE所产生的差异:
# with and without the empty documents
convert(dfmat, to = "topicmodels")
## <<DocumentTermMatrix (documents: 2, terms: 5)>>
## Non-/sparse entries: 5/5
## Sparsity : 50%
## Maximal term length: 5
## Weighting : term frequency (tf)
convert(dfmat, to = "topicmodels", omit_empty = FALSE)
## <<DocumentTermMatrix (documents: 3, terms: 5)>>
## Non-/sparse entries: 5/10
## Sparsity : 67%
## Maximal term length: 5
## Weighting : term frequency (tf)最后,如果希望对dfm进行子集以删除空文档,只需使用dfm_subset()。第二个参数被强制转换为一个逻辑值,当为ntoken(dfmat) > 0时为TRUE,当为0时为FALSE。
# subset dfm to remove the empty documents
dfm_subset(dfmat, ntoken(dfmat))
## Document-feature matrix of: 2 documents, 5 features (50.0% sparse).
## 2 x 5 sparse Matrix of class "dfm"
## features
## docs one two three four five
## text1 1 1 1 0 0
## text3 0 0 0 1 1https://stackoverflow.com/questions/57559642
复制相似问题