0
0
NLPml~10 mins

Why topic modeling discovers themes in NLP - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the library used for topic modeling.

NLP
from sklearn.decomposition import [1]
Drag options to blanks, or click blank then click option'
APCA
BLinearRegression
CKMeans
DNMF
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing PCA which is for dimensionality reduction but not typically for topic modeling.
Choosing KMeans which is a clustering algorithm, not matrix factorization.
2fill in blank
medium

Complete the code to fit the topic model on the document-term matrix.

NLP
model = NMF(n_components=5, random_state=42)
W = model.[1](X)
Drag options to blanks, or click blank then click option'
Atransform
Bfit_transform
Cfit_predict
Dpredict
Attempts:
3 left
💡 Hint
Common Mistakes
Using transform without fitting first causes an error.
fit_predict is not a method of NMF.
3fill in blank
hard

Fix the error in printing the top words for each topic.

NLP
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]]))
Drag options to blanks, or click blank then click option'
A::-1
Bascending
Creverse
Dsort
Attempts:
3 left
💡 Hint
Common Mistakes
Using sort() which returns None and causes an error.
Using reverse() which works only on lists, not numpy arrays.
4fill in blank
hard

Fill both blanks to create a dictionary of topics with their top words.

NLP
top_words = {f"Topic_{i}": [feature_names[j] for j in model.components_[i].[1]()[2][:5]] for i in range(model.n_components)}
Drag options to blanks, or click blank then click option'
Aargsort()[::-1]
Bsort
Creverse
Dargsort
Attempts:
3 left
💡 Hint
Common Mistakes
Using sort which returns None.
Trying to use reverse which is not valid here.
5fill in blank
hard

Fill all three blanks to compute and print the coherence score of the topic model.

NLP
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}")
Drag options to blanks, or click blank then click option'
Adictionary
Bcorpus
Cget_coherence
Dc_v
Attempts:
3 left
💡 Hint
Common Mistakes
Passing corpus instead of dictionary.
Using wrong method name to get coherence.