Complete the code to import the library used for topic modeling.
from sklearn.decomposition import [1]
The NMF class from sklearn.decomposition is commonly used for topic modeling to discover themes in text data.
Complete the code to fit the topic model on the document-term matrix.
model = NMF(n_components=5, random_state=42) W = model.[1](X)
The fit_transform method fits the model and returns the transformed data (topic weights for documents).
Fix the error in printing the top words for each topic.
feature_names = vectorizer.get_feature_names_out() for topic_idx, topic in enumerate(model.components_): print(f"Topic {topic_idx}:", end=" ") print(" ".join([feature_names[i] for i in topic.argsort()[1]()[:10]]))
Using argsort()[::-1] sorts indices in descending order to get top words per topic.
Fill both blanks to create a dictionary of topics with their top words.
top_words = {f"Topic_{i}": [feature_names[j] for j in model.components_[i].[1]()[2][:5]] for i in range(model.n_components)}We use argsort()[::-1] to get indices of top words in descending order. The second blank is empty because slicing is done after reversing.
Fill all three blanks to compute and print the coherence score of the topic model.
from gensim.models import CoherenceModel texts = [[word for word in doc.split()] for doc in documents] coherence_model = CoherenceModel(model=model, texts=texts, dictionary=[1], coherence=[2]) coherence_score = coherence_model.[3]() print(f"Coherence Score: {coherence_score:.3f}")
The dictionary is needed for coherence calculation, c_v is a common coherence type, and get_coherence() computes the score.