Can I caculate silhouette_score for BERTopic?
Or any other performance metrics? I think it is possible but I don' know how to get the matrix of the model.
I really like your package and It is really awesome!
You can calculate performance metrics but how you calculate them highly depends on the performance metric itself. There are many variations, from clustering metrics to topic coherence metrics.
Which performance metric do you want to calculate and why that performance metric?
You can calculate performance metrics but how you calculate them highly depends on the performance metric itself. There are many variations, from clustering metrics to topic coherence metrics.
Which performance metric do you want to calculate and why that performance metric?
Thank you for your reply! How about silhouette_score? I want to compare this performance result with different model. Then, I can declear your model is better than other. I read lots of article online and they use a matrix as a input. Are there any ways to get the matrix of BERTopic model ? My question might be stupid but I am still learning. Thank you again!
Of course the more calculation methods the better, or you can give me an example or a link, maybe next I will know how to calculate the rest
My question might be stupid but I am still learning.
Definitely not the case! Any and all questions are welcome here 😄
I want to compare this performance result with different model. Then, I can declear your model is better than other.
One thing that is important here is what you consider to be the definition of "better". Topic modeling can be quite subjective and selecting a proper performance metric can be quite difficult. Typically, NPMI is used to calculate the coherence of topics which is often used as a proxy for a topic model's performance. However, if you want to use the silhouette score to score the cluster generation, then it might be worthwhile to first look at the instructions here before applying it to BERTopic.
There, you can see it takes two variables of importance X and labels. In the case of BERTopic, X may either refer to the generated document embeddings or the reduced document embeddings. I would advise using the latter as the clustering procedure is based upon the reduced document embeddings.
Then, the labels refer to the clusters that are generated but since BERTopic generates outliers it is important to only select actual clusters and remove all outliers.
The procedure would be something like this:
from sklearn.metrics import silhouette_score
from sklearn.datasets import fetch_20newsgroups
from sentence_transformers import SentenceTransformer
from bertopic import BERTopic
from umap import UMAP
import numpy as np
# Prepare embeddings
docs = fetch_20newsgroups(subset='all', remove=('headers', 'footers', 'quotes'))['data']
sentence_model = SentenceTransformer("all-MiniLM-L6-v2")
embeddings = sentence_model.encode(docs, show_progress_bar=False)
# Train our topic model using our pre-trained sentence-transformers embeddings
umap_model = UMAP(n_neighbors=15, n_components=5, min_dist=0.0, metric='cosine', random_state=42)
topic_model = BERTopic(umap_model=umap_model, embedding_model=sentence_model)
topics, probs = topic_model.fit_transform(docs, embeddings)
# Generate `X` and `labels` only for non-outlier topics (as they are technically not clusters)
umap_embeddings = topic_model.umap_model.transform(embeddings)
indices = [index for index, topic in enumerate(topics) if topic != -1]
X = umap_embeddings[np.array(indices)]
labels = [topic for index, topic in enumerate(topics) if topic != -1]
# Calculate silhouette score
silhouette_score(X, labels)
What I want to stress here is that it is important to have a thorough understanding of the performance metrics as there are quite some small details and assumptions that affect the interpretation of the results.
Of course the more calculation methods the better
If you are searching for other performance metrics, then I would advise using OCTIS to calculate the performance (topic coherence, diversity, etc.). However, do note that having more calculation methods is not necessarily better. Make sure to select calculation methods that fit your use case.
Due to inactivity, I'll be closing this issue. However, if you want to discuss this further, let me know and I'll make sure to reopen the issue!