当前位置: 首页 > news >正文

海洋主题文本聚类研究与可视化分析

研究代码,使用Jupyter分层顺序打开使用。

# coding:utf-8 import os import csv import jieba import jieba.analyse import pandas as pd import warnings warnings.filterwarnings('ignore') # 在gensim包中使用LDA from gensim import corpora, models from gensim.test.utils import common_texts from gensim.corpora.dictionary import Dictionary # 载入自定义的分词词典 jieba.load_userdict(os.path.join('./model_data', "jiebaDict.txt")) # 读取停用词表并去重 def get_stop_words_set(file_name): with open(file_name, 'r', encoding='utf-8') as file: return set([line.strip() for line in file]) # 待分析数据去除停用词 def get_words_list(file_name, index, stop_word_file): ''' filename:待分析数据文件名 index:待分析数据所在列 stop_word_file:停用词文件名 ''' stop_words_set = get_stop_words_set(stop_word_file) print("共导入 %d 个停用词" % len(stop_words_set)) word_list = [] with open(file_name, 'r', encoding = 'gbk', errors = 'ignore') as csvfile: reader = csv.reader(csvfile) for line in reader: tmp_list = list(jieba.cut(line[index - 1], cut_all = False)) tmplist = [] for tmp in tmp_list: tmplist.append(tmp.upper()) word_list.append([term for term in tmplist if str(term) not in stop_words_set]) return word_list # 文本列表,其中每个元素也是一个列表,即每行文字分词后形成的词语列表 word_list = get_words_list('./model_data/51job_describ.csv', 9, './model_data/stop_words.txt') # 第9列:岗位要求 print(word_list[3]) # # 保存分词结果 # test = pd.DataFrame(data = [word_list]) # test.to_csv('./testcsv.csv', encoding = 'utf_8_sig') # 根据文本列表创建一个语料库,每个词与一个整型索引值对应 word_dict = corpora.Dictionary(word_list) # 词频统计,转化成空间向量格式 corpus_list = [word_dict.doc2bow(text) for text in word_list] import gensim.corpora from gensim.test.utils import common_corpus, common_dictionary from gensim.models.coherencemodel import CoherenceModel model_list = [] perplexity = [] # 困惑度 coherence_values = [] # 一致性 for num_topics in range(2, 8, 1): lda_model = models.LdaModel(corpus = corpus_list, id2word = word_dict, random_state = 1, num_topics = num_topics, passes = 20, alpha='auto') model_list.append(lda_model) # 不同主题个数下的lda模型 # 模型对应的困惑度(越低越好) perplexity_values = lda_model.log_perplexity(corpus_list) print('第 %d 个主题的Perplexity为: ' % (num_topics - 1), round(perplexity_values, 3)) perplexity.append(round(perplexity_values, 3)) # 模型对应的一致性(越高越好) coherencemodel = CoherenceModel(model = lda_model, corpus = common_corpus, coherence = 'u_mass') coherence_values.append(round(coherencemodel.get_coherence(),3)) print('第 %d 个主题的Coherence为: ' % (num_topics - 1), round(coherencemodel.get_coherence(),3)) # 最大的一致性值以及对应的主题个数 print('最大的Coherence为:' + str(max(coherence_values))) for i in range(len(coherence_values)): if coherence_values[i] == max(coherence_values): print('对应的主题个数为:' + str(i + 2)) import pyLDAvis.gensim_models # 最终模型 ## corpus: 文档词频矩阵 ## num_topics:主题数目 ## passes:训练伦次 lda = models.LdaModel(corpus = corpus_list, id2word = word_dict, random_state = 1, num_topics = 10, passes = 60, alpha='auto') # 结果展示 ## lda: 训练好的模型 d = pyLDAvis.gensim_models.prepare(lda, corpus_list, word_dict, mds = 'pcoa', sort_topics = True) pyLDAvis.save_html(d, 'lda_show.html') # 将结果保存为html文件 # 展示在notebook的output cell中 pyLDAvis.enable_notebook() vis = pyLDAvis.gensim_models.prepare(lda, corpus_list, word_dict) vis
http://www.jsqmd.com/news/485346/

相关文章:

  • 2026年上海靠谱中央空调排名,实力强的厂家推荐 - mypinpai
  • 为什么积分运算电路在反馈电容上要并联电阻
  • 教程分享:Vue2如何结合百度WebUploader插件实现大文件上传的进度可视化?
  • 航空航天Web服务如何基于百度WebUploader实现三维模型文件的跨平台分块校验?
  • 分布式驱动电动汽车模型:前轮主动转向与直接横摆力矩联合控制开发之路
  • 2026年佛山靠谱的GEO优化公司排名,知名GEO优化企业大盘点 - 工业推荐榜
  • 电动汽车集群并网的分布式鲁棒优化调度 电动汽车集群优化 采用matlab+yalmip编程,设...
  • 政务CMS如何扩展KindEditor的多格式文档智能填充?
  • CAIE认证课程体系全景图:从基础到进阶,覆盖哪些核心技术?
  • 通信工程毕业设计最新题目汇总
  • 农业信息化如何优化KindEditor的Web截屏插件功能?
  • C++编程实践—C++处理磁盘大小
  • 微信小程序 舞蹈课程预约系统 舞房约课系统
  • 2026年专科生必看!千笔·降AI率助手,最受欢迎的降AI率网站
  • 生成引擎优化(GEO)提升数字内容创作效率与用户体验的实践指南
  • 2026深度测评10款论文降重工具:3个免费方法亲测有效!谁是降重的最优解?(附论文降重避坑指南)
  • LangChain 实战:大模型对话记忆模块(附完整代码 + Web 案例)
  • 【黎阳之光:以无线专网与视频孪生,赋能智慧广电与数字中国】
  • Hello Vibe:面向小白/入门级开发者的AI编程指南
  • 虚拟机创建
  • 大厂量产的两相交错TCM图腾柱变频控制PFC+全桥LLC源代码 PFC可通过变频控制实现软开关
  • 建议收藏|一键生成论文工具,千笔写作工具 VS Checkjie,多场景适配首选
  • 5G 核心网用户面功能实体 UPF:技术演进与选型指南
  • 百度贴吧一键自动签到脚本(附Python脚本 下载方式)教程 养账号用
  • 所有人都在夸 AI 伴侣,我却看到 3 个正在发生的“情感退化”信号?
  • 鸿蒙应用开发UI基础第二十五节:手把手教打造第三方库HAR构建、混淆与发布 - 鸿蒙
  • NDW系列微机控制材料扭转试验机
  • 破局增长|中小企业低成本高回报的6个实用路径-佛山鼎策创局破局增长咨询
  • python搭建webrtc音视频服务端客户端
  • 第一个小项目天气预报