Skip to content

Repository files navigation

lda: Topic modeling with latent Dirichlet allocation

pypi versiontravis-ci build statusZenodo citation

lda implements latent Dirichlet allocation (LDA) using collapsed Gibbs sampling. lda is fast and is tested on Linux, OS X, and Windows.

You can read more about lda in the documentation.

Installation

pip install lda

Getting started

lda.LDA implements latent Dirichlet allocation (LDA). The interface follows conventions found in scikit-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'tlifeshowtoldverylovetelevisionfatherTopic13: yearsyeartimelastchurchworldpeoplesayTopic14: motherteresaheartcalcuttacharitynunhospitalmissionariesTopic15: citysalonikacapitalbuddhistculturalvietnambyzantineshowTopic16: musictouroperasingerisraelpeoplefilmisraeliTopic17: churchcatholicbernardincardinalbishopwrightdeathcancerTopic18: harrimanclintonu.sambassadorparispresidentchurchillfranceTopic19: citymuseumartexhibitioncenturymillionchurchesset

The document-topic distributions are available in 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'sconditionsaidstillunstable. CALCUTTA1996-08-23 (toptopic: 14)
3UK: PalacewarnsBritishweeklyoverCharlespictures. LONDON1996-08-25 (toptopic: 8)
4INDIA: MotherTeresa, slightlystronger, blessesnuns. CALCUTTA1996-08-25 (toptopic: 14)
5INDIA: MotherTeresa'sconditionunchanged, thousandspray. CALCUTTA1996-08-25 (toptopic: 14)
6INDIA: MotherTeresashowssignsofstrength, blessesnuns. CALCUTTA1996-08-26 (toptopic: 14)
7INDIA: MotherTeresa'sconditionimproves, manypray. CALCUTTA, India1996-08-25 (toptopic: 14)
8INDIA: MotherTeresaimproves, nunsprayfor"miracle". CALCUTTA1996-08-26 (toptopic: 14)
9UK: CharlesunderfireoverprospectofQueenCamilla. LONDON1996-08-26 (toptopic: 8)

Requirements

Python 2.7 or Python 3.3+ is required. The following packages are required

Caveat

lda aims for simplicity. (It happens to be fast, as essential parts are written in C via Cython.) If you are working with a very large corpus you may wish to use more sophisticated topic models such as those implemented in hca and MALLET. hca is written entirely in C and MALLET is written in Java. Unlike lda, hca can use more than one processor at a time. Both MALLET and hca implement topic models known to be more robust than standard latent Dirichlet allocation.

Notes

Latent Dirichlet allocation is described in Blei et al. (2003) and Pritchard et al. (2000). Inference using collapsed Gibbs sampling is described in Griffiths and Steyvers (2004).

Important links

Other implementations

License

lda is licensed under Version 2.0 of the Mozilla Public License.

About

Topic modeling with latent Dirichlet allocation using Gibbs sampling

Resources

Contributing

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages