Package: lda
NOTE: This package is in maintenance mode. Critical bugs will be fixed. No new features will be added.ldaimplements latent Dirichlet allocation (LDA) using collapsed Gibbs
sampling.ldais fast and is tested on Linux, OS X, and Windows.You can read more about lda inthe documentation.Installationpip install ldaGetting startedlda.LDAimplements latent Dirichlet allocation (LDA). The interface follows
conventions found inscikit-learn.The following demonstrates how to inspect a model of a subset of the Reuters
news dataset. The input below,X, is a document-term matrix (sparse matrices
are accepted).>>>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:citymuseumartexhibitioncenturymillionchurchessetThe document-topic distributions are available inmodel.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)RequirementsPython 3.9+ and NumPy is required.Caveatldaaims for simplicity. (It happens to be fast, as essential parts are
written in C viaCython.) If you are working with a very large corpus you may
wish to use more sophisticated topic models such as those implemented inhcaandMALLET.hcais written entirely in C andMALLETis written in Java.
Unlikelda,hcacan use more than one processor at a time. BothMALLETandhcaimplement topic models known to be more robust than standard latent
Dirichlet allocation.NotesLatent Dirichlet allocation is described inBlei et al. (2003)andPritchard
et al. (2000). Inference using collapsed Gibbs sampling is described inGriffiths and Steyvers (2004).Important linksDocumentation:http://lda.readthedocs.orgSource code:https://github.com/lda-project/lda/Issue tracker:https://github.com/lda-project/lda/issuesOther implementationsscikit-learn’sLatentDirichletAllocation(uses online variational inference)gensim(uses online variational inference)Licenselda is licensed under Version 2.0 of the Mozilla Public License.