LDA主题挖掘

本文介绍了如何使用Python库Gensim对新闻文本数据进行预处理、创建词袋表示,并训练LDA模型来确定5个主题,展示了主题内容和潜在的主题分布可能的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import pandas as pd
from gensim.models import LdaModel
from gensim.corpora import Dictionary
import matplotlib.pyplot as plt
from multiprocessing import freeze_support

# 读取新闻文本数据
df = pd.read_excel('nltk处理后新闻合并.xlsx', header=0, names=['cleaned_text'])

# 处理NaN值并将文本转换为词袋表示
def preprocess_text(text):
    if pd.isnull(text):
        return ""
    return text

df['cleaned_text'] = df['cleaned_text'].apply(preprocess_text)
tokenized_texts = [text.split() for text in df['cleaned_text'] if text]
dictionary = Dictionary(tokenized_texts)
corpus = [dictionary.doc2bow(text) for text in tokenized_texts]

if __name__ == '__main__':
    freeze_support()

    # 固定LDA主题数量为5
    num_topics = 5

    # 训练LDA模型
    lda_model = LdaModel(corpus, num_topics=num_topics, id2word=dictionary, passes=5)

    # 打印各主题内容
    for topic_idx, topic_words in lda_model.print_topics():
        print(f"Topic {topic_idx + 1}: {topic_words}")

    # 可以选择绘制主题分布图等其他操作
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值