标签: topic-modeling

从R topicmodels中删除DocumentTermMatrix中的空文档?

我正在使用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,它删除了任何文档中没有出现的术语,但没有类似的删除文档.

r lda topic-modeling topicmodels

40
推荐指数
3
解决办法
3万
查看次数

LDA主题建模 - 培训和测试

我读过LDA,我理解当输入文档集合时如何生成主题的数学.

参考文献说,LDA是一种算法,在给定文档集合的情况下(不需要监督),可以发现该集合中文档所表达的"主题".因此,通过使用LDA算法和Gibbs采样器(或变分贝叶斯),我可以输入一组文档,作为输出,我可以得到主题.每个主题都是一组具有指定概率的术语.

我不明白的是,如果上述情况属实,那么为什么许多主题建模教程谈论将数据集分成训练和测试集?

任何人都可以向我解释LDA如何用于训练模型的步骤(基本概念),然后可以用它来分析另一个测试数据集?

lda topic-modeling

37
推荐指数
1
解决办法
2万
查看次数

协作主题建模的简单Python实现?

我发现这两篇文章结合了协同过滤(矩阵分解)和主题建模(LDA),根据用户感兴趣的帖子/文章的主题条款向用户推荐类似的文章/帖子.

论文(PDF格式)为:" 推荐科学论文的协作主题建模 "和" 推荐GitHub存储库的协作主题建模 "

新算法称为协作主题回归.我希望找到一些实现此功能的python代码,但无济于事.这可能是一个很长的镜头,但有人可以显示一个简单的python示例吗?

python machine-learning collaborative-filtering lda topic-modeling

32
推荐指数
1
解决办法
3047
查看次数

使用gensim了解LDA实现

我试图了解Python中的gensim包如何实现Latent Dirichlet Allocation.我正在做以下事情:

定义数据集

documents = ["Apple is releasing a new product", 
             "Amazon sells many things",
             "Microsoft announces Nokia acquisition"]             
Run Code Online (Sandbox Code Playgroud)

删除停用词后,我创建了字典和语料库:

texts = [[word for word in document.lower().split() if word not in stoplist] for document in documents]
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
Run Code Online (Sandbox Code Playgroud)

然后我定义了LDA模型.

lda = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=5, update_every=1, chunksize=10000, passes=1)
Run Code Online (Sandbox Code Playgroud)

然后我打印主题:

>>> lda.print_topics(5)
['0.181*things + 0.181*amazon + 0.181*many + 0.181*sells + 0.031*nokia + 0.031*microsoft + 0.031*apple + 0.031*announces + 0.031*acquisition + 0.031*product', '0.077*nokia + …
Run Code Online (Sandbox Code Playgroud)

python dirichlet gensim topic-modeling

26
推荐指数
3
解决办法
3万
查看次数

主题模型:具有对数似然或困惑的交叉验证

我正在使用主题建模来集群文档.我需要提出最佳的主题数字.因此,我决定使用主题10,20,... 60进行十倍交叉验证.

我已将我的语料库分成十批,并留出一批用于保留集.我使用9个批次(总共180个文档)运行潜在的dirichlet分配(LDA),主题为10到60.现在,我必须计算保持集的困惑或记录可能性.

我从CV的一个讨论会上找到了这段代码.我真的不明白下面的几行代码.我有dtm矩阵使用holdout set(20个文档).但我不知道如何计算这个坚持集的困惑或记录可能性.


问题:

  1. 任何人都可以向我解释seq(2,100,by = 1)在这里意味着什么?那么,AssociatedPress [21:30]是什么意思?这里有什么功能(k)?

    best.model <- lapply(seq(2, 100, by=1), function(k){ LDA(AssociatedPress[21:30,], k) })
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果我想计算称为dtm的保持集的困惑或记录可能性,是否有更好的代码?我知道有perplexity()logLik()函数但是因为我是新手我无法弄清楚如何使用我的保持矩阵(称为dtm)来实现它.

  3. 如何使用包含200个文档的语料库进行十倍交叉验证?是否存在我可以调用的现有代码?我找到caret了这个目的,但也无法弄明白.

r topic-modeling cross-validation tm

22
推荐指数
2
解决办法
2万
查看次数

具有topicmodel的LDA,如何查看不同文档属于哪些主题?

我正在使用topicmodels包中的LDA,我已经在大约30.000个文档上运行它,获得了30个主题,并且获得了主题的前10个单词,它们看起来非常好.但是我想看看哪些文件最有可能属于哪个主题,我该怎么办呢?

myCorpus <- Corpus(VectorSource(userbios$bio))
docs <- userbios$twitter_id
myCorpus <- tm_map(myCorpus, tolower)
myCorpus <- tm_map(myCorpus, removePunctuation)
myCorpus <- tm_map(myCorpus, removeNumbers)
removeURL <- function(x) gsub("http[[:alnum:]]*", "", x)
myCorpus <- tm_map(myCorpus, removeURL)
myStopwords <- c("twitter", "tweets", "tweet", "tweeting", "account")

# remove stopwords from corpus
myCorpus <- tm_map(myCorpus, removeWords, stopwords('english'))
myCorpus <- tm_map(myCorpus, removeWords, myStopwords)


# stem words
# require(rJava) # needed for stemming function 
# library(Snowball) # also needed for stemming function 
# a <- tm_map(myCorpus, stemDocument, language = "english")

myDtm <- DocumentTermMatrix(myCorpus, …
Run Code Online (Sandbox Code Playgroud)

r lda topic-modeling tm

19
推荐指数
2
解决办法
2万
查看次数

如何从gensim打印LDA主题模型?蟒蛇

使用gensim我能够从LSA中的一组文档中提取主题但是如何访问从LDA模型生成的主题?

打印lda.print_topics(10)代码时出现以下错误,因为print_topics()返回a NoneType:

Traceback (most recent call last):
  File "/home/alvas/workspace/XLINGTOP/xlingtop.py", line 93, in <module>
    for top in lda.print_topics(2):
TypeError: 'NoneType' object is not iterable
Run Code Online (Sandbox Code Playgroud)

代码:

from gensim import corpora, models, similarities
from gensim.models import hdpmodel, ldamodel
from itertools import izip

documents = ["Human machine interface for lab abc computer applications",
              "A survey of user opinion of computer system response time",
              "The EPS user interface management system",
              "System and human system engineering testing of …
Run Code Online (Sandbox Code Playgroud)

python nlp lda gensim topic-modeling

19
推荐指数
5
解决办法
3万
查看次数

使用scikit-learn矢量化器和词汇表与gensim

我试图用gensim主题模型回收scikit-learn矢量化器对象.原因很简单:首先,我已经有了大量的矢量化数据; 第二,我更喜欢scikit-learn矢量化器的界面和灵活性; 第三,尽管使用gensim的主题建模非常快,但Dictionary()根据我的经验计算其词典()相对较慢.

之前已经提出过类似的问题,特别是在这里这里,桥接解决方案是gensim的Sparse2Corpus()函数,它将Scipy稀疏矩阵转换为gensim语料库对象.

但是,此转换不使用vocabulary_sklearn矢量化程序的属性,该属性保存单词和要素ID之间的映射.为了打印每个主题的判别词,这种映射是必要的(id2word在gensim主题模型中,描述为"从单词id(整数)到单词(字符串)的映射").

我知道gensim的Dictionary对象比scikit vect.vocabulary_(一个简单的Python dict)复杂得多(而且计算速度慢)......

任何想法使用vect.vocabulary_id2word在gensim模式?

一些示例代码:

# our data
documents = [u'Human machine interface for lab abc computer applications',
        u'A survey of user opinion of computer system response time',
        u'The EPS user interface management system',
        u'System and human system engineering testing of EPS',
        u'Relation of user perceived response time to error measurement',
        u'The generation of random …
Run Code Online (Sandbox Code Playgroud)

python gensim topic-modeling scikit-learn

19
推荐指数
3
解决办法
6780
查看次数

预测新数据的LDA主题

看起来这个问题可能已经被问了几次(这里 和这里),但还有待回答.我希望这是由于之前提出的问题含糊不清,正如评论所表明的那样.如果我通过再次询问一个类似问题来破坏协议,我道歉,我只是假设这些问题不会有任何新的答案.

无论如何,我是Latent Dirichlet Allocation的新手,我正在探索它作为文本数据降维方法的用途.最后,我想从一大堆单词中提取一小组主题,并使用这些主题作为模型中的一些变量来构建分类模型.我已经成功地在训练集上运行LDA,但我遇到的问题是能够预测哪些相同的主题出现在其他一些测试数据集中.我现在正在使用R的topicmodels包,但是如果还有其他方法可以使用其他包,我也会对此开放.

这是我想要做的一个例子:

library(topicmodels)
data(AssociatedPress)

train <- AssociatedPress[1:100]
test <- AssociatedPress[101:150]

train.lda <- LDA(train,5)
topics(train.lda)

#how can I predict the most likely topic(s) from "train.lda" for each document in "test"?
Run Code Online (Sandbox Code Playgroud)

r lda topic-modeling

17
推荐指数
1
解决办法
1万
查看次数

LDA对短文的不利之处是什么?

我试图理解为什么Latent Dirichlet Allocation(LDA)在像Twitter这样的短文本环境中表现不佳.我已经阅读了论文"短文本的主题模型",但是,我仍然不理解"单词共现的稀疏性".

从我的观点来看,LDA的生成部分对于任何类型的文本都是合理的,但是在短文本中导致不良结果的是抽样程序.我猜测LDA样本是基于两个部分的单词主题:(1)同一doc中其他单词的主题(2)该单词的其他出现的主题分配.由于短文本的(1)部分不能反映它的真实分布,因此导致每个单词的主题分配不佳.

如果您发现了这个问题,请随时发布您的想法,并帮助我理解这一点.

nlp lda topic-modeling

17
推荐指数
2
解决办法
9085
查看次数