Skip to main content

pypi version github actions build status Zenodo citation

NOTE: This package is in maintenance mode. Critical bugs will be fixed. No new features will be added.

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 ≥3.10 and NumPy.

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.

Metadata

Release files for lda 3.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lda 3.0.2
File Size Uploaded
lda-3.0.2.tar.gz 165.7 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for lda 3.0.2
File
lda-3.0.2-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details
lda-3.0.2-cp312-cp312-musllinux_1_2_x86_64.whl CPython 3.12 CPython 3.12 Linux musl 1.2+ x86-64 Details
lda-3.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.17+ x86-64 Details
lda-3.0.2-cp312-cp312-macosx_14_0_arm64.whl CPython 3.12 CPython 3.12 macOS 14.0+ ARM64 Details
lda-3.0.2-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
lda-3.0.2-cp311-cp311-musllinux_1_2_x86_64.whl CPython 3.11 CPython 3.11 Linux musl 1.2+ x86-64 Details
lda-3.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.17+ x86-64 Details
lda-3.0.2-cp311-cp311-macosx_14_0_arm64.whl CPython 3.11 CPython 3.11 macOS 14.0+ ARM64 Details
lda-3.0.2-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details
lda-3.0.2-cp310-cp310-musllinux_1_2_x86_64.whl CPython 3.10 CPython 3.10 Linux musl 1.2+ x86-64 Details
lda-3.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.17+ x86-64 Details
lda-3.0.2-cp310-cp310-macosx_14_0_arm64.whl CPython 3.10 CPython 3.10 macOS 14.0+ ARM64 Details

Total release size: 4.2 MB

Release files / lda-3.0.2.tar.gz

Download URL lda-3.0.2.tar.gz
Size 165.7 kB
Tags Source
SHA-256 checksum
How to use checksums
76fc6fbb066b6d1ec0360a1541c5e1c8b69a728666525e72644c4d5332fc778a
BLAKE2b-256 checksum
How to use checksums
dd46ffc9667172d794fd17daaf296c684ec8ed0d2a3fe9d557014407dfed64fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Darwin/23.5.0

Release files / lda-3.0.2-cp312-cp312-win_amd64.whl

Download URL lda-3.0.2-cp312-cp312-win_amd64.whl
Size 380.2 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
178b07b5d37505d4d63fe57b1bcd7208254232dd720b5d9ba0054f9f55f55488
BLAKE2b-256 checksum
How to use checksums
96b45d9c4497549fc2bb1f9bdaeab34703f7f2da135f26e3f27b5571fb29aba1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Windows/2022Server

Release files / lda-3.0.2-cp312-cp312-musllinux_1_2_x86_64.whl

Download URL lda-3.0.2-cp312-cp312-musllinux_1_2_x86_64.whl
Size 375.2 kB
Tags CPython 3.12 Linux musl 1.2+ x86-64
SHA-256 checksum
How to use checksums
20028b96fce4b336b390d9a208916e4ab86588724e99a96b9c29b748fa6753b9
BLAKE2b-256 checksum
How to use checksums
90f591db582f2a56eb34dbc49d28c26a8fbcbc244631a05fe7acefd96b8ba6bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-1024-azure

Release files / lda-3.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL lda-3.0.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 368.1 kB
Tags CPython 3.12 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
5b2f40fdbf221192c48e1628e0a3f815247e64d27daf8cee5f7b3106d70ce0a7
BLAKE2b-256 checksum
How to use checksums
c6d0ffabbf59deae9ef776d9843e04ab865399f627ed35fadbde0cc8250f2ac2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-1024-azure

Release files / lda-3.0.2-cp312-cp312-macosx_14_0_arm64.whl

Download URL lda-3.0.2-cp312-cp312-macosx_14_0_arm64.whl
Size 274.5 kB
Tags CPython 3.12 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
4012f7e834ecb7e0be8a046b3c42c149c3124f27099ba97b08b0feea4a0ab751
BLAKE2b-256 checksum
How to use checksums
88fbf2dc4cdfbbb8ed5ca75c5a45a816292186413aad75705d83c91814f8bec8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Darwin/23.5.0

Release files / lda-3.0.2-cp311-cp311-win_amd64.whl

Download URL lda-3.0.2-cp311-cp311-win_amd64.whl
Size 355.9 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
bb41865290b5f1f19b065d58fa4005f08e71947a01dde7f598f975cba3dd2294
BLAKE2b-256 checksum
How to use checksums
4ba028c2b275303dcb3e69b570a8e2d42270edea45ae434ac36300cf8429f39e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Windows/2022Server

Release files / lda-3.0.2-cp311-cp311-musllinux_1_2_x86_64.whl

Download URL lda-3.0.2-cp311-cp311-musllinux_1_2_x86_64.whl
Size 352.6 kB
Tags CPython 3.11 Linux musl 1.2+ x86-64
SHA-256 checksum
How to use checksums
68b95a93cc6c5d7c5c5d391a6e33e2dd97d38093380671d831976e2cf762c0a8
BLAKE2b-256 checksum
How to use checksums
dc1da1651b3a261af3f58034dc0773846802f1abd242e1d30d6881d04ddbdbfa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-1024-azure

Release files / lda-3.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL lda-3.0.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 347.7 kB
Tags CPython 3.11 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
5092577d2213890f0e1df4e6db02cc50955de97ca94bd57b3405dd53e21c1ab3
BLAKE2b-256 checksum
How to use checksums
be0d8d86a51d49d87bfae2edebe660589cee02423df08bb4322edeac5b9fccfd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-1024-azure

Release files / lda-3.0.2-cp311-cp311-macosx_14_0_arm64.whl

Download URL lda-3.0.2-cp311-cp311-macosx_14_0_arm64.whl
Size 267.9 kB
Tags CPython 3.11 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
d8b4773bed0b1428230ead6bd7ec223861f4189971a28e2fe7c9501452810f27
BLAKE2b-256 checksum
How to use checksums
c153aa93fcd3d7995e3c613701b29f33251c5680f5c294e698de74fbdb37539a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Darwin/23.5.0

Release files / lda-3.0.2-cp310-cp310-win_amd64.whl

Download URL lda-3.0.2-cp310-cp310-win_amd64.whl
Size 356.4 kB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
61d7b27aa3759c113e8a4deedfdc86a3fcbbed2cae64fe96c68e3eaadcdb78e8
BLAKE2b-256 checksum
How to use checksums
a3e3b8759d381ede320c949073cba778b4bb37ebb61a148876e6897b8ca7016a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Windows/2022Server

Release files / lda-3.0.2-cp310-cp310-musllinux_1_2_x86_64.whl

Download URL lda-3.0.2-cp310-cp310-musllinux_1_2_x86_64.whl
Size 354.3 kB
Tags CPython 3.10 Linux musl 1.2+ x86-64
SHA-256 checksum
How to use checksums
a92c6862476a640014fd7f891a579b446c41b4f3af68861a8f8179990ffa2c85
BLAKE2b-256 checksum
How to use checksums
2a027f6d4d0f9f92b940461a6a023d42dd5674fba78b1439a092232f24afa635
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-1024-azure

Release files / lda-3.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL lda-3.0.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 350.0 kB
Tags CPython 3.10 Linux glibc 2.17+ x86-64
SHA-256 checksum
How to use checksums
69783191736d1b6253edc84d0c2fa2f4189ef04c18680201c0d77866cad38f62
BLAKE2b-256 checksum
How to use checksums
b4c208572d76335ac6f4b77a67811eceaa79887b874292c6784cfbe6a4f905b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Linux/6.5.0-1024-azure

Release files / lda-3.0.2-cp310-cp310-macosx_14_0_arm64.whl

Download URL lda-3.0.2-cp310-cp310-macosx_14_0_arm64.whl
Size 270.0 kB
Tags CPython 3.10 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
3a6dfc8d3aede1f4d8debde89dca0d871066b756bb43bc6b6a8062ca81555ec2
BLAKE2b-256 checksum
How to use checksums
3c00fb9f066ae18294f96305169e38018db6b1b70f0c0921284824425d90e53d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.4 Darwin/23.5.0
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page