Bird
0
0

What will be the output of lda.transform(dtm) in the following code?

medium📝 Predict Output Q5 of 15
NLP - Topic Modeling
What will be the output of lda.transform(dtm) in the following code?
from sklearn.decomposition import LatentDirichletAllocation
from sklearn.feature_extraction.text import CountVectorizer

docs = ["cat dog", "dog mouse", "cat mouse"]
vectorizer = CountVectorizer()
dtm = vectorizer.fit_transform(docs)
lda = LatentDirichletAllocation(n_components=2, random_state=42)
lda.fit(dtm)
result = lda.transform(dtm)
print(result.shape)
A(3, 2)
B(2, 3)
C(3, 3)
D(2, 2)
Step-by-Step Solution
Solution:
  1. Step 1: Identify number of documents and topics

    There are 3 documents and 2 topics (n_components=2).
  2. Step 2: Understand transform output shape

    lda.transform() returns topic distribution per document, so shape is (3, 2).
  3. Final Answer:

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

    transform output shape = (documents, topics) = (3,2) [OK]
Quick Trick: lda.transform returns topic distribution per document [OK]
Common Mistakes:
MISTAKES
  • Swapping documents and topics in shape
  • Confusing transform output with components_ shape
  • Assuming transform returns vocabulary-sized output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NLP Quizzes