Articles → NLP → Non-Negative Matrix Factorization In NLP
Non-Negative Matrix Factorization In NLP
Purpose
Example
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import NMF
docs = [
"cricket match score",
"football match goal",
"iphone samsung mobile"
]
tfidf = TfidfVectorizer(stop_words='english')
V = tfidf.fit_transform(docs)
nmf = NMF(n_components=2, random_state=42)
W = nmf.fit_transform(V)
H = nmf.components_
terms = tfidf.get_feature_names_out()
for i, topic in enumerate(H):
print(f"Topic {i}:",
[terms[j] for j in topic.argsort()[-3:]])
Output
| Posted By - | Karan Gupta |
| |
| Posted On - | Saturday, January 24, 2026 |