MCPcopy
hub / github.com/MaartenGr/BERTopic / visualize_heatmap

Function visualize_heatmap

bertopic/plotting/_heatmap.py:11–136  ·  view source on GitHub ↗

Visualize a heatmap of the topic's similarity matrix. Based on the cosine similarity matrix between topic embeddings (either c-TF-IDF or the embeddings from the embedding model), a heatmap is created showing the similarity between topics. Arguments: topic_model: A fitted BERTop

(
    topic_model,
    topics: List[int] | None = None,
    top_n_topics: int | None = None,
    n_clusters: int | None = None,
    use_ctfidf: bool = False,
    custom_labels: Union[bool, str] = False,
    title: str = "<b>Similarity Matrix</b>",
    width: int = 800,
    height: int = 800,
)

Source from the content-addressed store, hash-verified

9
10
11def visualize_heatmap(
12 topic_model,
13 topics: List[int] | None = None,
14 top_n_topics: int | None = None,
15 n_clusters: int | None = None,
16 use_ctfidf: bool = False,
17 custom_labels: Union[bool, str] = False,
18 title: str = "<b>Similarity Matrix</b>",
19 width: int = 800,
20 height: int = 800,
21) -> go.Figure:
22 """Visualize a heatmap of the topic&#x27;s similarity matrix.
23
24 Based on the cosine similarity matrix between topic embeddings (either c-TF-IDF or the embeddings from the embedding
25 model), a heatmap is created showing the similarity between topics.
26
27 Arguments:
28 topic_model: A fitted BERTopic instance.
29 topics: A selection of topics to visualize.
30 top_n_topics: Only select the top n most frequent topics.
31 n_clusters: Create n clusters and order the similarity
32 matrix by those clusters.
33 use_ctfidf: Whether to calculate distances between topics based on c-TF-IDF embeddings. If False, the embeddings
34 from the embedding model are used.
35 custom_labels: If bool, whether to use custom topic labels that were defined using
36 `topic_model.set_topic_labels`.
37 If `str`, it uses labels from other aspects, e.g., "Aspect1".
38 title: Title of the plot.
39 width: The width of the figure.
40 height: The height of the figure.
41
42 Returns:
43 fig: A plotly figure
44
45 Examples:
46 To visualize the similarity matrix of
47 topics simply run:
48
49 ```python
50 topic_model.visualize_heatmap()
51 ```
52
53 Or if you want to save the resulting figure:
54
55 ```python
56 fig = topic_model.visualize_heatmap()
57 fig.write_html("path/to/file.html")
58 ```
59 <iframe src="../../getting_started/visualization/heatmap.html"
60 style="width:1000px; height: 720px; border: 0px;""></iframe>
61 """
62 embeddings = select_topic_representation(topic_model.c_tf_idf_, topic_model.topic_embeddings_, use_ctfidf)[0][
63 topic_model._outliers :
64 ]
65
66 # Select topics based on top_n and topics args
67 freq_df = topic_model.get_topic_freq()
68 freq_df = freq_df.loc[freq_df.Topic != -1, :]

Callers

nothing calls this directly

Calls 3

get_topic_freqMethod · 0.80
get_topicMethod · 0.80

Tested by

no test coverage detected