Topic discovery
Discover latent themes in a collection of documents.
Scikit-Learn · Class
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.
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.
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)| Name | Type | Description |
|---|---|---|
n_components | int, default=10 | Number of topics. |
doc_topic_prior | float, default=None | Prior of document-topic distribution. |
topic_word_prior | float, default=None | Prior of topic-word distribution. |
learning_method | {'batch', 'online'}, default='batch' | Update strategy: 'batch' or 'online'. |
learning_decay | float, default=0.7 | Online learning-rate decay. |
learning_offset | float, default=10.0 | Online learning-rate offset. |
max_iter | int, default=10 | Maximum number of iterations. |
batch_size | int, default=128 | Number of documents in each mini-batch. |
evaluate_every | int, default=-1 | Frequency of perplexity evaluation. |
total_samples | int, default=1000000 | Estimated total number of training samples. |
perp_tol | float, default=0.1 | Perplexity convergence tolerance. |
mean_change_tol | float, default=0.001 | Convergence tolerance for mean parameter change. |
A fitted LatentDirichletAllocation estimator from fit; transform returns document-topic coordinates; fit_transform returns the transformed training data.
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.
Discover latent themes in a collection of documents.
Represent documents as mixtures over learned topics.
Process data that arrives in sequential mini-batches.
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.
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).
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.NMFsklearn.decomposition.TruncatedSVDsklearn.decomposition.LatentDirichletAllocation.fitsklearn.decomposition.LatentDirichletAllocation.transformsklearn.decomposition.LatentDirichletAllocation.partial_fit