基于BERTopic模型的中文文本主题聚类及可视化

本文介绍了如何利用BERTopic模型对中文文本进行主题聚类和可视化。首先,简述了BERTopic的基本原理和模型加载,接着展示了数据处理、文本嵌入和模型构建的过程。实验中,对京东评论数据集进行处理,通过模型得到主题数量,并对主题分布进行了详细展示,包括词概率分布、聚类可视化和主题相似度等。最后,强调了BERTopic在自动识别文本主题方面的优势。
摘要由CSDN通过智能技术生成

BERTopic简介

BERTopic论文地址:BERTopic: Neural topic modeling with a class-based TF-IDF procedure

BERTopic是一种结合了预训练模型BERT和主题建模的强大工具。它允许我们将大规模文本数据集中的文档映射到主题空间,并自动识别潜在的主题。

它背后的核心思想是通过BERT模型来捕获文档的语义信息,并然后使用主题建模技术来对这些语义信息进行聚类,从而得出主题。

模型加载地址

https://public.ukp.informatik.tu-darmstadt.de/reimers/sentence-transformers/v0.2/

在这里插入图片描述

  • 默认的英文文本
好的,下面是一个简单的例子,使用Python的sklearn库来对中文文本进行Kmeans聚类可视化。 首先,我们需要准备一些中文文本数据。这里我们使用一个包含多篇新闻的文本数据集,可以从这里下载:https://github.com/crownpku/Information-Retrieval-Course/tree/master/data 接下来,我们需要对文本进行预处理,包括中文分词、去除停用词、提取文本特征等。这里我们使用jieba和sklearn库来完成这些任务。 ```python import jieba import jieba.analyse from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.cluster import KMeans import matplotlib.pyplot as plt # 加载文本数据 def load_data(file_path): data = [] with open(file_path, 'r', encoding='utf-8') as f: for line in f: data.append(line.strip()) return data # 中文分词 def chinese_word_cut(text): return " ".join(jieba.cut(text)) # 停用词列表 def get_stop_words(file_path): stop_words = [] with open(file_path, 'r', encoding='utf-8') as f: for line in f: stop_words.append(line.strip()) return stop_words # 提取文本特征 def get_tfidf_features(data, stop_words): tfidf_vectorizer = TfidfVectorizer(stop_words=stop_words, max_df=0.95, min_df=2, tokenizer=chinese_word_cut) tfidf_matrix = tfidf_vectorizer.fit_transform(data) return tfidf_matrix, tfidf_vectorizer # Kmeans聚类 def kmeans_cluster(tfidf_matrix, n_clusters): km_cluster = KMeans(n_clusters=n_clusters, max_iter=300, n_init=40, init='k-means++',n_jobs=-1) km_cluster.fit(tfidf_matrix) return km_cluster # 可视化聚类结果 def plot_cluster(tfidf_matrix, km_cluster, n_clusters): plt.rcParams['font.sans-serif'] = ['SimHei'] # 中文显示 plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题 fig, ax = plt.subplots(figsize=(10, 6)) colors = ['r', 'g', 'b', 'c', 'm', 'y', 'k', 'w'] for i in range(n_clusters): cluster = tfidf_matrix[km_cluster.labels_ == i].toarray() ax.scatter(cluster[:, 0], cluster[:, 1], s=30, c=colors[i % len(colors)], label='Cluster %d' % i) ax.legend() ax.grid(True) ax.set_xlabel('Feature 1') ax.set_ylabel('Feature 2') ax.set_title('Kmeans Clustering') plt.show() if __name__ == '__main__': # 加载文本数据 data = load_data('news.txt') # 中文分词 data_cut = list(map(chinese_word_cut, data)) # 加载停用词列表 stop_words = get_stop_words('stop_words.txt') # 提取文本特征 tfidf_matrix, tfidf_vectorizer = get_tfidf_features(data_cut, stop_words) # Kmeans聚类 n_clusters = 3 km_cluster = kmeans_cluster(tfidf_matrix, n_clusters) # 可视化聚类结果 plot_cluster(tfidf_matrix, km_cluster, n_clusters) ``` 运行完上述代码后,我们可以得到一个可视化聚类结果,如下图所示: ![Kmeans聚类可视化结果](https://img-blog.csdn.net/20180220132937488?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGF2aWRzbWFzZG9uZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/q/80)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cachel wood

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值