NLP04-pyLDAvis可视化主题

42 篇文章 1 订阅
2 篇文章 0 订阅

摘要:演示一个pyLDAvis例子,作为入门,结合gensim来进行文本主题分类提供可视化的入门。

1. 安装pyLDAvis

官网:http://pyldavis.readthedocs.io/en/latest/
安装:pip install pyLDAvis
这个安装如果网速不好,会比较慢,这里要安装比较多的依赖包。

2. 例子

这个例子的数据采用前面文章的,http://blog.csdn.net/ld326/article/details/78353338,这里只是重点突出一下主题模型的可视化。

import pyLDAvis.gensim
from gensim import corpora
from gensim.models import LdaModel

def get_corpus_dictionary():
    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 EPS",
                 "Relation of user perceived response time to error measurement",
                 "The generation of random binary unordered trees",
                 "The intersection graph of paths in trees",
                 "Graph minors IV Widths of trees and well quasi ordering",
                 "Graph minors A survey"]
    stoplist = set('for a of the and to in'.split())
    texts = [[word for word in document.lower().split() if word not in stoplist]
             for document in documents]

    from collections import defaultdict
    frequency = defaultdict(int)
    for text in texts:
        for token in text:
            frequency[token] += 1

    texts = [[token for token in text if frequency[token] > 1]
             for text in texts]
    dictionary = corpora.Dictionary(texts)
    corpus = [dictionary.doc2bow(text) for text in texts]

    return corpus, dictionary

def test_lda():
    corpus, dictionary = get_corpus_dictionary()
    lda = LdaModel(corpus=corpus,num_topics=2)
    data = pyLDAvis.gensim.prepare(lda, corpus, dictionary)
    pyLDAvis.show(data,open_browser=False)

if __name__ == "__main__":
    test_lda()

3. 结果显示

打开web浏览器,
输入:http://127.0.0.1:8888/
至于这个IP与端口是可以修改的,在pyLDAvis.show()方法中可以修改了。
点击显示的内容查看各主题情况:
这里写图片描述

【作者:happyprince, http://blog.csdn.net/ld326/article/details/78370495

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值