Topic modeling with latent Dirichlet allocation
Project description
lda implements latent Dirichlet allocation (LDA) using collapsed Gibbs sampling. lda is 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 in the documentation.
Installation
pip install ldafork
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 3.6+ is required. The following packages are required and will be installed along with with ldafork when you use pip:
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
Documentation: http://lda.readthedocs.org
Source code: https://github.com/lda-project/lda/
Issue tracker: https://github.com/lda-project/lda/issues
Other implementations
scikit-learn’s LatentDirichletAllocation (uses online variational inference)
gensim (uses online variational inference)
License
lda is licensed under Version 2.0 of the Mozilla Public License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for ldafork-1.2.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 605faa687ec2092bcd591063c08345175f9a44e23cc033240e86416157341c40 |
|
MD5 | fcf28ddec5b05f3f60d993a163e2ea5f |
|
BLAKE2b-256 | b86e9d14fc1a75de046f9dca274bd9afd963559f112108606d8e78ab1aa22251 |
Hashes for ldafork-1.2.0-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c6d3c7f1f5e087fac7890e7aa4792812e8a964a92f7eaced10727d6da54ef02 |
|
MD5 | 593e6ccafa552ce4c2317f17091fdd45 |
|
BLAKE2b-256 | 9e69db96e4dc9491eaab0c57e4ce8d264d9bb355ee6a22aea86e6703b5794eee |
Hashes for ldafork-1.2.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61e38e1a34510a8eab3a1d075fb18ad374765898df475ca49267f2f80889db1b |
|
MD5 | 95296f9cef7e47c38164a02c9f74123d |
|
BLAKE2b-256 | f7863b92084b0abac056628061141c5257193b819fd5e659c56dec55198246e8 |
Hashes for ldafork-1.2.0-cp38-cp38-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58c747b7f898ec40e68b1726e83a01c3831e3201f3fe65f0572bde1b301671f8 |
|
MD5 | 8b0b3ef3c30c408e4b8d7731bcffc1ef |
|
BLAKE2b-256 | 690750bb26f7816c413bc9d6bcca6aac0c3ad2c5ccce1fb6ea0ebf8b3349342b |
Hashes for ldafork-1.2.0-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a179376d1bab1e9fa24abd80919f1f7e45611fc25e2b8ead37319030bb1a75f |
|
MD5 | cfd377d403f9596f591cce73b2a7de96 |
|
BLAKE2b-256 | 32bfe46345d7ffa2dd34e96c28f2136f2ddfbcecc92aaa27e4affcfab6ecb9af |
Hashes for ldafork-1.2.0-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8961fa74eda2931e7c709878ec2cbd1761d9edfe39e2fb14d07abc4501ac9115 |
|
MD5 | c98b587cd5affc5d97df6b827e9fa8fc |
|
BLAKE2b-256 | 806ba33de36873013daad04ee01554b2870c20d0171a0378800f915cf1a587c4 |
Hashes for ldafork-1.2.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c13eeafc247434196dd8b8630033d906ce7f7512ba73b6ca2b4c6068aa60eda0 |
|
MD5 | 6a9c7d6732cc8afd268fc00bbd5c15d3 |
|
BLAKE2b-256 | d5e873ba42aec85fe14f463993542bbd1b3c76cab7624dda3c324a03b495230d |
Hashes for ldafork-1.2.0-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3e7c8eb05541da30730a8eff575d515ef7350fdf8236b1c16322a878f67d115 |
|
MD5 | c30f3e5909350564408dc88f18af02af |
|
BLAKE2b-256 | 41618f45c2db4f2b7c9e5f62522bee60a9f2ed79a285622a01a0e54c579b4553 |
Hashes for ldafork-1.2.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14b42beba7a0c028d36d57bcb857ca06673fd0526eab666ee5d1c51dcd19e325 |
|
MD5 | 67c8f61a4551baa96de45a8eae2c85c8 |
|
BLAKE2b-256 | e474f22931ffd8c791f680b147b3756d274cbaa70381e68c644f5b863d7169a0 |
Hashes for ldafork-1.2.0-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ab463d0ffd9797523be57abc27ea9252c23dba861753908a94626084dd4dc70 |
|
MD5 | 37e857ed8c6ec028b8e1bcec1ee9b103 |
|
BLAKE2b-256 | a77610cf4c7169ba494fce62da8fe86e2346984d970ca669dcbff5bc7d2a95e7 |
Hashes for ldafork-1.2.0-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b8df294513f0483b1865a00c0c8c44077ef9730217a0dfd116008bb0bc12234 |
|
MD5 | 3e02082fd62b11e2727da0d6d9ae15cb |
|
BLAKE2b-256 | e045ab714186841e99982d518b690966b8defbfef629d587c0353714ed8c126f |
Hashes for ldafork-1.2.0-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 531dbf8b604bf471a07b4303bda6a309e99985cb8390d1074d770b1fa7099051 |
|
MD5 | 177344dd3b1d4ddc27144fcfb5aa2ef7 |
|
BLAKE2b-256 | 624808d522e78d46b41ce997e5812c510bae2f4b6a8ad2a6bb579c64656ff953 |
Hashes for ldafork-1.2.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b25c6a8cfb8f03a8b9cf5f6bba76118e44b3a455135473236540f85a0288705 |
|
MD5 | 6dd3ed1496c8257ae392f2579948d83b |
|
BLAKE2b-256 | cf01f0afb4dd408ceb33e97903a8fb5f05d8dba682ddb862879a0023d3f7876f |
Hashes for ldafork-1.2.0-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a96ee6986ac789cd930f52acd561b118c16ebf9dc58e183ed6e4a0abd55ea065 |
|
MD5 | 6059ada41ee04bf4defcffe03e46013a |
|
BLAKE2b-256 | 0bea41cec5c20c4340375c4b591d2c0f93f2a5865227b28677be76e200a26659 |
Hashes for ldafork-1.2.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7960366442be2b545b030a56d96ecb8c3e2d6f6accacaf36b11d1e63c5808f9 |
|
MD5 | 19dfdf4911f1f25c1913e6da7533dd9e |
|
BLAKE2b-256 | a152f4a08b2d71168dec25eb45abf644009d8ddaaba5adacb24daa65feebae96 |
Hashes for ldafork-1.2.0-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c334172312dccb88539403a00716a49222d1ec18ba7b951c504bf18321167fff |
|
MD5 | 165a0695779bb071cab612f9ef4622a9 |
|
BLAKE2b-256 | b2a7e92b99d4f6f7bde712d734f7c4f44e6271ef3a4ddcda1c9bd2fe8d791cb8 |
Hashes for ldafork-1.2.0-cp36-cp36m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f0b0d53b18b4a13192261199ca7c73c0a6fb5a79ec74a4eeba792684987c6e5 |
|
MD5 | 4ffc92db75b3e2495e51293291fb5029 |
|
BLAKE2b-256 | f22e0f7857e08a19a5911057bcafcf5f9af19bb7ed8c9e4d875e4ca8be4238da |
Hashes for ldafork-1.2.0-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8358d05e91ab0314d159bfcf75715a7a00bbce849ca9e8e654aaac26dcba5f7f |
|
MD5 | a9edbbc2e079981c23cd5c8edb133d65 |
|
BLAKE2b-256 | 4cada9e518557fbb5b954eaca18221d900b96d6f62c65d4a848032a7ec137b03 |