WIP: Added fighting-words term importance to clustering models#75
Open
x-tabdeveloping wants to merge 3 commits intomainfrom
Open
WIP: Added fighting-words term importance to clustering models#75x-tabdeveloping wants to merge 3 commits intomainfrom
x-tabdeveloping wants to merge 3 commits intomainfrom
Conversation
Owner
Author
|
Thanks for sharing the post @KennethEnevoldsen |
Owner
Author
|
Also, the component values are way more interpretable, since they're basically z-scores. |
Collaborator
|
Oh this looks great! Glad to see that you are already tackling it What are your thoughts on adapting it to an embedding use case? |
Owner
Author
import numpy as np
import pandas as pd
import plotly.express as px
# from scipy.stats import multivariate_normal
from sentence_transformers import SentenceTransformer
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer
from tqdm import tqdm
from turftopic.feature_importance import fighting_words
from turftopic.supervised.semantic_lexical import SemanticLexicalAnalysis
ds = fetch_20newsgroups(
subset="all",
remove=("headers", "footers", "quotes"),
# categories=["alt.atheism", "sci.space"],
)
embeddings = np.load("_emb/20news_all-MiniLM.npy")
corpus = ds.data
labels = np.array(ds.target)
trf = SentenceTransformer("all-MiniLM-L6-v2")
# embeddings = trf.encode(corpus, show_progress_bar=True)
model = SemanticLexicalAnalysis(encoder=trf).fit(
corpus, y=labels, embeddings=embeddings
)
model.plot_semantic_lexical_square(19)
model.plot_residuals(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


I'm working on adding term importance from this paper.
It's honestly way smarter than tf-idf based approaches, and doesn't suffer from the smoothing issues of the
bayesmethod I developed earlier.It also doesn't have the theoretical weaknesses of Top2Vec, and it produces similar or better quality topics.
I'm considering making it the default in the library.