mmmaaatttllbb 2024-01-29 21:39 采纳率: 100%
浏览 9
已结题

BERTopic主题建模报错

Batches到 100%时就出现以下报错信息:TypeError: 'numpy.float64' object cannot be interpreted as an integer
数据是中文期刊的摘要,不知道是哪里出现问题了。感谢!

import pandas as pd
import jieba
from bertopic import BERTopic
from sentence_transformers import SentenceTransformer
file_path = ''
data = pd.read_excel(file_path)

# 读取和合并停用词表
stopwords = set()
for file in ['hit_stopwords.txt', 'scu_stopwords.txt', 'baidu_stopwords.txt', 'cn_stopwords.txt']:
    with open(file, 'r', encoding='utf-8') as f:
        stopwords.update([line.strip() for line in f.readlines()])

# 分词和去除停用词
def preprocess(text):
    words = jieba.cut(text)
    return ' '.join(word for word in words if word not in stopwords)

data['processed'] = data['摘要'].apply(preprocess)

# 加载预训练的中文句子嵌入模型
model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')

# 为处理后的文档生成嵌入
embeddings = model.encode(data['processed'].tolist(), show_progress_bar=True)

# 创建BERTopic模型
topic_model = BERTopic(language="multilingual", calculate_probabilities=True)

# 使用文档和生成的嵌入训练模型
topics, probabilities = topic_model.fit_transform(data['processed'], embeddings)

for topic in topic_model.get_topic_info().head(10)['Name']:
    print(topic)

  • 写回答

3条回答 默认 最新

  • mmmaaatttllbb 2024-01-30 21:12
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月7日
  • 已采纳回答 1月30日
  • 修改了问题 1月29日
  • 创建了问题 1月29日

悬赏问题

  • ¥15 封装的 matplotlib animation 不显示图像
  • ¥15 python摄像头画面无法显示
  • ¥15 关于#3d#的问题:d标定算法(语言-python)
  • ¥15 cve,cnnvd漏洞扫描工具推荐
  • ¥15 图像超分real-esrgan网络自己训练模型遇到问题
  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错