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


Picture showing the output of non-negative matrix factorization in nlp





Posted By  -  Karan Gupta
 
Posted On  -  Saturday, January 24, 2026

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250