Python wrapper for Latent Dirichlet Allocation (LDA) from MALLET, the Java topic modelling toolkit .
This module allows both LDA model estimation from a training corpus and inference of topic distribution on new, unseen documents, using an (optimized version of) collapsed gibbs sampling from MALLET.
MALLET’s LDA training requires O(#corpus_words) of memory, keeping the entire corpus in RAM. If you find yourself running out of memory, either decrease the workers constructor parameter, or use LdaModel which needs only O(1) memory.
The wrapped model can NOT be updated with new documents for online training – use gensim’s LdaModel for that.
>>> model = gensim.models.LdaMallet('/Users/kofola/mallet-2.0.7/bin/mallet', corpus=my_corpus, num_topics=20, id2word=dictionary) >>> print model[my_vector] # print LDA topics of a document
Class for LDA training using MALLET. Communication between MALLET and Python takes place by passing around data files on disk and calling Java with subprocess.call().
mallet_path is path to the mallet executable, e.g. /home/kofola/mallet-2.0.7/bin/mallet. corpus is a gensim corpus, aka a stream of sparse document vectors. id2word is a mapping between tokens ids and token. workers is the number of threads, for parallel training. prefix is the string prefix under which all data files will be stored; default: system temp + random filename prefix. optimize_interval optimize hyperparameters every N iterations (sometimes leads to Java exception; 0 to switch off hyperparameter optimization). iterations is the number of sampling iterations.
Serialize documents (lists of unicode tokens) to a temporary text file, then convert that text file to MALLET format outfile.
Load a previously saved object from file (also see save).
If the object was saved with large arrays stored separately, you can load these arrays via mmap (shared memory) using mmap=’r’. Default: don’t use mmap, load large arrays as normal objects.
Save the object to file (also see load).
If separately is None, automatically detect large numpy/scipy.sparse arrays in the object being stored, and store them into separate files. This avoids pickle memory errors and allows mmap’ing large arrays back on load efficiently.
You can also set separately manually, in which case it must be a list of attribute names to be stored in separate files. The automatic check is not performed in this case.
ignore is a set of attribute names to not serialize (file handles, caches etc). On subsequent load() these attributes will be set to None.
Print the num_words most probable words for num_topics number of topics. Set num_topics=-1 to print all topics.
Set formatted=True to return the topics as a list of strings, or False as lists of (weight, word) pairs.
Yield document topic vectors from MALLET’s “doc-topics” format, as sparse gensim vectors.