skip to navigation
skip to content

lda 1.0.4

Topic modeling with latent Dirichlet allocation

Package Documentation

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).

>>> import numpy as np
>>> import lda
>>> import lda.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
>>> for i, topic_dist in enumerate(topic_word):
...     topic_words = np.array(vocab)[np.argsort(topic_dist)][:-(n_top_words+1):-1]
...     print('Topic {}: {}'.format(i, ' '.join(topic_words)))

Topic 0: british churchill sale million major letters west britain
Topic 1: church government political country state people party against
Topic 2: elvis king fans presley life concert young death
Topic 3: yeltsin russian russia president kremlin moscow michael operation
Topic 4: pope vatican paul john surgery hospital pontiff rome
Topic 5: family funeral police miami versace cunanan city service
Topic 6: simpson former years court president wife south church
Topic 7: order mother successor election nuns church nirmala head
Topic 8: charles prince diana royal king queen parker bowles
Topic 9: film french france against bardot paris poster animal
Topic 10: germany german war nazi letter christian book jews
Topic 11: east peace prize award timor quebec belo leader
Topic 12: n't life show told very love television father
Topic 13: years year time last church world people say
Topic 14: mother teresa heart calcutta charity nun hospital missionaries
Topic 15: city salonika capital buddhist cultural vietnam byzantine show
Topic 16: music tour opera singer israel people film israeli
Topic 17: church catholic bernardin cardinal bishop wright death cancer
Topic 18: harriman clinton u.s ambassador paris president churchill france
Topic 19: city museum art exhibition century million churches set

The document-topic distributions are available in model.doc_topic_.

>>> doc_topic = model.doc_topic_
>>> for i in range(10):
...     print("{} (top topic: {})".format(titles[i], doc_topic[i].argmax()))
0 UK: Prince Charles spearheads British royal revolution. LONDON 1996-08-20 (top topic: 8)
1 GERMANY: Historic Dresden church rising from WW2 ashes. DRESDEN, Germany 1996-08-21 (top topic: 13)
2 INDIA: Mother Teresa's condition said still unstable. CALCUTTA 1996-08-23 (top topic: 14)
3 UK: Palace warns British weekly over Charles pictures. LONDON 1996-08-25 (top topic: 8)
4 INDIA: Mother Teresa, slightly stronger, blesses nuns. CALCUTTA 1996-08-25 (top topic: 14)
5 INDIA: Mother Teresa's condition unchanged, thousands pray. CALCUTTA 1996-08-25 (top topic: 14)
6 INDIA: Mother Teresa shows signs of strength, blesses nuns. CALCUTTA 1996-08-26 (top topic: 14)
7 INDIA: Mother Teresa's condition improves, many pray. CALCUTTA, India 1996-08-25 (top topic: 14)
8 INDIA: Mother Teresa improves, nuns pray for "miracle". CALCUTTA 1996-08-26 (top topic: 14)
9 UK: Charles under fire over prospect of Queen Camilla. LONDON 1996-08-26 (top topic: 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).

Other implementations

License

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

 
File Type Py Version Uploaded on Size
lda-1.0.4-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (md5, pgp) Python Wheel cp27 2016-07-15 364KB
lda-1.0.4-cp27-cp27m-manylinux1_i686.whl (md5, pgp) Python Wheel cp27 2016-07-15 459KB
lda-1.0.4-cp27-cp27m-manylinux1_x86_64.whl (md5, pgp) Python Wheel cp27 2016-07-15 474KB
lda-1.0.4-cp27-cp27m-win32.whl (md5, pgp) Python Wheel cp27 2016-07-15 297KB
lda-1.0.4-cp27-cp27m-win_amd64.whl (md5, pgp) Python Wheel cp27 2016-07-15 306KB
lda-1.0.4-cp27-cp27mu-manylinux1_i686.whl (md5, pgp) Python Wheel cp27 2016-07-15 459KB
lda-1.0.4-cp27-cp27mu-manylinux1_x86_64.whl (md5, pgp) Python Wheel cp27 2016-07-15 474KB
lda-1.0.4-cp33-cp33m-win32.whl (md5, pgp) Python Wheel cp33 2016-07-15 297KB
lda-1.0.4-cp33-cp33m-win_amd64.whl (md5, pgp) Python Wheel cp33 2016-07-15 305KB
lda-1.0.4-cp34-cp34m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (md5, pgp) Python Wheel cp34 2016-07-15 364KB
lda-1.0.4-cp34-cp34m-manylinux1_i686.whl (md5, pgp) Python Wheel cp34 2016-07-15 475KB
lda-1.0.4-cp34-cp34m-manylinux1_x86_64.whl (md5, pgp) Python Wheel cp34 2016-07-15 492KB
lda-1.0.4-cp34-cp34m-win32.whl (md5, pgp) Python Wheel cp34 2016-07-15 297KB
lda-1.0.4-cp34-cp34m-win_amd64.whl (md5, pgp) Python Wheel cp34 2016-07-15 305KB
lda-1.0.4-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (md5, pgp) Python Wheel cp35 2016-07-15 364KB
lda-1.0.4-cp35-cp35m-manylinux1_i686.whl (md5, pgp) Python Wheel cp35 2016-07-15 473KB
lda-1.0.4-cp35-cp35m-manylinux1_x86_64.whl (md5, pgp) Python Wheel cp35 2016-07-15 489KB
lda-1.0.4-cp35-cp35m-win32.whl (md5, pgp) Python Wheel cp35 2016-07-15 296KB
lda-1.0.4-cp35-cp35m-win_amd64.whl (md5, pgp) Python Wheel cp35 2016-07-15 305KB
lda-1.0.4.tar.gz (md5, pgp) Source 2016-07-15 292KB
lda-1.0.4.zip (md5, pgp) Source 2016-07-15 309KB