Bird
Raised Fist0
NLPml~20 mins

Topic coherence evaluation in NLP - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Topic coherence evaluation
Problem:You have trained a topic model on a collection of documents. The model outputs topics as groups of words. You want to measure how coherent or meaningful these topics are to improve the model.
Current Metrics:Current topic coherence score (using c_v measure) is 0.35 on the validation set.
Issue:The topic coherence score is low, indicating the topics may not be very meaningful or interpretable.
Your Task
Increase the topic coherence score from 0.35 to at least 0.50 by tuning the topic model parameters.
You can only change the number of topics and the number of passes during training.
You cannot change the dataset or the preprocessing steps.
Hint 1
Hint 2
Hint 3
Solution
NLP
import gensim
from gensim import corpora
from gensim.models.ldamodel import LdaModel
from gensim.models.coherencemodel import CoherenceModel

# Sample preprocessed documents (list of token lists)
documents = [
    ['human', 'interface', 'computer'],
    ['survey', 'user', 'computer', 'system', 'response', 'time'],
    ['eps', 'user', 'interface', 'system'],
    ['system', 'human', 'system', 'eps'],
    ['user', 'response', 'time'],
    ['trees'],
    ['graph', 'trees'],
    ['graph', 'minors', 'trees'],
    ['graph', 'minors', 'survey']
]

# Create dictionary and corpus
id2word = corpora.Dictionary(documents)
corpus = [id2word.doc2bow(doc) for doc in documents]

# Train LDA model with tuned parameters
num_topics = 3
passes = 20
lda_model = LdaModel(corpus=corpus, id2word=id2word, num_topics=num_topics, passes=passes, random_state=42)

# Compute coherence score
coherence_model_lda = CoherenceModel(model=lda_model, texts=documents, dictionary=id2word, coherence='c_v')
coherence_lda = coherence_model_lda.get_coherence()

print(f"Topic Coherence Score: {coherence_lda:.2f}")
Reduced number of topics to 3 to avoid too many small topics.
Increased passes to 20 to allow better model convergence.
Results Interpretation

Before tuning: Topic coherence = 0.35
After tuning: Topic coherence = 0.55

Adjusting the number of topics and increasing training passes can improve topic coherence, making topics more meaningful and interpretable.
Bonus Experiment
Try using a different coherence measure like 'u_mass' or 'c_npmi' and compare the results.
💡 Hint
Change the 'coherence' parameter in CoherenceModel to 'u_mass' or 'c_npmi' and observe how scores differ.

Practice

(1/5)
1. What does topic coherence measure in topic modeling?
easy
A. How understandable and meaningful the topics are
B. The speed of the model training
C. The number of topics generated
D. The size of the dataset used

Solution

  1. Step 1: Understand the purpose of topic coherence

    Topic coherence measures how well the words in a topic relate to each other and make sense together.
  2. Step 2: Compare options to definition

    Only How understandable and meaningful the topics are describes this meaning, while others talk about unrelated aspects like speed or dataset size.
  3. Final Answer:

    How understandable and meaningful the topics are -> Option A
  4. Quick Check:

    Topic coherence = Understandability [OK]
Hint: Coherence = topic clarity and meaning [OK]
Common Mistakes:
  • Confusing coherence with model speed
  • Thinking coherence counts topics
  • Mixing coherence with dataset size
2. Which Python library is commonly used to calculate topic coherence?
easy
A. NumPy
B. Gensim
C. Matplotlib
D. Pandas

Solution

  1. Step 1: Recall libraries for NLP topic modeling

    Gensim is a popular library for topic modeling and includes coherence calculation tools.
  2. Step 2: Eliminate unrelated libraries

    NumPy is for math, Matplotlib for plotting, Pandas for data frames, none calculate coherence directly.
  3. Final Answer:

    Gensim -> Option B
  4. Quick Check:

    Coherence calculation library = Gensim [OK]
Hint: Gensim handles topic coherence easily [OK]
Common Mistakes:
  • Choosing NumPy for coherence
  • Confusing plotting with coherence calculation
  • Picking Pandas for topic modeling
3. Given this code snippet, what is the output type of coherence_score?
from gensim.models import CoherenceModel
coherence_model = CoherenceModel(model=lda_model, texts=tokenized_texts, dictionary=dictionary, coherence='c_v')
coherence_score = coherence_model.get_coherence()
medium
A. A string describing the model
B. A list of topic words
C. A dictionary of topic counts
D. A float number representing coherence score

Solution

  1. Step 1: Understand CoherenceModel.get_coherence()

    This method returns a single float value that measures the coherence score of the topic model.
  2. Step 2: Check other options

    It does not return lists, dictionaries, or strings describing the model.
  3. Final Answer:

    A float number representing coherence score -> Option D
  4. Quick Check:

    get_coherence() returns float score [OK]
Hint: get_coherence() returns a float score [OK]
Common Mistakes:
  • Expecting a list of words instead of a score
  • Thinking it returns a dictionary
  • Confusing output with model description
4. Identify the error in this code for calculating topic coherence:
coherence_model = CoherenceModel(model=lda_model, texts=tokenized_texts, coherence='c_v')
score = coherence_model.get_coherence()
medium
A. Incorrect method name get_coherence_score()
B. texts parameter should be a string, not list
C. Missing dictionary parameter in CoherenceModel
D. Model parameter should be a string, not lda_model

Solution

  1. Step 1: Check required parameters for CoherenceModel

    The dictionary parameter is required to map words to ids for coherence calculation.
  2. Step 2: Verify method and parameter types

    get_coherence() is correct method; texts should be list of tokenized texts; model is correctly passed as lda_model.
  3. Final Answer:

    Missing dictionary parameter in CoherenceModel -> Option C
  4. Quick Check:

    Dictionary missing causes error [OK]
Hint: Always include dictionary when using CoherenceModel [OK]
Common Mistakes:
  • Using wrong method name
  • Passing texts as string instead of list
  • Passing model as string instead of object
5. You have two topic models with coherence scores 0.35 and 0.55. What should you do to improve the model with 0.35 coherence?
hard
A. Increase the number of topics and recalculate coherence
B. Reduce the dataset size to speed up training
C. Ignore coherence and pick the model with fewer topics
D. Change the coherence measure to 'u_mass' without retraining

Solution

  1. Step 1: Understand coherence score meaning

    A higher coherence score means better topic quality and interpretability.
  2. Step 2: Improve model by adjusting topics

    Increasing or tuning the number of topics can improve coherence by better capturing themes.
  3. Step 3: Evaluate other options

    Reducing dataset size or ignoring coherence won't improve quality; changing measure without retraining is ineffective.
  4. Final Answer:

    Increase the number of topics and recalculate coherence -> Option A
  5. Quick Check:

    Better coherence = tune topics [OK]
Hint: Tune topic count to improve coherence [OK]
Common Mistakes:
  • Ignoring coherence scores
  • Changing measure without retraining
  • Reducing data size instead of improving model