你好!
我正在尝试使用您的库对Yahoo answers主题数据集进行简单的主题建模。目前,我正在尝试使用无监督的方法。在可视化最终的主题时,n_words
参数似乎只更新了绘图内的条形图,而没有更新y轴标签值。以下是代码和最终图像:
from umap import UMAP
from hdbscan import HDBSCAN
from sentence_transformers import SentenceTransformer
from sklearn.cluster import KMeans
from bertopic.representation import MaximalMarginalRelevance
from bertopic import BERTopic
from bertopic.representation import KeyBERTInspired
from datasets import load_dataset
import matplotlib.pyplot as plt
print("Downloading dataset . . .")
data = load_dataset("yahoo_answers_topics", split="test")
text = data.to_pandas()[["best_answer", "topic"]]
num_classes = len(set(text.topic.values))
print("Downloading model . . .")
embedding_model = SentenceTransformer("sentence-transformers/all-mpnet-base-v2")
umap_model = UMAP(
n_neighbors=15, n_components=5, min_dist=0.0, metric="cosine"
)
kmeans_model = KMeans(n_clusters=num_classes)
representation_model = [
KeyBERTInspired(),
MaximalMarginalRelevance(diversity=0.5)
]
topic_model = BERTopic(
embedding_model=embedding_model,
umap_model=umap_model,
hdbscan_model=kmeans_model,
representation_model=representation_model,
verbose=True,
)
print("Finding topics . . .")
topics, probs = topic_model.fit_transform(text.best_answer.values)
fig = topic_model.visualize_barchart(
top_n_topics=num_classes,
n_words=10,
)
fig.write_image("topics_barchart.jpg")
3条答案
按热度按时间nr9pn0ug1#
你是否尝试过增加
height
参数的默认值?由于图表长度不够,因此无法显示文字,所以增加height
的值应该可以解决问题。除此之外,你也可以直接放大图表,这样就可以显示文字了。p4tfgftt2#
这确实有效。我原本期待不同的默认行为,因为让字体变小或者自动改变高度会更好。
ffdz8vbo3#
很高兴听到它起作用了!将此作为默认行为是可能的,但可能需要一些复杂的缩放和公开Plotly的字体(大小)以计算如何进行缩放。无论如何,很高兴听到它起作用了,如果有人想继续研究这个自动缩放功能,我会保持这个开放状态。不幸的是,我现在无法接手这个项目。