Media¶
Show each topic's media beside its name: its pictures, and a montage to play.
A topic's media is summarised once per modality when it is represented: its images tiled into a collage, the middle frame of each video tiled into a frame sheet, and the opening seconds of each clip joined into a montage. This lays those summaries out one row per topic, so a topic of photographs, recordings and clips is seen and heard at once.
Plotly has no mark for sound, so the page is plain HTML with every picture and montage embedded in it. Showing it needs IPython, which every notebook has.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
topic_model |
BERTopic
|
A fitted BERTopic instance. |
required |
topics |
list[int] | None
|
A selection of topics to show. |
None
|
top_n_topics |
int | None
|
Only show the top n most frequent topics. |
None
|
height |
int
|
The height of each picture on the page, in pixels. |
200
|
Returns:
| Type | Description |
|---|---|
HTML
|
An |
HTML
|
itself, which any browser opens once it is written to a file. |
Examples: To see the media of a model fitted on images and their captions:
from bertopic import BERTopic
from bertopic.representation import MultiModalRepresentation
topic_model = BERTopic(representation_model={"Media": MultiModalRepresentation()})
topic_model.fit(captions, images=images)
topic_model.visualize_media()
Or to save the ten largest topics as a page:
page = topic_model.visualize_media(top_n_topics=10)
with open("media.html", "w", encoding="utf-8") as file:
file.write(page.data)
Source code in bertopic\plotting\_media.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |