MaartenGr / MaartenGr/BERTopic

`IndexError: list index out of range` when using zeroshot_topic_list in 0.16.1

Open
#1,946 19 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, I recently re-ran a notebook for `zeroshot_topic_list` and got the `IndexError: list index our of range`
I fixed this by downgrading to 0.16.0

Full stacktrace:
```
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[2], line 18
9 vectorizer_model = CountVectorizer(stop_words="english")
11 topic_model = BERTopic(
12 min_topic_size=20,
13 zeroshot_topic_list=zeroshot_topic_list,
14 zeroshot_min_similarity=.25,
15 vectorizer_model=vectorizer_model
16 )
---> 18 topics, probs = topic_model.fit_transform(docs)
19 topic_model.get_topic_info()

File /opt/conda/lib/python3.10/site-packages/bertopic/_bertopic.py:448, in BERTopic.fit_transform(self, documents, embeddings, images, y)
446 # Combine Zero-shot with outliers
447 if self._is_zeroshot() and len(documents) != len(doc_ids):
--> 448 predictions = self._combine_zeroshot_topics(documents, assigned_documents, assigned_embeddings)
450 return predictions, self.probabilities_

File /opt/conda/lib/python3.10/site-packages/bertopic/_bertopic.py:3682, in BERTopic._combine_zeroshot_topics(self, documents, assigned_documents, embeddings)
3680 cluster_indices = list(documents.Old_ID.values)
3681 cluster_names = list(merged_model.topic_labels_.values())[len(set(y)):]
-> 3682 cluster_topics = [cluster_names[topic + self._outliers] for topic in documents.Topic.values]
3684 df = pd.DataFrame({
3685 "Indices": zeroshot_indices + cluster_indices,
3686 "Label": zeroshot_topics + cluster_topics}
3687 ).sort_values("Indices")
3688 reverse_topic_labels = dict((v, k) for k, v in merged_model.topic_labels_.items())

File /opt/conda/lib/python3.10/site-packages/bertopic/_bertopic.py:3682, in (.0)
3680 cluster_indices = list(documents.Old_ID.values)
3681 cluster_names = list(merged_model.topic_labels_.values())[len(set(y)):]
-> 3682 cluster_topics = [cluster_names[topic + self._outliers] for topic in documents.Topic.values]
3684 df = pd.DataFrame({
3685 "Indices": zeroshot_indices + cluster_indices,
3686 "Label": zeroshot_topics + cluster_topics}
3687 ).sort_values("Indices")
3688 reverse_topic_labels = dict((v, k) for k, v in merged_model.topic_labels_.items())
```

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 in _bertopic.py at fit_transform and _combine_zeroshot_topics, using the reported zeroshot_topic_list notebook configuration to reproduce the failure. Confirm that fitting under 0.16.1 completes without the IndexError and that the returned topics and probabilities remain available.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.