• LDA算法并提取这份数据集中各个文档的主题


    任务描述:现有一份“网易新闻语料”数据集,请尝试用Python或Java实现LDA算法并提取这份数据集中各个文档的主题,并显示出来(可参考下图的输出结果,可网上拷贝代码,但需对算法以及代码有一定的基本了解),其中,附件5中“news.dat”为“网易新闻语料”,包含了2043个文档,附件6为停止词列表(主要用于删除news.dat中的无效词,避免对主题提取的干扰)。

    代码如下:

    1. import jieba
    2. import pickle
    3. from gensim import corpora, models
    4. # 1. 准备数据
    5. with open('./Dateset/news.dat', 'r', encoding='utf-8') as f:
    6. documents = f.readlines()
    7. with open('./Dateset/stopword.txt', 'r', encoding='gbk') as f:
    8. stopwords = [line.strip() for line in f.readlines()]
    9. #
    10. # 2. 数据预处理
    11. texts = []
    12. for doc in documents:
    13. words = jieba.lcut(doc) #有关jieba.lcut 参见https://blog.csdn.net/liujingwei8610/article/details/121758179
    14. words = [word for word in words if word not in stopwords]
    15. texts.append(words)
    16. # with open("texts.pkl", "wb") as f:
    17. # pickle.dump(texts, f)
    18. # with open("texts.pkl", "rb") as f:
    19. # texts = pickle.load(f)
    20. # 3. 构建词袋模型
    21. dictionary = corpora.Dictionary(texts) #字典封装了规范化单词与其整数 ID 之间的映射。 简单来说,将每个字符串(词/词组)映射为不同数字
    22. corpus = [dictionary.doc2bow(text) for text in texts] #dictionary.doc2bow(text) Convert `document` into the bag-of-words (BoW) format = list of `(token_id, token_count)` tuples.
    23. # 4. 训练LDA模型
    24. lda_model = models.LdaModel(corpus, num_topics=10, id2word=dictionary)
    25. # #
    26. # with open("lda_model.pkl", "wb") as f:
    27. # pickle.dump(lda_model, f)
    28. # with open("lda_model.pkl", "rb") as f:
    29. # lda_model = pickle.load(f)
    30. # 5. 提取主题
    31. topics = []
    32. for doc in corpus:
    33. topic = lda_model.get_document_topics(doc,minimum_probability = 0.000001) #minimum_probability 避免概率过低,取不到足够的主题词
    34. topics.append(topic)
    35. # 6. 显示主题结果
    36. print("10个文档的主题分布")
    37. topicList=[]
    38. for i, topic in enumerate(topics):
    39. print(f"第 {i+1}个文档的前10个主题:")
    40. for t in topic:
    41. topicList.append(t[1])
    42. print(topicList)
    43. topicList.clear()
    44. print()

    完成一轮
    1. for doc in documents:
    2. words = jieba.lcut(doc) #有关jieba.lcut 参见https://blog.csdn.net/liujingwei8610/article/details/121758179
    3. words = [word for word in words if word not in stopwords]
    4. texts.append(words)
    texts内容如下:

    完成 corpus = [dictionary.doc2bow(text) for text in texts] 后如下图。转换结果相当于每篇文章中的词频率统计。
    说明:dictionary.doc2bow(text) Convert `document` into the bag-of-words (BoW) format = list of `(token_id, token_count)` tuples.
    

    lda_model = models.LdaModel(corpus, num_topics=10, id2word=dictionary)
    这里使用的Lda模型为发表于NIPS的论文:Online Learning for Latent Dirichlet Allocation
    这里做简单介绍:  传统的LDA模型参见: 一文详解LDA主题模型 - 知乎 (zhihu.com)
    

    《Online Learning for Latent Dirichlet Allocation》是一篇关于在线学习的隐含狄利克雷分配(Latent Dirichlet Allocation,简称LDA)的论文。
         该论文主要介绍了一种在线学习的方法,用于在大规模文本数据集上进行LDA模型的训练和推断。传统的LDA算法需要将整个数据集加载到内存中进行批量处理,而在线学习的方法可以逐步处理数据,适用于数据量大、不断增长的场景。
         论文中提出了一种基于变分推断的在线LDA算法,通过引入一些技巧和近似方法,实现了高效的在线学习和参数更新。该方法可以在不断到达新文档时,动态地更新模型参数,同时保持对历史文档的推断结果。
         论文还介绍了在线LDA算法的推导过程、参数更新规则以及实验结果。通过在多个大规模文本数据集上进行实验,论文验证了在线LDA算法的有效性和可扩展性,对于处理大规模文本数据集和动态数据流具有重要的意义。

    主题提取部分结果如下: 

    1. 10个文档的主题分布
    2. 1个文档的前10个主题:
    3. [0.01087394, 0.00019692296, 0.7152148, 0.00019692726, 0.00019692296, 0.00019694411, 0.00019694012, 0.15501586, 0.00019697643, 0.11771379]
    4. 2个文档的前10个主题:
    5. [0.85160214, 6.795004e-05, 0.009034262, 6.7951565e-05, 6.795004e-05, 6.795801e-05, 6.795616e-05, 0.07648344, 0.044096787, 0.018443592]
    6. 3个文档的前10个主题:
    7. [0.00025585885, 0.000102130005, 0.00010215806, 0.0001021321, 0.000102130005, 0.000102141545, 0.00010213848, 0.005841021, 0.86980826, 0.12348203]
    8. 4个文档的前10个主题:
    9. [0.0006572597, 0.0006570946, 0.00065728865, 0.00065710756, 0.0006570946, 0.0006571689, 0.0006571418, 0.00065724866, 0.0006572691, 0.9940853]
    10. 5个文档的前10个主题:
    11. [0.09089063, 5.748639e-05, 0.42282772, 5.748767e-05, 5.748639e-05, 5.7492874e-05, 5.749149e-05, 0.14581433, 0.03272865, 0.30745125]
    12. 6个文档的前10个主题:
    13. [0.01268742, 4.7202997e-05, 0.2854256, 4.7204012e-05, 4.7202997e-05, 4.720842e-05, 4.720712e-05, 0.36365467, 0.04033447, 0.29766187]
    14. 7个文档的前10个主题:
    15. [0.004294038, 6.870105e-05, 0.08180204, 6.8702626e-05, 6.870105e-05, 6.87093e-05, 6.870766e-05, 0.7040278, 6.871957e-05, 0.2094639]
    16. 8个文档的前10个主题:
    17. [0.6133319, 0.0004353154, 0.23381416, 0.00043532476, 0.0004353154, 0.0004353643, 0.0004353534, 0.0004354392, 0.0004440389, 0.14979775]
    18. 9个文档的前10个主题:
    19. [0.00048655766, 0.00048641532, 0.40117988, 0.00048642544, 0.00048641532, 0.0004864658, 0.00048645603, 0.521131, 0.00048655254, 0.07428386]
    20. 10个文档的前10个主题:
    21. [0.017286241, 6.7313216e-05, 0.031750664, 6.7314664e-05, 6.7313216e-05, 6.732086e-05, 6.7319226e-05, 0.45735896, 0.012209379, 0.48105815]
    22. 11个文档的前10个主题:
    23. [0.00032338154, 0.0003232927, 0.7388656, 0.00032329932, 0.0003232927, 0.00032332516, 0.00032331893, 0.074141674, 0.00032338157, 0.18472938]

  • 相关阅读:
    springboot配置多个数据源
    flink版本升级之 checkpoint和savepoint 代码和SQL
    2023高教社杯国赛数学建模C题思路+模型+代码(9.7晚开赛后第一时间更新)
    JavaScript相关概念
    7月份最后一篇博客
    Appium移动自动化测试--安装Appium
    昇思25天学习打卡营第15天 | Vision Transformer图像分类
    hvv蓝初 看完可去 面试可用 面经
    神经网络模型训练简记(一)
    leetcode 二叉树的公共近祖先
  • 原文地址:https://blog.csdn.net/wangxiaojie6688/article/details/133231903