1.dictionary = gensim.corpora.Dictionary(clean_content)  对输入的列表做一个数字映射字典,

2. corpus = [dictionary,doc2vec(cl_content) for cl_content in clean_content]  # 输出clean_content每一个元素根据dictionary做数字映射后的结果

3.lda = gensim.model.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=20)  # corpus表示映射后的文本列表, id2word表示根据哪个数字映射字典张开, num_topics表示主题的个数

4. lda.print_topics(1, topn=5)  # 打印第一个主题,前5个词

第一步: 载入语料库数据

第二步:进行分词操作

第三步:载入停用词表,去除语料库中的停用词

第四步:

构建数字映射字典

对文本做逐个映射

构建LDA主题模型

打印主题模型的主题和前5个主题词

import pandas as pd
import numpy as np
import jieba # 1.导入数据语料的新闻数据
df_data = pd.read_table('data/val.txt', names=['category', 'theme', 'URL', 'content'], encoding='utf-8') # 2.对语料库进行分词操作
df_contents = df_data.content.values.tolist() # list of list 结构
Jie_content = []
for df_content in df_contents:
split_content = jieba.lcut(df_content)
if len(split_content) > 1 and split_content != '\t\n':
Jie_content.append(split_content) # 3. 导入停止词的语料库, sep='\t'表示分隔符, quoting控制引号的常量, names=列名, index_col=False,不用第一列做为行的列名, encoding
stopwords = pd.read_csv('stopwords.txt', sep='\t', quoting=3, names=['stopwords'], index_col=False, encoding='utf-8')
print(stopwords.head()) # 对文本进行停止词的去除
def drop_stops(Jie_content, stopwords):
clean_content = []
all_words = []
for j_content in Jie_content:
line_clean = []
for line in j_content:
if line in stopwords:
continue
line_clean.append(line)
all_words.append(line)
clean_content.append(line_clean) return clean_content, all_words
# 将DateFrame的stopwords数据转换为list形式
stopwords = stopwords.stopwords.values.tolist()
clean_content, all_words = drop_stops(Jie_content, stopwords)
print(clean_content[0]) # 4. 进行LDA主题模型
import gensim
from gensim import corpora
# 使用gensim.dictionary 生成word2vec
dictionary = corpora.Dictionary(clean_content)
print(np.shape(dictionary))
# 对clean_content 根据dictionary映射构造向量
corpus = [dictionary.doc2bow(clean_c) for clean_c in clean_content]
lda = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=20)
print(lda.print_topic(1, topn=5))

机器学习入门-贝叶斯构造LDA主题模型,构造word2vec 1.gensim.corpora.Dictionary(构造映射字典) 2.dictionary.doc2vec(做映射) 3.gensim.model.ldamodel.LdaModel(构建主题模型)4lda.print_topics(打印主题).的更多相关文章

  1. 机器学习入门-贝叶斯中文新闻分类任务 1. .map(做标签数字替换) 2.CountVectorizer(词频向量映射) 3.TfidfVectorizer(TFDIF向量映射) 4.MultinomialNB()贝叶斯模型构建

    1.map做一个标签的数字替换 2.vec = CountVectorizer(lowercase=False, max_features=4000)  # 从sklean.extract_featu ...

  2. 吴裕雄 python 机器学习——多项式贝叶斯分类器MultinomialNB模型

    import numpy as np import matplotlib.pyplot as plt from sklearn import datasets,naive_bayes from skl ...

  3. 机器学习朴素贝叶斯 SVC对新闻文本进行分类

    朴素贝叶斯分类器模型(Naive Bayles) Model basic introduction: 朴素贝叶斯分类器是通过数学家贝叶斯的贝叶斯理论构造的,下面先简单介绍贝叶斯的几个公式: 先验概率: ...

  4. 吴裕雄 python 机器学习——高斯贝叶斯分类器GaussianNB

    import matplotlib.pyplot as plt from sklearn import datasets,naive_bayes from sklearn.model_selectio ...

  5. Python之机器学习-朴素贝叶斯(垃圾邮件分类)

    目录 朴素贝叶斯(垃圾邮件分类) 邮箱训练集下载地址 模块导入 文本预处理 遍历邮件 训练模型 测试模型 朴素贝叶斯(垃圾邮件分类) 邮箱训练集下载地址 邮箱训练集可以加我微信:nickchen121 ...

  6. 机器学习---朴素贝叶斯与逻辑回归的区别(Machine Learning Naive Bayes Logistic Regression Difference)

    朴素贝叶斯与逻辑回归的区别: 朴素贝叶斯 逻辑回归 生成模型(Generative model) 判别模型(Discriminative model) 对特征x和目标y的联合分布P(x,y)建模,使用 ...

  7. spark 机器学习 朴素贝叶斯 实现(二)

    已知10月份10-22日网球场地,会员打球情况通过朴素贝叶斯算法,预测23,24号是否适合打网球.结果,日期,天气 温度 风速结果(0否,1是)天气(0晴天,1阴天,2下雨)温度(0热,1舒适,2冷) ...

  8. spark 机器学习 朴素贝叶斯 原理(一)

    朴素贝叶斯算法仍然是流行的挖掘算法之一,该算法是有监督的学习算法,解决的是分类问题,如客户是否流失.是否值得投资.信用等级评定等多分类问题.该算法的优点在于简单易懂.学习效率高.在某些领域的分类问题中 ...

  9. 机器学习入门-文本特征-使用LDA主题模型构造标签 1.LatentDirichletAllocation(LDA用于构建主题模型) 2.LDA.components(输出各个词向量的权重值)

    函数说明 1.LDA(n_topics, max_iters, random_state)  用于构建LDA主题模型,将文本分成不同的主题 参数说明:n_topics 表示分为多少个主题, max_i ...

随机推荐

  1. 剑指offer-int类型负数补码中1的个数-位操作

    在java中Interger类型表示的最大数是 System.out.println(Integer.MAX_VALUE);//打印最大整数:2147483647 这个最大整数的二进制表示,头部少了一 ...

  2. 如何判断一个请求是否为AJAX请求

    普通请求与ajax请求的报文头不一样,通过如下 String requestType = request.getHeader("X-Requested-With");  如果req ...

  3. silverlight 进行本地串口调用的一种可行的解决方法 之silverlight端代码

    接上边的文章. 在javascript暴露操作activex 串口接收之后,就是silverlight端进行串口数据的显示,我们的显示方式比较简单,只是为了演示,我们每隔1秒进行数据的获取并显示, 为 ...

  4. 创建ASM实例及ASM数据库

    --======================== -- 创建ASM实例及ASM数据库 --======================== 一.ASM相关概念 1.什么是ASM(Auto Stor ...

  5. Laravel日志查看器 -- log-viewer扩展

    1.修改laravel配置文件. config\app.php 'log'=>'daily' 2.在项目目录中composer命令安装扩展:composer require arcanedev/ ...

  6. War包反编译过程

    War包反编译过程 很多人可以将项目编译为war发布,可是有时候得到war确看不到源码.今天分享下war反编译的过程: 1.首先下载一个小工具,在http://jd.benow.ca/官网下载jd-g ...

  7. 关于buffer,cache,wb,wt,clean,inv,flush,以及其他

    1. 有时候需要区分buffer和cache:buffer解决CPU写的问题,比如将多次写操作buffer起来一次性更新:cache解决CPU读的问题,将数据cache起来在下次读的时候快速取用. 2 ...

  8. SpringCloud使用jpa之传统方式

    不说废话,直接上代码: pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xml ...

  9. C++ 获取类成员函数地址方法 浅析

    C语言中可以用函数地址直接调用函数:   void print ()   {   printf ("function print");   }   typdef void (*fu ...

  10. 2018-2019 20165226 Exp5 MSF基础应用

    2018-2019 20165226 Exp5 MSF基础应用 目录 一.实验内容说明及基础问题回答 二.实验过程 Task1 主动攻击实践 ms08_067 ms17_010 Task2 针对浏览器 ...