mojolearn.LatentDirichletAllocation#

class mojolearn.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, numeric_mode=None)[source]#

Bases: _Base

Latent Dirichlet allocation topic model, fitted by variational EM.

Follows scikit-learn’s LatentDirichletAllocation. X is a document-term count matrix (non-negative; a scipy.sparse matrix is densified). Random draws come from mojolearn’s own generator, so topics differ from scikit-learn’s for the same seed. Online learning takes the mini-batches in row order.

Parameters:
  • n_components (int, default 10) – Number of topics.

  • doc_topic_prior (float or None, default None) – Prior on the document-topic distribution; None uses 1 / n_components.

  • topic_word_prior (float or None, default None) – Prior on the topic-word distribution; None uses 1 / n_components.

  • learning_method ({“batch”, “online”}, default “batch”) – Use all data in each update, or mini-batches.

  • learning_decay (float, default 0.7) – Online learning rate decay.

  • learning_offset (float, default 10.0) – Online learning rate offset (down-weights early iterations).

  • max_iter (int, default 10) – Maximum passes over the data.

  • batch_size (int, default 128) – Documents per mini-batch for online learning; must be a positive int.

  • evaluate_every (int, default -1) – Compute perplexity every this many iterations and stop once it improves by less than perp_tol; 0 or negative never evaluates.

  • total_samples (float, default 1e6) – Total number of documents, used by partial_fit and perplexity(sub_sampling=True).

  • perp_tol (float, default 0.1) – Perplexity tolerance for evaluate_every.

  • mean_change_tol (float, default 1e-3) – Stopping tolerance of the per-document E-step.

  • max_doc_update_iter (int, default 100) – Maximum E-step iterations per document.

  • n_jobs (ignored)

  • verbose (ignored)

  • random_state (int or None, default None) – Seed; None means 0.

  • numeric_mode ({“identical”, “fast”} or None, default None) – Numeric mode for this estimator. None uses the package default ("identical" unless changed). "identical" gives the same bits on NVIDIA and AMD GPUs; "fast" applies on Apple GPUs.

Variables:
  • components_ (ndarray of shape (n_components, n_features)) – Variational topic-word parameters.

  • exp_dirichlet_component_ (ndarray of shape (n_components, n_features))

  • doc_topic_prior_ (float)

  • topic_word_prior_ (float)

  • bound_ (float) – Final perplexity on the training data.

  • n_iter_ (int)

  • n_batch_iter_ (int)

  • n_features_in_ (int)

Raises:

ValueError – If X has negative values, or learning_method or batch_size is invalid.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).integers(0, 5, (200, 30)).astype(np.float32)
>>> lda = mojolearn.LatentDirichletAllocation(n_components=4, random_state=0).fit(X)
>>> doc_topics = lda.transform(X)
__init__(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, numeric_mode=None)[source]#

Methods

fit(X[, y])

Fit the topic model to a document-term matrix.

fit_transform(X[, y, normalize])

Fit the model, then return transform(X, normalize=normalize).

get_params([deep])

Return this estimator's constructor parameters.

partial_fit(X[, y])

Update the model with one batch of documents (online learning).

perplexity(X[, sub_sampling])

Return the approximate perplexity of X.

score(X[, y])

Return the approximate log-likelihood (variational bound) of X.

set_params(**params)

Set constructor parameters by keyword.

transform(X, *[, normalize])

Return the topic distribution of each document.

fit(X, y=None)[source]#

Fit the topic model to a document-term matrix.

Parameters:
  • X (array-like or scipy.sparse matrix of shape (n_samples, n_features)) – Non-negative word counts.

  • y (ignored)

Returns:

self

partial_fit(X, y=None)[source]#

Update the model with one batch of documents (online learning).

Parameters:
  • X (array-like or scipy.sparse matrix of shape (n_samples, n_features)) – Non-negative word counts, split into mini-batches of batch_size.

  • y (ignored)

Returns:

self

transform(X, *, normalize=True)[source]#

Return the topic distribution of each document.

Parameters:
  • X (array-like or scipy.sparse matrix of shape (n_samples, n_features)) – Non-negative word counts.

  • normalize (bool, default True) – Normalize each row to sum to 1.

Returns:

ndarray of shape (n_samples, n_components), float32

fit_transform(X, y=None, *, normalize=True)[source]#

Fit the model, then return transform(X, normalize=normalize).

Parameters:
  • X (array-like or scipy.sparse matrix of shape (n_samples, n_features))

  • y (ignored)

  • normalize (bool, default True)

Returns:

ndarray of shape (n_samples, n_components), float32

score(X, y=None)[source]#

Return the approximate log-likelihood (variational bound) of X.

Parameters:
  • X (array-like or scipy.sparse matrix of shape (n_samples, n_features))

  • y (ignored)

Returns:

float

perplexity(X, sub_sampling=False)[source]#

Return the approximate perplexity of X.

Parameters:
  • X (array-like or scipy.sparse matrix of shape (n_samples, n_features))

  • sub_sampling (bool, default False) – Scale the bound as if X were a sample of total_samples documents.

Returns:

float – Infinity when X has no words.