我正在使用R中的topicmodels包进行主题建模.我正在创建一个Corpus对象,进行一些基本的预处理,然后创建一个DocumentTermMatrix:
corpus <- Corpus(VectorSource(vec), readerControl=list(language="en"))
corpus <- tm_map(corpus, tolower)
corpus <- tm_map(corpus, removePunctuation)
corpus <- tm_map(corpus, removeWords, stopwords("english"))
corpus <- tm_map(corpus, stripWhitespace)
corpus <- tm_map(corpus, removeNumbers)
...snip removing several custom lists of stopwords...
corpus <- tm_map(corpus, stemDocument)
dtm <- DocumentTermMatrix(corpus, control=list(minDocFreq=2, minWordLength=2))
Run Code Online (Sandbox Code Playgroud)
然后执行LDA:
LDA(dtm, 30)
Run Code Online (Sandbox Code Playgroud)
最后调用LDA()返回错误
"Each row of the input matrix needs to contain at least one non-zero entry".
Run Code Online (Sandbox Code Playgroud)
我认为这意味着在预处理之后至少有一个文档中没有任何术语.有没有一种简单的方法可以从DocumentTermMatrix中删除不包含任何术语的文档?
我查看了topicmodels包的文档,找到了函数removeSparseTerms,它删除了任何文档中没有出现的术语,但没有类似的删除文档.
请耐心等待,因为我对此非常陌生,正在开设证书课程的课程.
我有通过从Pubmed和Embase数据库中检索文献计量记录获得的.csv数据集.有1034行.有几列,但是,我试图从一列创建主题模型,Abstract列和一些记录没有摘要.我已经完成了一些处理(删除停用词,标点符号等),并且能够对出现超过200次的单词进行条形图处理以及按等级创建"常用术语"列表,还可以运行与所选单词的单词关联.所以,似乎r在抽象字段中看到了单词本身.当我尝试使用topicmodels包创建主题模型时,我的问题出现了.这是我正在使用的一些代码.
#including 1st 3 lines for reference
options(header = FALSE, stringsAsFactors = FALSE, FileEncoding =
"latin1")
records <- read.csv("Combined.csv")
AbstractCorpus <- Corpus(VectorSource(records$Abstract))
AbstractTDM <- TermDocumentMatrix(AbstractCorpus)
library(topicmodels)
library(lda)
lda <- LDA(AbstractTDM, k = 8)
(term <- terms(lda, 6))
term <- (apply(term, MARGIN = 2, paste, collapse = ","))
Run Code Online (Sandbox Code Playgroud)
但是,我得到的主题输出如下.
Topic 1 Topic 2 Topic 3 Topic 4 Topic 5 Topic 6 Topic 7 Topic 8
[1,] "499" "733" "390" "833" "17" "413" "719" "392"
[2,] "484" "655" "808" "412" "550" …Run Code Online (Sandbox Code Playgroud) 我读了这个问题(相干分数 0.4 是好还是坏?),发现相干分数(u_mass)是从 -14 到 14。但是当我做实验时,我得到的 u_mass 分数为 -18,c_v 分数为 0.67 。我想知道我的 u_mass 分数如何超出范围 (-14, 14)?
更新:我使用gensim库并扫描了从2到50的主题数量。对于u_mass,它从0开始到最低的负点并返回一点,就像c_v的颠倒版本。
成功运行我的 stm 几次后,现在每次尝试运行它时都会收到此错误消息:
\nUNRELIABLE VALUE: Future (\xe2\x80\x98<none>\xe2\x80\x99) unexpectedly generated random numbers without specifying argument 'seed'. There is a risk that those random numbers are not statistically sound and the overall results might be invalid. To fix this, specify 'seed=TRUE'. This ensures that proper, parallel-safe random numbers are produced via the L'Ecuyer-CMRG method. To disable this check, use 'seed=NULL', or set option 'future.rng.onMisuse' to "ignore".\nRun Code Online (Sandbox Code Playgroud)\n这是我运行的代码:
\nmany_models <- data_frame(K = c(10, 20, 30, 40, 50, 60)) %>%\nmutate(topic_model = …Run Code Online (Sandbox Code Playgroud) 我正在尝试在相当大的数据集上使用 topicmodels 包中的 LDA()。在尝试解决以下错误“In nr * nc : NAs 由整数溢出产生”和“输入矩阵的每一行需要包含至少一个非零条目”之后,我最终得到了这个错误。
ask<- read.csv('askreddit201508.csv', stringsAsFactors = F)
myDtm <- create_matrix(as.vector(ask$title), language="english", removeNumbers=TRUE, stemWords=TRUE, weighting=weightTf)
myDtm2 = removeSparseTerms(myDtm,0.99999)
myDtm2 <- rollup(myDtm2, 2, na.rm=TRUE, FUN = sum)
rowTotals <- apply(myDtm2 , 1, sum)
myDtm2 <- myDtm2[rowTotals> 0, ]
LDA2 <- LDA(myDtm2,100)
Error in LDA(myDtm2, 100) :
The DocumentTermMatrix needs to have a term frequency weighting
Run Code Online (Sandbox Code Playgroud) 在 lda 分析中
library(topicmodels)
# parameters for Gibbs sampling
burnin <- 4000
iter <- 2000
thin <- 500
seed <-list(1969,5,25,102855,2012)
nstart <- 5
best <- TRUE
#Number of topics
k <- 10
library(topicmodels)
data("AssociatedPress", package = "topicmodels")
#Run LDA with Gibbs
ldaOut <-LDA(AssociatedPress[1:20,], k, method="Gibbs", control=list(nstart=nstart, seed = seed, best = best, burnin =
burnin, iter = iter, thin=thin))
Run Code Online (Sandbox Code Playgroud)
如何创建网格搜索以找到参数的最佳值?