基于潜在dirichlet分配的主题建模

lda的Python项目详细描述


pypi versiontravis-ci build statusZenodo citation

lda使用折叠gibbs实现潜在的dirichlet分配(lda) 取样。lda速度很快,在Linux、OS X和Windows上进行了测试。

您可以在the documentation中阅读有关lda的更多信息。

安装

pip install lda

开始

lda.LDA实现潜在的dirichlet分配(lda)。接口如下 在scikit-learn中找到约定。

下面演示如何检查路透社子集的模型 新闻数据集。下面的输入X是一个文档项矩阵(稀疏矩阵 被接受)。

>>>importnumpyasnp>>>importlda>>>importlda.datasets>>>X=lda.datasets.load_reuters()>>>vocab=lda.datasets.load_reuters_vocab()>>>titles=lda.datasets.load_reuters_titles()>>>X.shape(395,4258)>>>X.sum()84010>>>model=lda.LDA(n_topics=20,n_iter=1500,random_state=1)>>>model.fit(X)# model.fit_transform(X) is also available>>>topic_word=model.topic_word_# model.components_ also works>>>n_top_words=8>>>fori,topic_distinenumerate(topic_word):...topic_words=np.array(vocab)[np.argsort(topic_dist)][:-(n_top_words+1):-1]...print('Topic {}: {}'.format(i,' '.join(topic_words)))Topic0:britishchurchillsalemillionmajorletterswestbritainTopic1:churchgovernmentpoliticalcountrystatepeoplepartyagainstTopic2:elviskingfanspresleylifeconcertyoungdeathTopic3:yeltsinrussianrussiapresidentkremlinmoscowmichaeloperationTopic4:popevaticanpauljohnsurgeryhospitalpontiffromeTopic5:familyfuneralpolicemiamiversacecunanancityserviceTopic6:simpsonformeryearscourtpresidentwifesouthchurchTopic7:ordermothersuccessorelectionnunschurchnirmalaheadTopic8:charlesprincedianaroyalkingqueenparkerbowlesTopic9:filmfrenchfranceagainstbardotparisposteranimalTopic10:germanygermanwarnaziletterchristianbookjewsTopic11:eastpeaceprizeawardtimorquebecbeloleaderTopic12:n't life show told very love television fatherTopic13:yearsyeartimelastchurchworldpeoplesayTopic14:motherteresaheartcalcuttacharitynunhospitalmissionariesTopic15:citysalonikacapitalbuddhistculturalvietnambyzantineshowTopic16:musictouroperasingerisraelpeoplefilmisraeliTopic17:churchcatholicbernardincardinalbishopwrightdeathcancerTopic18:harrimanclintonu.sambassadorparispresidentchurchillfranceTopic19:citymuseumartexhibitioncenturymillionchurchesset

文档主题发行版位于model.doc_topic_中。

>>>doc_topic=model.doc_topic_>>>foriinrange(10):...print("{} (top topic: {})".format(titles[i],doc_topic[i].argmax()))0UK:PrinceCharlesspearheadsBritishroyalrevolution.LONDON1996-08-20(toptopic:8)1GERMANY:HistoricDresdenchurchrisingfromWW2ashes.DRESDEN,Germany1996-08-21(toptopic:13)2INDIA:MotherTeresa's condition said still unstable. CALCUTTA 1996-08-23 (top topic: 14)3UK:PalacewarnsBritishweeklyoverCharlespictures.LONDON1996-08-25(toptopic:8)4INDIA:MotherTeresa,slightlystronger,blessesnuns.CALCUTTA1996-08-25(toptopic:14)5INDIA:MotherTeresa's condition unchanged, thousands pray. CALCUTTA 1996-08-25 (top topic: 14)6INDIA:MotherTeresashowssignsofstrength,blessesnuns.CALCUTTA1996-08-26(toptopic:14)7INDIA:MotherTeresa's condition improves, many pray. CALCUTTA, India 1996-08-25 (top topic: 14)8INDIA:MotherTeresaimproves,nunsprayfor"miracle".CALCUTTA1996-08-26(toptopic:14)9UK:CharlesunderfireoverprospectofQueenCamilla.LONDON1996-08-26(toptopic:8)

要求

Python2.7或Python3.5+是必需的。需要以下软件包

警告

lda旨在简化。(它碰巧很快,因为基本部件是 用c语言通过Cython)编写。如果你正在使用一个非常大的语料库,你可以 希望使用更复杂的主题模型,如在hca中实现的那些 以及MALLEThca完全用c编写,MALLET用java编写。 与lda不同,hca一次可以使用多个处理器。MALLEThca实现已知比标准潜在模型更健壮的主题模型 迪里克利特分配。

注释

潜在的dirichlet分配在Blei et al. (2003)Pritchard et al. (2000)中描述。使用折叠gibbs采样的推断在 Griffiths and Steyvers (2004)

其他实现

许可证

lda是在mozilla公共许可的2.0版本下获得许可的。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
JPanel的java滚动窗格?   java JPA查询方法:findTopX,其中X是给定的(变量)数?   当应用程序在Oreo中处于后台时,java音乐服务在几秒钟后不工作   java通过TCP流发送密钥编码的字符串BadPaddingException with Windows   java在这里构建代码的更好方法是什么?   如何使用Java(无给定参数)检查列表中是否有数字   跨多个JVM的java事务控制   dataframe如何使用SparkJavaAPI的MapFunction和ReduceFunction在集群上并行处理这个问题?   java如何在多模块spring引导中配置yml文件   已安装java JDBC驱动程序,但出现异常   JUnit 4中的java,有没有一种方法可以在扩展BlockJUnit4ClassRunner的自定义运行程序中,在all@BeforeClass之前运行一段代码   继续未能选择JAVA上的radioButton   java如何通过PHP代码运行shell命令?   java JSlider SynthLookandFeel填充颜色   java给定一个数字,打印下三个数字   java SpringMVC(安全)403错误   java Graphhopper从装载的obb文件系统加载路由信息时出错   java安全工具扫描问题,发现依赖默认编码:新字符串(字节[])   java如何获取嵌套字段