BERTopic(三)update topics

简介: BERTopic更新主题

BERTopic生成的主题数量较多,并且有一些重复。

hdbscan的min_cluster_size是官方推荐的用于控制主题数量的参数。

Manual Topic Reduction
合并主题

topics_to_merge = [[1, 2],
                   [3, 4]]
topic_model.merge_topics(docs, topics_to_merge)

Automatic Topic Reduction

from bertopic import BERTopic
topic_model = BERTopic(nr_topics="auto")

Topic Reduction after Training

from bertopic import BERTopic
from sklearn.datasets import fetch_20newsgroups

# Create topics -> Typically over 50 topics
docs = fetch_20newsgroups(subset='all',  remove=('headers', 'footers', 'quotes'))['data']
topic_model = BERTopic()
topics, probs = topic_model.fit_transform(docs)

# Further reduce topics
topic_model.reduce_topics(docs, nr_topics=30)

# Access updated topics
topics = topic_model.topics_

Update Topic Representation after Training

# 换一个n_gram_range
topic_model.update_topics(docs, n_gram_range=(1, 3))

# 或者自定义CountVectorizer
from sklearn.feature_extraction.text import CountVectorizer
vectorizer_model = CountVectorizer(stop_words="english", ngram_range=(1, 5))
topic_model.update_topics(docs, vectorizer_model=vectorizer_model)

Outlier reduction
把离群点放入非离群的主题里面

new_topics = topic_model.reduce_outliers(docs, topics)

参数threshold选择离群点和主题的最小距离

默认方法是计算离群文档的c-TF-IDF表示,然后放入最匹配的非离群文档中。其它可以考虑的策略还有:

用topic-document probabilities分配主题

This strategy uses the soft-clustering as performed by HDBSCAN to find the best matching topic for each outlier document.

from bertopic import BERTopic

# Train your BERTopic model and calculate the document-topic probabilities
topic_model = BERTopic(calculate_probabilities=True)
topics, probs = topic_model.fit_transform(docs)

# Reduce outliers using the `probabilities` strategy
new_topics = topic_model.reduce_outliers(docs, topics, probabilities=probs, strategy="probabilities")

用topic-document distributions分配主题

from bertopic import BERTopic

# Train your BERTopic model
topic_model = BERTopic()
topics, probs = topic_model.fit_transform(docs)

# Reduce outliers using the `distributions` strategy
new_topics = topic_model.reduce_outliers(docs, topics, strategy="distributions")

用c-TF-IDF representations分配主题
Calculate the c-TF-IDF representation for each outlier document and find the best matching c-TF-IDF topic representation using cosine similarity.

from bertopic import BERTopic

# Train your BERTopic model
topic_model = BERTopic()
topics, probs = topic_model.fit_transform(docs)

# Reduce outliers using the `c-tf-idf` strategy
new_topics = topic_model.reduce_outliers(docs, topics, strategy="c-tf-idf")

用document and topic embeddings分配主题
Using the embeddings of each outlier documents, find the best matching topic embedding using cosine similarity.

from bertopic import BERTopic

# Train your BERTopic model
topic_model = BERTopic()
topics, probs = topic_model.fit_transform(docs)

# Reduce outliers using the `embeddings` strategy
new_topics = topic_model.reduce_outliers(docs, topics, strategy="embeddings")

多种策略结合

# Use the "c-TF-IDF" strategy with a threshold
new_topics = topic_model.reduce_outliers(docs, new_topics , strategy="c-tf-idf", threshold=0.1)

# Reduce all outliers that are left with the "distributions" strategy
new_topics = topic_model.reduce_outliers(docs, topics, strategy="distributions")

Update Topics

topic_model.update_topics(docs, topics=new_topics)
相关文章
|
数据可视化 数据挖掘
基于Bert的文本聚类工具:BERTopic
基于Bert的文本聚类工具:BERTopic
2137 0
基于Bert的文本聚类工具:BERTopic
|
数据可视化 数据挖掘 开发工具
【办公自动化】用Python批量从上市公司年报中获取主要业务信息
【办公自动化】用Python批量从上市公司年报中获取主要业务信息
1067 0
|
传感器 数据采集 监控
上千个完整设计的单片机、8086、STM32制作教程和资料-转发分享
在网上收集了接近上千个完整设计的单片机、8086、STM32制作教程和资料-转发分享(涵盖了大部分的毕设课设题目),学习单片机的最好教程,也可以作为帮助大家在做电子课设毕设时有利的帮助,可以从以下百度网盘下载(按照编号下载)。
2641 0
上千个完整设计的单片机、8086、STM32制作教程和资料-转发分享
|
自然语言处理 算法 数据可视化
NLP-基于bertopic工具的新闻文本分析与挖掘
这篇文章介绍了如何使用Bertopic工具进行新闻文本分析与挖掘,包括安装Bertopic库、加载和预处理数据集、建立并训练主题模型、评估模型性能、分类新闻标题、调优聚类结果的详细步骤和方法。
NLP-基于bertopic工具的新闻文本分析与挖掘
|
8月前
|
弹性计算 JSON 自然语言处理
语音交互产品通过WebSocket协议对外提供实时语音流语音转写功能
阿里云智能语音交互产品通过WebSocket协议提供实时语音转写功能,支持长语音。音频流以Binary Frame上传,指令和事件为Text Frame。支持单声道、16 bit采样位数的PCM、WAV等格式,采样率8000Hz/16000Hz。可设置返回中间结果、添加标点、中文数字转阿拉伯数字,并支持多语言识别。服务端通过临时Token鉴权,提供外网和上海ECS内网访问URL。交互流程包括StartTranscription、StopTranscription指令及多种事件反馈。
|
10月前
|
传感器 安全 物联网
新技术潮流之巅:区块链、物联网与虚拟现实的融合与创新
在数字时代的浪潮中,新兴技术不断涌现,重塑着我们的工作和生活方式。本文将深入探讨三项引领未来的技术:区块链、物联网(IoT)和虚拟现实(VR),并揭示它们如何独立发展同时又相互交织,共同构建一个更加智能、互联的世界。我们将从技术的基本原理出发,探索它们在实际应用场景中的革新作用,以及这些技术融合后所带来的全新可能性。通过具体案例分析,本文旨在为读者提供一个关于未来技术趋势及其应用的全面视角。
173 7
|
机器学习/深度学习 传感器 编解码
史上最全 | BEV感知算法综述(基于图像/Lidar/多模态数据的3D检测与分割任务)
以视觉为中心的俯视图(BEV)感知最近受到了广泛的关注,因其可以自然地呈现自然场景且对融合更友好。随着深度学习的快速发展,许多新颖的方法尝试解决以视觉为中心的BEV感知,但是目前还缺乏对该领域的综述类文章。本文对以视觉为中心的BEV感知及其扩展的方法进行了全面的综述调研,并提供了深入的分析和结果比较,进一步思考未来可能的研究方向。如下图所示,目前的工作可以根据视角变换分为两大类,即基于几何变换和基于网络变换。前者利用相机的物理原理,以可解释性的方式转换视图。后者则使用神经网络将透视图(PV)投影到BEV上。
史上最全 | BEV感知算法综述(基于图像/Lidar/多模态数据的3D检测与分割任务)
|
10月前
|
自然语言处理 算法 搜索推荐
NLTK模块使用详解
NLTK(Natural Language Toolkit)是基于Python的自然语言处理工具集,提供了丰富的功能和语料库。本文详细介绍了NLTK的安装、基本功能、语料库加载、词频统计、停用词去除、分词分句、词干提取、词形还原、词性标注以及WordNet的使用方法。通过示例代码,帮助读者快速掌握NLTK的核心功能。
1933 1
|
自然语言处理 数据可视化 数据挖掘
BERTopic(一)基本用法
bertopic基本用法
476 0

热门文章

最新文章