From 0a5be5edf55513aedc4cf72180c08604cba849a6 Mon Sep 17 00:00:00 2001 From: Ajda Date: Mon, 23 Dec 2024 11:30:40 +0100 Subject: [PATCH] Update _topics.py Proposed solution to not having enough topics and the call visualize_topics() resulting in an Error. --- bertopic/plotting/_topics.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bertopic/plotting/_topics.py b/bertopic/plotting/_topics.py index 2e477d05..f2febeda 100644 --- a/bertopic/plotting/_topics.py +++ b/bertopic/plotting/_topics.py @@ -1,5 +1,6 @@ import numpy as np import pandas as pd +import warnings from umap import UMAP from typing import List, Union from sklearn.preprocessing import MinMaxScaler @@ -56,6 +57,9 @@ def visualize_topics( freq_df = freq_df.loc[freq_df.Topic != -1, :] if topics is not None: topics = list(topics) + elif len(topics) < 5: + warnings.warn("Too few topics to display.") + return elif top_n_topics is not None: topics = sorted(freq_df.Topic.to_list()[:top_n_topics]) else: