Nonnegative Matrix Factorization
NMF is a factor-based topic model of bag-of-words or tf-idf representations. It treats topics as latent factors that explain variance in word counts/importances in documents. Factors are strictly positive, hence the name.
Tip
NMF is an older topic model with substantially less interpretable topics than modern approaches, and with no contextual understanding. If you want to use a model that is more powerful but similar to NMF, we recommend you try out KeyNMF or SensTopic.
By Qwertyus - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=29114677
Usage
from turftopic import NMF
documents = [...]
model = NMF(10)
doc_topic_matrix = model.fit_transform(documents)
model.print_topics()
Tip
Since NMF is a BoW topic model, it does not handle stop words particularly well. We recommend that you use a vectorizer that removes stop words or all too frequent words, potentially with tf-idf weighting
from turftopic import LDA
from sklearn.feature_extraction.text import TfidfVectorizer
model = NMF(10, vectorizer=TfidfVectorizer(min_df=10, stop_words="english", max_df=0.8))
Citation
Please cite the authors of the implementation, and Turftopic when using NMF:
@article{Cedric2011,
author = {Févotte, Cédric and Idier, Jérôme},
title = {Algorithms for Nonnegative Matrix Factorization with the β-Divergence},
journal = {Neural Computation},
volume = {23},
number = {9},
pages = {2421-2456},
year = {2011},
month = {09},
abstract = {This letter describes algorithms for nonnegative matrix factorization (NMF) with the β-divergence (β-NMF). The β-divergence is a family of cost functions parameterized by a single shape parameter β that takes the Euclidean distance, the Kullback-Leibler divergence, and the Itakura-Saito divergence as special cases (β = 2, 1, 0 respectively). The proposed algorithms are based on a surrogate auxiliary function (a local majorization of the criterion function). We first describe a majorization-minimization algorithm that leads to multiplicative updates, which differ from standard heuristic multiplicative updates by a β-dependent power exponent. The monotonicity of the heuristic algorithm can, however, be proven for β ∈ (0, 1) using the proposed auxiliary function. Then we introduce the concept of the majorization-equalization (ME) algorithm, which produces updates that move along constant level sets of the auxiliary function and lead to larger steps than MM. Simulations on synthetic and real data illustrate the faster convergence of the ME approach. The letter also describes how the proposed algorithms can be adapted to two common variants of NMF: penalized NMF (when a penalty function of the factors is added to the criterion function) and convex NMF (when the dictionary is assumed to belong to a known subspace).},
issn = {0899-7667},
doi = {10.1162/NECO_a_00168},
url = {https://doi.org/10.1162/NECO_a_00168},
eprint = {https://direct.mit.edu/neco/article-pdf/23/9/2421/852282/neco_a_00168.pdf},
}
@article{
Kardos2025,
title = {Turftopic: Topic Modelling with Contextual Representations from Sentence Transformers},
doi = {10.21105/joss.08183},
url = {https://doi.org/10.21105/joss.08183},
year = {2025},
publisher = {The Open Journal},
volume = {10},
number = {111},
pages = {8183},
author = {Kardos, Márton and Enevoldsen, Kenneth C. and Kostkan, Jan and Kristensen-McLachlan, Ross Deans and Rocca, Roberta},
journal = {Journal of Open Source Software}
}
API Reference
turftopic.models.bow.lda.LDA
Bases: ContextualModel
Turftopic wrapper around sklearns's implementation of Latent Dirichlet Allocation. Note that LDA is a Bag-of-words model and NOT a contextual model and uses static word representations! The encoder and passed embeddings will be ignored. For interpretation of hyperparameters refer to sklearn's documentation
from turftopic import LDA
corpus: list[str] = ["some text", "more text", ...]
model = LDA(10).fit(corpus)
model.print_topics()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components |
int
|
Number of topics. |
required |
encoder |
Ignored. |
None
|
|
vectorizer |
Optional[CountVectorizer]
|
Vectorizer used for term extraction. Can be used to prune or filter the vocabulary. |
None
|
random_state |
Optional[int]
|
Random state to use so that results are exactly reproducible. |
None
|
trf_kwargs |
Ignored. |
None
|
|
encode_kwargs |
Ignored. |
None
|
Source code in turftopic/models/bow/lda.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
transform(raw_documents, embeddings=None)
Infers topic importances for new documents based on a fitted model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_documents |
Documents to fit the model on. |
required | |
embeddings |
Optional[ndarray]
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray of shape (n_dimensions, n_topics)
|
Document-topic matrix. |
Source code in turftopic/models/bow/lda.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |