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)
相关文章
|
机器学习/深度学习 自然语言处理 算法
文本分析-使用jieba库进行中文分词和去除停用词(附案例实战)
文本分析-使用jieba库进行中文分词和去除停用词(附案例实战)
10485 145
|
数据可视化 数据挖掘
基于Bert的文本聚类工具:BERTopic
基于Bert的文本聚类工具:BERTopic
2499 0
基于Bert的文本聚类工具:BERTopic
|
自然语言处理 算法 数据可视化
NLP-基于bertopic工具的新闻文本分析与挖掘
这篇文章介绍了如何使用Bertopic工具进行新闻文本分析与挖掘,包括安装Bertopic库、加载和预处理数据集、建立并训练主题模型、评估模型性能、分类新闻标题、调优聚类结果的详细步骤和方法。
NLP-基于bertopic工具的新闻文本分析与挖掘
|
6月前
|
机器学习/深度学习 自然语言处理 数据可视化
28_主题建模详解:从LDA到BERTopic - 深度解析与教学
主题建模(Topic Modeling)是自然语言处理(NLP)领域的核心技术之一,旨在从大量非结构化文本中自动发现潜在的主题结构和语义模式。随着大语言模型的崛起,主题建模技术也在不断演进,从传统的统计方法到基于深度学习的高级模型,为文本理解、信息检索、舆情分析等任务提供了强大的技术支撑。
1480 0
|
11月前
|
机器学习/深度学习 人工智能 关系型数据库
通义 CoGenAV 大模型音画同步感知,重新定义语音理解边界
CoGenAV 是一种创新的多模态语音理解模型,核心理念是实现“音画同步”的深度理解。通过学习 audio-visual-text 的时序对齐关系,构建更鲁棒、更通用的语音表征框架。它在视觉语音识别(VSR)、音视频语音识别(AVSR)、语音增强与分离(AVSE/AVSS)及主动说话人检测(ASD)等任务中表现出色,尤其在嘈杂环境下性能显著提升。仅需 223 小时数据训练即可媲美传统数千小时数据的效果,大幅降低训练成本。CoGenAV 支持主流平台如 GitHub、HuggingFace 和 ModelScope,助力多场景应用开发。
1253 10
|
自然语言处理 网络安全 Python
【Python】已解决:nltk.download(‘punkt’) [nltk_data] Error loading punkt: [WinError 10060] [nltk_data]
【Python】已解决:nltk.download(‘punkt’) [nltk_data] Error loading punkt: [WinError 10060] [nltk_data]
4188 1
|
人工智能 算法 新能源
TRIZ专利策略:快速技术创新,并实现高质量专利突破
在当今竞争激烈的市场中,高质量发明专利是企业核心竞争力的关键。TRIZ(发明问题解决理论)作为一种系统化的创新方法,通过分析问题本质、解决矛盾与冲突,为企业提供高效的专利突破路径。本文介绍了TRIZ的核心理念、特点及其在智能手机、新能源汽车、医疗器械等行业的成功应用案例,同时阐述了运用TRIZ实现高质量专利突破的具体步骤。无论企业追求技术领先还是规避侵权风险,TRIZ都能助力制定更优的专利策略。联系法思诺获取更多创新咨询与培训服务。
424 0
Bert可以提取关键词了:KeyBERT的介绍与使用
Bert可以提取关键词了:KeyBERT的介绍与使用
2883 1
Bert可以提取关键词了:KeyBERT的介绍与使用
|
自然语言处理 数据可视化 数据挖掘
BERTopic(一)基本用法
bertopic基本用法
973 1