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
2161 0
基于Bert的文本聚类工具:BERTopic
|
SQL 存储 数据库
Python 的安全性和测试:什么是 SQL 注入攻击?如何防范 SQL 注入?
Python 的安全性和测试:什么是 SQL 注入攻击?如何防范 SQL 注入?
282 1
|
数据可视化 数据挖掘 开发工具
【办公自动化】用Python批量从上市公司年报中获取主要业务信息
【办公自动化】用Python批量从上市公司年报中获取主要业务信息
1108 0
|
传感器 数据采集 监控
上千个完整设计的单片机、8086、STM32制作教程和资料-转发分享
在网上收集了接近上千个完整设计的单片机、8086、STM32制作教程和资料-转发分享(涵盖了大部分的毕设课设题目),学习单片机的最好教程,也可以作为帮助大家在做电子课设毕设时有利的帮助,可以从以下百度网盘下载(按照编号下载)。
2687 0
上千个完整设计的单片机、8086、STM32制作教程和资料-转发分享
|
自然语言处理 算法 数据可视化
NLP-基于bertopic工具的新闻文本分析与挖掘
这篇文章介绍了如何使用Bertopic工具进行新闻文本分析与挖掘,包括安装Bertopic库、加载和预处理数据集、建立并训练主题模型、评估模型性能、分类新闻标题、调优聚类结果的详细步骤和方法。
NLP-基于bertopic工具的新闻文本分析与挖掘
|
7月前
|
机器学习/深度学习 数据采集 自然语言处理
基于Python的情感分析与情绪识别技术深度解析
本文探讨了基于Python的情感分析与情绪识别技术,涵盖基础概念、实现方法及工业应用。文中区分了情感分析与情绪识别的核心差异,阐述了从词典法到深度学习的技术演进,并通过具体代码展示了Transformers架构在细粒度情感分析中的应用,以及多模态情绪识别框架的设计。此外,还介绍了电商评论分析系统的构建与优化策略,包括领域自适应训练和集成学习等方法。未来,随着深度学习和多模态数据的发展,该技术将更加智能与精准。
412 1
|
11月前
|
传感器 安全 物联网
新技术潮流之巅:区块链、物联网与虚拟现实的融合与创新
在数字时代的浪潮中,新兴技术不断涌现,重塑着我们的工作和生活方式。本文将深入探讨三项引领未来的技术:区块链、物联网(IoT)和虚拟现实(VR),并揭示它们如何独立发展同时又相互交织,共同构建一个更加智能、互联的世界。我们将从技术的基本原理出发,探索它们在实际应用场景中的革新作用,以及这些技术融合后所带来的全新可能性。通过具体案例分析,本文旨在为读者提供一个关于未来技术趋势及其应用的全面视角。
181 7
|
11月前
|
自然语言处理 算法 搜索推荐
NLTK模块使用详解
NLTK(Natural Language Toolkit)是基于Python的自然语言处理工具集,提供了丰富的功能和语料库。本文详细介绍了NLTK的安装、基本功能、语料库加载、词频统计、停用词去除、分词分句、词干提取、词形还原、词性标注以及WordNet的使用方法。通过示例代码,帮助读者快速掌握NLTK的核心功能。
2000 1
|
机器学习/深度学习 编解码 自然语言处理
卷积神经网络(CNN)的发展历程
【10月更文挑战第1天】卷积神经网络(CNN)的发展历程