Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

LatentDirichletAllocation

LatentDirichletAllocation is a topic-model estimator that learns latent topics from a document-term matrix using variational Bayes. It supports batch and online training and can transform documents into topic distributions.

Since
0.17

How it works

The estimator learns topic-word weights from a non-negative document-term matrix with variational Bayesian inference. After fitting, transform maps documents to topic mixtures, while components_ stores the learned topic-term weights; batch and online updates support full-data and mini-batch training.

Syntax

class sklearn.decomposition.LatentDirichletAllocation(n_components=10, *, doc_topic_prior=None, topic_word_prior=None, learning_method='batch', learning_decay=0.7, learning_offset=10.0, max_iter=10, batch_size=128, evaluate_every=-1, total_samples=1000000.0, perp_tol=0.1, mean_change_tol=0.001, max_doc_update_iter=100, n_jobs=None, verbose=0, random_state=None)

Parameters

NameTypeDescription
n_componentsint, default=10Number of topics.
doc_topic_priorfloat, default=NonePrior of document-topic distribution.
topic_word_priorfloat, default=NonePrior of topic-word distribution.
learning_method{'batch', 'online'}, default='batch'Update strategy: 'batch' or 'online'.
learning_decayfloat, default=0.7Online learning-rate decay.
learning_offsetfloat, default=10.0Online learning-rate offset.
max_iterint, default=10Maximum number of iterations.
batch_sizeint, default=128Number of documents in each mini-batch.
evaluate_everyint, default=-1Frequency of perplexity evaluation.
total_samplesint, default=1000000Estimated total number of training samples.
perp_tolfloat, default=0.1Perplexity convergence tolerance.
mean_change_tolfloat, default=0.001Convergence tolerance for mean parameter change.

Returns

A fitted LatentDirichletAllocation estimator from fit; transform returns document-topic coordinates; fit_transform returns the transformed training data.

Key points

  • The estimator models latent topics with online variational Bayes.
  • n_components sets how many topics the model learns.
  • learning_method selects batch or online parameter updates.
  • For online learning, learning_decay and learning_offset govern the learning-rate schedule.
  • The learned topic-term representation is stored in components_.
  • transform produces document-topic representations for input documents.
  • partial_fit supports incremental training when data is obtained sequentially.

Common mistakes

  • The online method is intended for mini-batch updates; use partial_fit when data arrives sequentially.
  • Online training's result and convergence speed can vary with mini-batch size and learning-rate settings.

Example: Fit and transform documents

from sklearn.datasets import make_multilabel_classification
from sklearn.decomposition import LatentDirichletAllocation

X, _ = make_multilabel_classification(random_state=0)
lda = LatentDirichletAllocation(n_components=5, random_state=0)
lda.fit(X)
document_topics = lda.transform(X[:2])
print(document_topics.shape)
print(lda.components_.shape)

Fits five topics to a non-negative document-term-like matrix, then obtains document-topic representations.

When to use it

Topic discovery

Discover latent themes in a collection of documents.

Document representation

Represent documents as mixtures over learned topics.

Incremental topic modeling

Process data that arrives in sequential mini-batches.

Interview questions

QWhat does LatentDirichletAllocation learn and produce?

It is a topic-model estimator that uses online variational Bayes and supports both batch and online updates. For a document-term matrix, it learns topic-term weights and can infer document-topic representations.

Follow-up: How would you train on sequentially available data? Use partial_fit, which the documentation identifies for data fetched sequentially.

QHow do components_ and transform relate?

components_ stores the topic-term matrix learned from the input. transform computes the document-topic matrix for documents.

Follow-up: What is the corresponding matrix shape? components_ has shape (n_components, n_features), while transformed data has shape (n_samples, n_components).

QWhat is the difference between batch and online learning?

learning_method controls updates: batch uses full passes through the data, while online uses mini-batches. learning_decay and learning_offset control the online learning rate.

Follow-up: When is online training especially useful? It is useful when data can be fetched sequentially and processed incrementally with partial_fit.

  • sklearn.decomposition.NMF
  • sklearn.decomposition.TruncatedSVD
  • sklearn.decomposition.LatentDirichletAllocation.fit
  • sklearn.decomposition.LatentDirichletAllocation.transform
  • sklearn.decomposition.LatentDirichletAllocation.partial_fit

Source: LatentDirichletAllocation — scikit-learn 1.9.1 documentation. Checked 2026-10-15.