MaartenGr / MaartenGr/BERTopic

Visualisation after merge_models

Open
#1,700 14 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
7.8k
Forks
920
Avg merge
22h 24m
Merged PRs (30d)
5

Description

Hi @MaartenGr ,

I am trying to visualise the topic after merging the models but getting an error. Could you please guide me to fix it?

Fitted model using two different datasets:

keybert = KeyBERTInspired()

mmr = MaximalMarginalRelevance(diversity=0.3)

representation_models = [keybert,mmr]

topic_model = BERTopic(language="english",
top_n_words=100,
verbose=True,
representation_model=representation_models,
vectorizer_model=CountVectorizer(ngram_range=(1, 10) , stop_words="english")
)
print("Fitting model")
topic_model.fit(docs)

This resulted in two models model1 & model2

loading models to merge
topic_model1 = BERTopic.load("model1")
topic_model2 = BERTopic.load("model2")

merging model
merged_model = BERTopic.merge_models([topic_model1, topic_model2], min_similarity=0.99)

Visualisation (topic over time):

timestamps = df['Date'].to_list()
topics_over_time = merged_model.topics_over_time(docs, timestamps, nr_bins=10, global_tuning = True, evolution_tuning = True)
topic_over_time=merged_model.visualize_topics_over_time(topics_over_time, topics=[1,2,5,7,14 ], title='', width=800, height=400, custom_labels=True)
topic_over_time.write_html('topics_over_time_test.html')

ERROR:
topics_over_time = merged_model.topics_over_time(docs, timestamps, nr_bins=10, global_tuning = True, evolution_tuning = True)
File ".conda\envs\bertopic2\lib\site-packages\bertopic_bertopic.py", line 768, in topics_over_time
global_c_tf_idf = normalize(self.c_tf_idf_, axis=1, norm='l1', copy=False)
File ".conda\envs\bertopic2\lib\site-packages\sklearn\utils_param_validation.py", line 204, in wrapper
validate_parameter_constraints(
File ".conda\envs\bertopic2\lib\site-packages\sklearn\utils_param_validation.py", line 96, in validate_parameter_constraints
raise InvalidParameterError(
sklearn.utils._param_validation.InvalidParameterError: The 'X' parameter of normalize must be an array-like or a sparse matrix. Got None instead.

Thanks

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with BERTopic's merge_models and topics_over_time entry points, then inspect bertopic.py around line 768 where c_tf_idf is passed to sklearn's normalize. Reproduce the two-model merge and topic-over-time call from the issue, and determine how the merged model should provide the required data without raising this error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, scikit-learn
Domain
data-visualization, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.