LatentDirichletAllocation
- class sklearn.decomposition.LatentDirichletAllocation(n_components=10, *, doc_topic_prior=None, topic_word_prior=None, learning_method='batch', learning_decay=0.7, learning_offset, , , , , , , , , ,
Latent Dirichlet Allocation with online variational Bayes algorithm.
The implementation is based on [1] and [2].
Read more in the User Guide.
Number of topics.
Prior of document topic distribution theta. If the value is None,
defaults to 1 / n_components.
In [1], this is called alpha.
Prior of topic word distribution beta. If the value is None, defaults
to 1 / n_components.
In [1], this is called eta.
Method used to update _component. Only used in fit method.
In general, if the data size is large, the online update will be much
faster than the batch update.
Valid options:
‘batch’: Batch variational Bayes method. Use all training data in each EM
update. Old components_ will be overwritten in each iteration.
‘online’: Online variational Bayes method. In each EM update, use mini-batch
of training data to update the components_ variable incrementally. The
learning rate is controlled by the learning_decay and the
learning_offset parameters.
It is a parameter that control learning rate in the online learning
method. The value should be set between (0.5, 1.0] to guarantee
asymptotic convergence. When the value is 0.0 and batch_size is
n_samples, the update method is same as batch learning. In the
literature, this is called kappa.
A (positive) parameter that downweights early iterations in online learning. It should be greater than 1.0. In the literature, this is called tau_0.
The maximum number of passes over the training data (aka epochs).
It only impacts the behavior in the fit method, and not the
partial_fit method.
Number of documents to use in each EM iteration. Only used in online learning.
How often to evaluate perplexity. Only used in fit method.
set it to 0 or negative number to not evaluate perplexity in
training at all. Evaluating perplexity can help you check convergence
in training process, but it will also increase total training time.
Evaluating perplexity in every iteration might increase training time
up to two-fold.
Total number of documents. Only used in the partial_fit method.
Perplexity tolerance. Only used when evaluate_every is greater than 0.
Stopping tolerance for updating document topic distribution in E-step.
Max number of iterations for updating document topic distribution in the E-step.
The number of jobs to use in the E-step.
None means 1 unless in a joblib.parallel_backend context.
-1 means using all processors. See Glossary
for more details.
Verbosity level.
Pass an int for reproducible results across multiple function calls. See Glossary.
Variational parameters for topic word distribution. Since the complete
conditional for topic word distribution is a Dirichlet,
components_[i, j] can be viewed as pseudocount that represents the
number of times word j was assigned to topic i.
It can also be viewed as distribution over the words for each topic
after normalization:
model.components_ / model.components_.sum(axis=1)[:, np.newaxis].
Exponential value of expectation of log topic word distribution.
In the literature, this is exp(E[log(beta)]).
“Online Learning for Latent Dirichlet Allocation”, Matthew D. Hoffman, David M. Blei, Francis Bach, 2010. blei-lab/onlineldavb
“Stochastic Variational Inference”, Matthew D. Hoffman, David M. Blei, Chong Wang, John Paisley, 2013. https://jmlr.org/papers/volume14/hoffman13a/hoffman13a.pdf
>>> from sklearn.decomposition import LatentDirichletAllocation
>>> from sklearn.datasets import make_multilabel_classification
>>> # This produces a feature matrix of token counts, similar to what
>>> # CountVectorizer would produce on text.
>>> X, _ = make_multilabel_classification(random_state=0)
>>> lda = LatentDirichletAllocation(
Learn model for the data X with variational Bayes method.
When learning_method is ‘online’, use mini-batch update.
Otherwise, use batch update.
Document word matrix.
Not used, present here for API consistency by convention.
Fit to data, then transform it.
Fits transformer to X and y and returns a transformed version of X.
Get output feature names for transformation.
The feature names out will prefixed by the lowercased class name. For
example, if the transformer outputs 3 features, then the feature names
out are: ["class_name0", "class_name1", "class_name2"].
Only used to validate feature names with the names seen in fit.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
A MetadataRequest encapsulating
routing information.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Online VB with Mini-Batch update.
Document word matrix.
Not used, present here for API consistency by convention.
Partially fitted estimator.
Calculate approximate perplexity for data X.
Perplexity is defined as exp(-1. * log-likelihood per word)
Document word matrix.
Calculate approximate log-likelihood as score.
Document word matrix.
Not used, present here for API consistency by convention.
Set output container.
Refer to the user guide for more details and Introducing the set_output API for an example on how to use the API.
Configure output of transform and .
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as Pipeline). The latter have
parameters of the form <component>__<parameter> so that it’s
possible to update each component of a nested object.
Estimator parameters.
Configure whether metadata should be requested to be passed to the method.
Transform data X according to the fitted model.
Document word matrix.
Topic extraction with Non-negative Matrix Factorization and Latent Dirichlet Allocation
Changed in version 0.20: The default learning method is now "batch".
Number of iterations of the EM step.
Number of features seen during fit.
n_features_in_,)Names of features seen during fit. Defined only when X
has feature names that are all strings.
Number of passes over the dataset.
Final perplexity score on training set.
Prior of document topic distribution theta. If the value is None,
it is 1 / n_components.
RandomState instance that is generated either from a seed, the random
number generator or by np.random.
Prior of topic word distribution beta. If the value is None, it is
1 / n_components.
Fitted estimator.
Input samples.
Target values (None for unsupervised transformations).
Whether to normalize the document topic distribution in transform.
Transformed array.
Transformed feature names.
Do sub-sampling or not.
Perplexity score.
Use approximate bound as score.
"default": Default output format of a transformer
"pandas": DataFrame output
"polars": Polars output
None: Transform configuration is unchanged
Estimator instance.
Estimator instance.
Note that this method is only relevant when this estimator is used as a
sub-estimator within a meta-estimator and metadata routing is enabled
with enable_metadata_routing=True (see sklearn.set_config).
Please check the User Guide on how the routing
mechanism works.
The options for each parameter are:
True: metadata is requested, and passed to transform if provided. The request is ignored if metadata is not provided.
False: metadata is not requested and the meta-estimator will not pass it to transform.
None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (sklearn.utils.metadata_routing.UNCHANGED) retains the
existing request. This allows you to change the request for some
parameters and not others.
Metadata routing for normalize parameter in transform.
The updated object.
Whether to normalize the document topic distribution.
Document topic distribution for X.