topicIdx = predict(ldaMdl,documents)
returns the LDA topic indices with the largest probabilities for
documents based on the LDA model
ldaMdl.

topicIdx = predict(ldaMdl,bag)
returns the LDA topic indices with the largest probabilities for the documents
represented by a bag-of-words or bag-of-n-grams model.

topicIdx = predict(ldaMdl,counts)
returns the LDA topic indices with the largest probabilities for the documents
represented by a matrix of word counts.

To reproduce the results in this example, set rng to 'default'.

rng('default')

Load the example data. The file sonnetsPreprocessed.txt contains preprocessed versions of Shakespeare's sonnets. The file contains one sonnet per line, with words separated by a space. Extract the text from sonnetsPreprocessed.txt, split the text into documents at newline characters, and then tokenize the documents.

Predict the top topics for an array of new documents.

newDocuments = tokenizedDocument([
"what's in a name? a rose by any other name would smell as sweet.""if music be the food of love, play on."]);
topicIdx = predict(mdl,newDocuments)

Load the example data. sonnetsCounts.mat contains a matrix of word counts and a corresponding vocabulary of preprocessed versions of Shakespeare's sonnets.

load sonnetsCounts.mat
size(counts)

ans = 1×2
154 3092

Fit an LDA model with 20 topics. To reproduce the results in this example, set rng to 'default'.

To reproduce the results in this example, set rng to 'default'.

rng('default')

Load the example data. The file sonnetsPreprocessed.txt contains preprocessed versions of Shakespeare's sonnets. The file contains one sonnet per line, with words separated by a space. Extract the text from sonnetsPreprocessed.txt, split the text into documents at newline characters, and then tokenize the documents.

Predict the top topics for a new document. Specify the iteration limit to be 200.

newDocument = tokenizedDocument("what's in a name? a rose by any other name would smell as sweet.");
iterationLimit = 200;
[topicIdx,scores] = predict(mdl,newDocument, ...'IterationLimit',iterationLimit)

documents — Input documents tokenizedDocument array | string array of words | cell array of character vectors

Input documents, specified as a tokenizedDocument array, a string array of words, or a cell array of
character vectors. If documents is a
tokenizedDocument, then it must be a column vector. If
documents is a string array or a cell array of character
vectors, then it must be a row of the words of a single document.

Tip

To ensure that the function does not discard useful information, you must
first preprocess the input documents using the same steps used to preprocess the
documents used to train the model.

bag — Input model bagOfWords object | bagOfNgrams object

Input bag-of-words or bag-of-n-grams model, specified as a bagOfWords object or a bagOfNgrams object. If bag is a
bagOfNgrams object, then the function treats the n-grams as
individual words.

counts — Frequency counts of words matrix of nonnegative integers

Frequency counts of words, specified as a matrix of nonnegative integers. If you specify
'DocumentsIn' to be 'rows', then the value
counts(i,j) corresponds to the number of times the
jth word of the vocabulary appears in the ith
document. Otherwise, the value counts(i,j) corresponds to the number
of times the ith word of the vocabulary appears in the
jth document.

Name-Value Pair Arguments

Specify optional
comma-separated pairs of Name,Value arguments. Name is
the argument name and Value is the corresponding value.
Name must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN.

Example: 'IterationLimit',200 specifies the iteration limit to
be 200.

'DocumentsIn' — Orientation of documents 'rows' (default) | 'columns'

Orientation of documents in the word count matrix, specified as the comma-separated pair
consisting of 'DocumentsIn' and one of the following:

'rows' – Input is a matrix of word counts with rows corresponding
to documents.

'columns' – Input is a transposed matrix of word counts with
columns corresponding to documents.

This option only applies if you specify the input documents as a matrix of word
counts.

Note

If you orient your word count matrix so that documents correspond to columns and specify
'DocumentsIn','columns', then you might experience a significant
reduction in optimization-execution time.

'IterationLimit' — Maximum number of iterations 100 (default) | positive integer

Maximum number of iterations, specified as the comma-separated pair consisting of 'IterationLimit' and a positive integer.

Relative tolerance on log-likelihood, specified as the comma-separated pair consisting
of 'LogLikelihoodTolerance' and a positive scalar. The optimization
terminates when this tolerance is reached.

topicIdx — Predicted topic indices vector of numeric indices

Predicted topic indices, returned as a vector of numeric indices.

score — Predicted topic probabilities matrix

Predicted topic probabilities, returned as a
D-by-K matrix, where
D is the number of input documents and
K is the number of topics in the LDA model.
score(i,j) is the probability that topic
j appears in document i. Each row
of score sums to one.

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.