MaartenGr / MaartenGr/BERTopic

outlier reduction

Open
#2,148 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

### Have you searched existing issues? 🔎

- [X] I have searched and found no existing issues

### Desribe the bug

![newplot (3)](https://github.com/user-attachments/assets/2c46995c-ccce-4dd5-8816-df07eb18f4a9)
Whenever I perform outlier reduction (regardless of the outlier reduction strategy), meaningless feature words such as 'the', 'end', 'of' frequently appear among the top 10 feature words for each topic. Why is this happening? When I don’t perform outlier reduction, the representative feature words are displayed correctly."

### Reproduction

```python
from bertopic import BERTopic

```
embedding_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
embeddings = embedding_model.encode(data['document'], show_progress_bar=False)

# Step 2 - Reduce dimensionality
umap_model = UMAP(n_neighbors=15, n_components=5, min_dist=0.0, metric='cosine',random_state=42)#
# Step 3 - Cluster reduced embeddings
#cluster_model = KMeans(n_clusters=15)
hdbscan_model = HDBSCAN(min_cluster_size=10, metric='euclidean', cluster_selection_method='eom', prediction_data=True, min_samples=5)
from sklearn.feature_extraction.text import CountVectorizer
# Step 4 - Tokenize topics
vectorizer_model = CountVectorizer(ngram_range=(1, 1),stop_words="english",min_df=2)#,
# Step 5 - Create topic representation
ctfidf_model = ClassTfidfTransformer(reduce_frequent_words=True)
representation_model = MaximalMarginalRelevance(diversity=0.2,top_n_words=15)#

#representation_model = KeyBERTInspired()
topic_model = BERTopic(
top_n_words=16,
embedding_model=embedding_model,
representation_model=representation_model,
umap_model=umap_model,
hdbscan_model=hdbscan_model,
vectorizer_model=vectorizer_model,
ctfidf_model=ctfidf_model,
calculate_probabilities=True,
nr_topics="auto"
)
topics, probs= topic_model.fit_transform(data['document'],embeddings)

print(topic_model.get_topic_info())

new_topics = topic_model.reduce_outliers(data['document'],topics, strategy="c-tf-idf", threshold=0.1)
new_topics = topic_model.reduce_outliers(data['document'], new_topics, strategy="distributions")

topic_model.update_topics(data['document'], topics=new_topics)
![newplot (3)](https://github.com/user-attachments/assets/2c46995c-ccce-4dd5-8816-df07eb18f4a9)
Whenever I perform outlier reduction (regardless of the outlier reduction strategy), meaningless feature words such as 'the', 'end', 'of' frequently appear among the top 10 feature words for each topic. Why is this happening? When I don’t perform outlier reduction, the representative feature words are displayed correctly."

### BERTopic Version

1.6.1

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 the supplied Python reproduction, focusing on reduce_outliers with the c-tf-idf and distributions strategies and the subsequent update_topics call. Compare topic representations before and after these steps, then determine whether the stop-word and meaningless-word regression is reproducible and what change or documented limitation explains it.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, scikit-learn
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.