Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1749 #2267

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions bertopic/_bertopic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4450,7 +4450,6 @@ def _auto_reduce_topics(self, documents: pd.DataFrame, use_ctfidf: bool = False)
"""
topics = documents.Topic.tolist().copy()
unique_topics = sorted(list(documents.Topic.unique()))[self._outliers :]
max_topic = unique_topics[-1]

# Find similar topics
embeddings = select_topic_representation(
Expand All @@ -4464,12 +4463,23 @@ def _auto_reduce_topics(self, documents: pd.DataFrame, use_ctfidf: bool = False)
prediction_data=True,
).fit_predict(norm_data[self._outliers :])

# Map similar topics
mapped_topics = {
unique_topics[index]: prediction + max_topic
for index, prediction in enumerate(predictions)
if prediction != -1
}
# Map clusters to their lowest topic_id
cluster_to_lowest = {}
for cluster, topic_id in zip(predictions, unique_topics):
if cluster != -1: # Ignore unclustered items
if cluster not in cluster_to_lowest:
cluster_to_lowest[cluster] = topic_id
else:
cluster_to_lowest[cluster] = min(cluster_to_lowest[cluster], topic_id)

# Map each topic_id to the lowest topic_id in its cluster
mapped_topics = {}
for cluster, topic_id in zip(predictions, unique_topics):
if cluster == -1:
mapped_topics[topic_id] = topic_id # No clustering, stays the same
else:
mapped_topics[topic_id] = cluster_to_lowest[cluster]

documents.Topic = documents.Topic.map(mapped_topics).fillna(documents.Topic).astype(int)
mapped_topics = {from_topic: to_topic for from_topic, to_topic in zip(topics, documents.Topic.tolist())}

Expand Down
Loading