Latent Dirichlet Allocation (LDA)

This module implements Latent Dirichlet Allocation (LDA) using variational EM inference.


πŸ” Overview

Latent Dirichlet Allocation (LDA) is a generative probabilistic model for collections of discrete data such as text corpora. It discovers hidden topic structures in the documents, with each topic being a distribution over words, and each document a distribution over topics.


βš™οΈ Class Definition

class soft_clustering.LDA(
    n_topics: int = 10,
    alpha: float = None,
    beta: float = 0.01,
    max_iter: int = 100,
    var_max_iter: int = 20,
    tol: float = 1e-4)

πŸ”— Source on GitHub


πŸ“‹ Parameters

Parameter

Type

Default

Description

n_topics

int

10

Number of latent topics in the corpus.

alpha

float

None

Prior for document-topic distribution.

beta

float

0.01

Prior for topic-word distribution.

max_iter

int

100

Maximum EM iterations.

var_max_iter

int

20

Max variational steps per document.

tol

float

1e-4

Convergence threshold for stopping EM iterations.


πŸš€ Usage Examples

from soft_clustering import LDA

# Sample documents
docs = [
    "apple banana apple",
    "banana fruit apple",
    "fruit banana banana"
]

# Initialize and fit the model
model = LDA(n_topics=2, max_iter=20, var_max_iter=10)
model.fit(docs)

# Print top words in each topic
model.print_top_words(n_top_words=5)

πŸ› οΈ Methods

fit(X, vocabulary=None)

Fits the LDA model to a corpus using variational EM inference.

Parameters:

  • X (list[str] or csr_matrix): Input documents as raw strings or a precomputed term-document matrix.

  • vocabulary (list[str], optional): Fixed vocabulary to use when building the term-document matrix.

Returns:

  • self (LDA): The trained model instance.

πŸ”— Source definition

get_topic_word_dist()

Returns the normalized topic-word distribution matrix.

Returns:

  • topic_word (ndarray of shape (n_topics, V)): Each row is a probability distribution over the vocabulary for a topic.

πŸ”— Source definition


πŸ“š Reference

  1. Blei, D. M., Ng, A. Y., & Jordan, M. I. (2003). Latent Dirichlet Allocation. Journal of Machine Learning Research, 3, 993–1022. (https://jmlr.org/papers/v3/blei03a.html)