Package: ldafork
ldaimplements latent Dirichlet allocation (LDA) using collapsed Gibbs
sampling.ldais fast and is tested on Linux, OS X, and Windows. This is
a maintainance fork of the original code for newer Python versions.You can read more about lda inthe documentation.NoteThis is a fork of the originallda packagedeveloped byAllan Riddellto maintain the package for newer Python versions, OS versions and
dependencies. The only difference to the original implementation are compatibility fixes implemented for this
purpose. This fork specifically provides wheels (installable binary packages) for Python 3.6 to Python 3.8 on
Linux, MacOS and Windows 10.Installationpip install ldaforkGetting 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.6+ is required. The following packages are required and will be installed along with withldaforkwhen you
usepip:numpypbrCaveatldaaims 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.