1
$\begingroup$

I new to topic modeling and I'm trying to use BERTopic inside of PyCharm. I'm struggling to get any type of visualization to work inside of PyCharm with BERTopic.

Here is my current code:

import pickle
from umap import UMAP
from hdbscan import HDBSCAN
from bertopic import BERTopic
from sent2vec.vectorizer import Vectorizer
from sentence_transformers import SentenceTransformer
from sklearn.feature_extraction.text import CountVectorizer


# 6000 articles about cryptocurrencies
with open('all_articles', 'rb') as file_in:
    random_articles = pickle.load(file_in)

embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
umap_model = UMAP(n_neighbors=3, 
                  n_components=3, 
                  min_dist=0.05)

hdbscan_model = HDBSCAN(min_cluster_size=80, 
                        min_samples=40,
                        gen_min_span_tree=True,
                        prediction_data=True)

vectorizer_model = CountVectorizer(ngram_range=(1, 2), 
                                   stop_words='english')

model = BERTopic(
    umap_model=umap_model,
    hdbscan_model=hdbscan_model,
    embedding_model=embedding_model,
    vectorizer_model=vectorizer_model,
    top_n_words=5,
    language='english',
    calculate_probabilities=True,
    verbose=True
)
topics, probs = model.fit_transform(random_articles)

# this produces nothing
model.visualize_topics(top_n_topics=5)

I found this issue on visualization not working within PyCharm with BERTopic. This issue wasn't useful in solving my issue.

How do I get something to visualize inside of PyCharm?

$\endgroup$

1 Answer 1

0
$\begingroup$

Have you tried replacing your last line with the following?

fig = model.visualize_topics(top_n_topics=5)
fig.show()
$\endgroup$
1
  • $\begingroup$ Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. $\endgroup$
    – Community Bot
    Mar 25, 2023 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.