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)
π Parametersο
Parameter |
Type |
Default |
Description |
|---|---|---|---|
n_topics |
|
|
Number of latent topics in the corpus. |
alpha |
|
|
Prior for document-topic distribution. |
beta |
|
|
Prior for topic-word distribution. |
max_iter |
|
|
Maximum EM iterations. |
var_max_iter |
|
|
Max variational steps per document. |
tol |
|
|
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]orcsr_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.
get_topic_word_dist()ο
Returns the normalized topic-word distribution matrix.
Returns:
topic_word(ndarrayof shape(n_topics, V)): Each row is a probability distribution over the vocabulary for a topic.
print_top_words(n_top_words=10)ο
Prints the top words in each topic based on their probabilities.
Parameters:
n_top_words(int): Number of top words to display per topic.
Returns:
None
π Referenceο
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)