Gensim-TFIDF,LDA,LSI实战
来源:互联网 发布:工行软件开发中心 编辑:程序博客网 时间:2024/06/03 20:02
介绍
Gensim能很方便的分析文本,包括了TFIDF,LDA,LSA,DP等文本分析方法
词典与词库
首先将文本处理生成dictionary和corpus。
dictionary是词典,包含词以及词在词典中对应的位置。
corpus将文本存贮成(词在词典中位置,词频)这种形式,每个文本为一行。
实战
from gensim import corpora, models, similaritiesfrom pprint import pprintdef GenDictandCorpus(): 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"] texts = [[word for word in document.lower().split()] for document in documents] # 词典 dictionary = corpora.Dictionary(texts) # 词库,以(词,词频)方式存贮 corpus = [dictionary.doc2bow(text) for text in texts] print(dictionary) print(corpus) return dictionary, corpus
结果:
TFIDF
实战
def Tfidf(): dictionary, corpus = GenDictandCorpus() # initialize a model tfidf = models.TfidfModel(corpus) # print(tfidf) # Transforming vectors # 此时,tfidf被视为一个只读对象,可以用于将任何向量从旧表示(词频)转换为新表示(TfIdf实值权重) doc_bow = [(0, 1), (1, 1)] # 使用模型tfidf,将doc_bow(由词,词频)表示转换成(词,tfidf)表示 # print(tfidf[doc_bow]) # 转换整个词库 corpus_tfidf = tfidf[corpus] for doc in corpus_tfidf: print(doc) return corpus_tfidf
结果:
LDA
实战:
def LDA(): dictionary, corpus = GenDictandCorpus() corpus_tfidf = Tfidf() ldamodel = models.LdaModel(corpus, id2word=dictionary, num_topics=2) ldamodel.print_topics() pprint(ldamodel.print_topics())
结果:
LSI
实战:
# 潜在语义索引(Latent Semantic Indexing,以下简称LSI),有的文章也叫Latent Semantic Analysis(LSA)# LSI是基于奇异值分解(SVD)的方法来得到文本的主题的def LSI(): dictionary, corpus = GenDictandCorpus() corpus_tfidf = Tfidf() # initialize an LSI transformation lsi = models.LsiModel(corpus_tfidf, id2word=dictionary, num_topics=2) corpus_lsi = lsi[corpus_tfidf] # print(corpus_lsi) # pprint(lsi.print_topics(2)) # 在这里实际执行了bow-> tfidf和tfidf-> lsi转换 for doc in corpus_lsi: print(doc) # lsi.save('/tmp/model.lsi') # lsi = models.LsiModel.load('/tmp/model.lsi')
结果:
DP
实战:
# 随机投影(Random Projections),RP旨在减少矢量空间维数。# 这是非常有效的方法,通过投掷一点随机性来近似文档之间的TfIdf距离。# 推荐的目标维度数百/千,取决于您的数据集。def RP(): corpus_tfidf = Tfidf() RP_model = models.RpModel(corpus_tfidf, num_topics=2) # print(RP_model) corpus_rp = RP_model[corpus_tfidf] for doc in corpus_rp: print(doc)
结果:
如有理解错误,还望指教。
参考:http://radimrehurek.com/gensim/tut2.html
阅读全文
0 0
- Gensim-TFIDF,LDA,LSI实战
- Gensim-维基百科中文语料LDA,LSI实验记录
- 使用Gensim建立bow TFIDF LSI模型对文本相似度计算
- gensim-lda
- gensim的主题模型LSI
- gensim中实践LDA
- 用gensim做LDA
- gensim 之 td-idf和lsi模型
- Gensim LDA主题模型实验
- 文本分析之TFIDF/LDA/Word2vec实践
- 基于tfidf 以及 lsi 的文本相似度分析
- python+gensim︱jieba分词、词袋doc2bow、TFIDF文本挖掘
- Gensim实战(一)
- Python gensim基础实战
- gensim的LSI模型来计算文档的相似度
- gensim的LSI模型来计算文档的相似度
- 主题模型TopicModel:通过gensim实现LDA
- Gensim做中文主题模型(LDA)
- 框架学习系列 mybatis 第十五篇 mybatis动态sql及常用标签二
- 继承——代码块的继承
- 360 RePlugin引入教程
- 排序算法---最快最简单的桶排序
- 50.实现幂函数
- Gensim-TFIDF,LDA,LSI实战
- Android InputMethodManager输入法简介
- No resource identifier
- container-with-most-water
- 字符串中常用的方法
- redis常用命令(保存)
- JavaWeb学习篇之----EL表达式详解
- ECMAScript Assignment Operators
- Android 带你彻底理解 Window 和 WindowManager