MaartenGr / MaartenGr/BERTopic

How to calculate entropy with Bertopic

Open
#2,186 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Have you searched existing issues? 🔎
  • I have searched and found no existing issues
Desribe the bug

Hi everyone, first of all I would like to thank @MaartenGr and all the contributors for this amazing project.
For my project, I need to calculate the entropy of each topic. Could you help me how to calculate entropy in Bertopic. I have used probs to calculate, but the bug showed that the probs were 1 dimension array. But my code requires two dimension array. Thank you very much!

Reproduction

import numpy as np
import pandas as pd

doc_topic_matrix = np.array(probs)

normalized_doc_topic_matrix = doc_topic_matrix / doc_topic_matrix.sum(axis=1, keepdims=True)

topic_entropy = (-normalized_doc_topic_matrix * np.log2(normalized_doc_topic_matrix + 1e-9)).sum(axis=0)

entropy_df = pd.DataFrame({'Topic': range(len(topic_entropy)), 'Entropy': topic_entropy})

topic_freq['Entropy'] = sorted_entropy_df['Entropy'].values

BERTopic Version

0.16.4

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

No project file, test, or entry point is named. Start by reproducing the one-dimensional probs result with BERTopic 0.16.4 and clarify whether entropy should be calculated per document or topic; done means an agreed calculation and documented supported output.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
machine-learning
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.