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. ES6 — 箭头函数

    一 为什么要有箭头函数 我们在日常开发中,可能会需要写类似下面的代码 const Person = { 'name': 'little bear', 'age': 18, 'sayHello': fu ...

  2. EditPLus添加到右键图文教程

    最近在研究asp听他们说EditPlus非常适合,于是下了一个,感觉还真不错,EditPlus就是一个文本编辑器,说得通俗点他和WINDOWS自带的记事本差不多,但是功能更强,一般应用于程序员编程,因 ...

  3. RAC5——11gR2以后GI进程的变化

    参考文档: 11gR2 Clusterware and Grid Home - What You Need to Know (Doc ID 1053147.1)诊断 Grid Infrastructu ...

  4. 使用crypto-js的md5加密

    官方地址:https://github.com/brix/crypto-js md5加密代码: let CryptoJS = require('crypto-js') let yxcsigns = C ...

  5. docker 知识点

    docker 教程:http://www.runoob.com/docker/docker-tutorial.html docker 仓库地址:https://store.docker.com/ do ...

  6. POJ3177(3352)(边双连通分量)

    题目: 原本没有记录桥是谁,而是染色时即时判断的.后来发现不行,因为a去b可能满足low[b]>dfn[a],但b去a就不满足了. 这是因为low和dfn的关系是相对的,仅限于tarjan时的那 ...

  7. Zigbee 的 mesh功能设置

    1. 在编译选项中加入ZIGBEEPRO 最大的节点深度为20,网络模式为mesh网络 如果不配置,则为HOME_CONTROLS,支持5级路由深度,每个路由器最多可连接20个节点(最多包括6个路由器 ...

  8. 中点Brehensam画圆算法

    #include<stdio.h> #include<stdlib.h> #include<graphics.h> #include<math.h> v ...

  9. CSS-DOM操作

    所谓CSS-DOM操作就是读取和设置style对象的各种属性,style属性很强大,但是美中不足的是无法通过它来提取到通过外部CSS设置的样式信息,然而在家jQuery中,这些就可以通过css()方法 ...

  10. LBS(基于位置服务)

    ylbtech-杂项:LBS(基于位置服务) 基于位置的服务,它是通过电信移动运营商的无线电通讯网络(如GSM网.CDMA网)或外部定位方式(如GPS)获取移动终端用户的位置信息(地理坐标,或大地坐标 ...