三.主题模型

https://blog.csdn.net/qq_39422642/article/details/78730662
详细介绍

LDA

是一种无监督的贝叶斯模型
可以将文档集中每篇文档的主题按照概率分布的形式给出。同时它是一种无监督学习算法,在训练时不需要手工标注的训练集,需要的是文档集和指定主题的数量k即可。此外LDA的有点事,对于每一个主题均可找出一些词语来描述它。
是一种典型的词袋模型,词与词之间的顺序没有考虑。

理论:贝叶斯公式

模型:用概率作为可信度
需要一个模型来解释数据的生成。

先验,后验,似然
P(好工程师|简历)=P(好工程师)P(简历|好工程师)
后验概率=先验概率*似然概率
p(单词|文档)=P(单词|主题)P(主题|文档)

LDA生成过程:
1.对每一篇文档,从主题分布中抽取一个主题;
2.从上述被抽到的主题所对应的单词分布中抽取一个单词;
3.重复上述过程制止遍历文档中的每一个单词

PLSA和LDA对比:
PLSA和LDA的本质区别就在他们去估计未知参数所采用的的思想不同,前者采用的是频率派思想,后者用的是贝叶斯派思想。

希拉里邮件门

from gensim import corpora, models, similarities
import gensim
import numpy as np
import pandas as pd
import re # 正则表达式
# 读入数据
df = pd.read_csv("../input/HillaryEmails.csv")
# 原邮件数据中有很多Nan的值,直接扔了。
df = df[['Id','ExtractedBodyText']].dropna()
#文本预处理
def clean_email_text(text):
    text  = text.replace('\n'," ") # 新行变成空格
    text = re.sub('-'," ",text) # 两个单词用-连接的分开
    text = re.sub(r"\d+/\d+/\d+", "", text) # 三个连输数字为日期,对主体模型没什么意义
    text = re.sub(r"[0-2]?[0-9]:[0-6][0-9]", "", text) #时间,没意义
    text = re.sub(r"[\w]+@[\.\w]+", "", text) #邮件地址,没意义
    text = re.sub(r"/[a-zA-Z]*[:\//\]*[A-Za-z0-9\-_]+\.+[A-Za-z0-9\.\/%&=\?\-_]+/i", "", text) #网址,没意义
    pure_text = ''
    for letter in text:
        if letter.isalpha() or letter ==' ':
            pure_text += letter
    text = ' '.join(word for word in pure_text.split() if len(word)>1)
    return text

docs = df['ExtractedBodyText']
docs = docs.apply(lambda x :clean_email_text(x)) #apply表示应用这个函数,lambda匿名函数
docslist = docs.values # 将值取出来变成列表


# LDA模型,语料、模型、测试相似度
from gensim import corpora, models, similarities
import gensim
# 直接使用停用词列表或者NLTK
stoplist = ['very', 'ourselves', 'am', 'doesn', 'through', 'me', 'against', 'up', 'just', 'her', 'ours', 
            'couldn', 'because', 'is', 'isn', 'it', 'only', 'in', 'such', 'too', 'mustn', 'under', 'their', 
            'if', 'to', 'my', 'himself', 'after', 'why', 'while', 'can', 'each', 'itself', 'his', 'all', 'once', 
            'herself', 'more', 'our', 'they', 'hasn', 'on', 'ma', 'them', 'its', 'where', 'did', 'll', 'you', 
            'didn', 'nor', 'as', 'now', 'before', 'those', 'yours', 'from', 'who', 'was', 'm', 'been', 'will', 
            'into', 'same', 'how', 'some', 'of', 'out', 'with', 's', 'being', 't', 'mightn', 'she', 'again', 'be', 
            'by', 'shan', 'have', 'yourselves', 'needn', 'and', 'are', 'o', 'these', 'further', 'most', 'yourself', 
            'having', 'aren', 'here', 'he', 'were', 'but', 'this', 'myself', 'own', 'we', 'so', 'i', 'does', 'both', 
            'when', 'between', 'd', 'had', 'the', 'y', 'has', 'down', 'off', 'than', 'haven', 'whom', 'wouldn', 
            'should', 've', 'over', 'themselves', 'few', 'then', 'hadn', 'what', 'until', 'won', 'no', 'about', 
            'any', 'that', 'for', 'shouldn', 'don', 'do', 'there', 'doing', 'an', 'or', 'ain', 'hers', 'wasn', 
            'weren', 'above', 'a', 'at', 'your', 'theirs', 'below', 'other', 'not', 're', 'him', 'during', 'which']
# 句子拆成单词
texts = [[word for word in doc.lower().split() if word not in stoplist] for doc in doclist]

# 建立语料库,用词袋的方法,把每个单词用一个数字index指代,并把原文本变成一条长长的数组
dictionary = corpora.Dictionary(texts) # 变成表
corpus = [dictionary.doc2bow(text) for text in texts] #文本转换为数字,包括数字以及这个数字对应单词对应的出现次数

lda = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=20) # LDA模型,到驼峰才是使用的类,设置主题的个数,主题数越多会越精细
lda.print_topic(10, topn=5)

lda.print_topics(num_topics=5,num_words =6) #打印


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值