Skip to content

feat: add FeatureImportance representation model with contrastive and embedding-aware methods#2511

Open
pidefrem wants to merge 1 commit into
MaartenGr:masterfrom
pidefrem:pr12-feature-importance
Open

feat: add FeatureImportance representation model with contrastive and embedding-aware methods#2511
pidefrem wants to merge 1 commit into
MaartenGr:masterfrom
pidefrem:pr12-feature-importance

Conversation

@pidefrem

@pidefrem pidefrem commented Jul 8, 2026

Copy link
Copy Markdown

What does this PR do?

feat: add FeatureImportance representation model with contrastive and embedding-aware methods

BERTopic's topic term importance is based solely on c-TF-IDF. This PR adds a FeatureImportance representation class with two pluggable importance methods:

Method Algorithm Answers
"fighting_words" Bayesian log-odds with Dirichlet priors (Monroe et al. 2008) "What terms distinguish this topic from the rest?"
"centroid_distance" Cosine similarity between topic centroid and term embeddings "What terms are semantically central to this topic?"

Changes:

New FeatureImportance class under bertopic/representation/ that follows the existing BaseRepresentation interface. Slots into BERTopic's topic_aspects_ infrastructure:

from bertopic.representation import FeatureImportance

topic_model = BERTopic(
    representation_model={
        "Main": main_model,
        "FightingWords": FeatureImportance(method="fighting_words"),
        "CentroidDistance": FeatureImportance(method="centroid_distance"),
    }
)
topic_model.get_topic(0, aspect="FightingWords")

No changes to core BERTopic code — this is a new representation model that plugs into the existing pipeline.

Fixes #2506

Coordination note: this is purely additive — a new representation/ class behind the existing BaseRepresentation interface, no core changes. I shipped two methods (fighting_words, centroid_distance); if you'd prefer to start with just one to keep the surface minimal, or want a different class/method naming, happy to trim before review.

Before submitting

  • This PR fixes a typo or improves the docs (if yes, ignore all other checks!).
  • Did you read the contributor guideline?
  • Was this discussed/approved via a Github issue? Please add a link to it if that's the case.
  • Did you make sure to update the documentation with your changes (if applicable)?
  • Did you write any new necessary tests?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add contrastive and embedding-aware feature importance as representation aspects

2 participants