Bird
0
0

Given the following code snippet, what will be the shape of the variable topic_distribution?

medium📝 Predict Output Q13 of 15
NLP - Topic Modeling
Given the following code snippet, what will be the shape of the variable topic_distribution?
from sklearn.decomposition import LatentDirichletAllocation
from sklearn.feature_extraction.text import CountVectorizer

docs = ["apple banana apple", "banana orange banana", "apple orange orange"]
vectorizer = CountVectorizer()
dtm = vectorizer.fit_transform(docs)
lda = LatentDirichletAllocation(n_components=2, random_state=0)
lda.fit(dtm)
topic_distribution = lda.transform(dtm)
A(2, 3)
B(3, 2)
C(3, 3)
D(2, 2)
Step-by-Step Solution
Solution:
  1. Step 1: Understand input and model parameters

    There are 3 documents and the LDA model is set to find 2 topics (n_components=2).
  2. Step 2: Determine output shape of lda.transform

    The transform method returns a matrix with rows = number of documents (3) and columns = number of topics (2).
  3. Final Answer:

    (3, 2) -> Option B
  4. Quick Check:

    Output shape = (documents, topics) = (3, 2) [OK]
Quick Trick: Output shape = (number of docs, number of topics) [OK]
Common Mistakes:
MISTAKES
  • Confusing number of topics with number of documents
  • Swapping rows and columns in output shape
  • Assuming transform returns topic-word matrix

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes