LDA主题模型_完整项目_CodingPark编程公园
发布日期:2021-06-29 15:45:58 浏览次数:2 分类:技术文章

本文共 5891 字,大约阅读时间需要 19 分钟。

LDA (Latent Dirichlet Allocation)

一些预备知识点,科普一下

PLSA -> LDA (Latent Dirichlet Allocation)
PLSA 参数随语料大小线性增加;LDA 参数规模与语料大小无关,只取决于topic个数和字典中term总数
可以大体理解为:从PLSA 到 LDA 增加的 就是 文章与主题的 Dirichlet分布 与 主题与关键词的 Dirichlet分布
Gensim是一个用于从文档中自动提取语义主题的Python库
gensim 包括了 LSA + LDA
LDA 可以被用来改造,做有监督文本分类 SLDA(适合长文本)
pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

import numpy as npimport pandas as pdimport refrom gensim import corpora, models, similaritiesimport gensim# corpora    语料 , models    模型, similarities    相似度'''获取文件'''df = pd.read_csv("HillaryEmails.csv")#print(df)# 原邮件数据中有很多空值,扔!df = df[['Id','ExtractedBodyText']].dropna()'''文件与处理(清洗数据)'''# 1 通过正则进行第一波清洗def clean_email_text(text):    text = text.replace('\n'," ") #新行,我们是不需要的    text = re.sub(r"-", " ", text) #把 "-" 的两个单词,分开。(比如:july-edu ==> july edu)    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 = ''# 2 第二波清洗    # 以防还有其他特殊字符(数字)等等,我们直接把他们loop一遍,过滤掉    for letter in text:    # 只留下字母和空格;  isalpha() 方法检测字符串是否只由字母组成。        if letter.isalpha() or letter==' ':            pure_text += letter    # 再把那些去除特殊字符后落单的单词,直接排除。    # 我们就只剩下有意义的单词了。    text = ' '.join(word for word in pure_text.split() if len(word)>1)    return text# 3 第三波清洗# 停止词列表 就是别因为他们影响我们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']docs = df['ExtractedBodyText']docs = docs.apply(lambda s: clean_email_text(s))#print(docs)# 在用Pandas读取数据之后,我们往往想要观察一下数据读取是否准确,这就要用到Pandas里面的head( )函数print('测试docs.head(1).values ---> ',docs.head(1).values)# 4 拿到成品  那我们就全拿出来 存放入 doclistdoclist = docs.values'''LDA模型构建'''# 1 分词# 人工分词:# 这里,英文的分词,直接就是对着空白处分割就可以了。# 中文的分词稍微复杂点儿,具体可以百度:CoreNLP, HaNLP, 结巴分词,等等# 分词的意义在于,把我们的长长的字符串(原文本)-> 转化成有意义的小元素:texts = [[word for word in doc.lower().split() if word not in stoplist] for doc in doclist]# 测试一下 texts[0]print('分词后的结果 ----> ',texts[0])# 2 建立语料dictionary = corpora.Dictionary(texts)# corpus 语料库corpus = [dictionary.doc2bow(text) for text in texts]# 来看看 语料库什么样!print('语料库 --> ',corpus[13])# 3 建立模型# 只有点到 驼峰表达式时,才是最终要的类。 其余都是文件夹lda = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=20)final_20topic = lda.print_topics(num_topics=20, num_words=5)print()print("输出20topic结果 ---> ",final_20topic)

结果展示:

测试docs.head(1).values —> [‘Thursday March PM Latest How Syria is aiding Qaddafi and more Sid hrc memo syria aiding libya docx hrc memo syria aiding libya docx March For Hillary’]
分词后的结果 ----> [‘thursday’, ‘march’, ‘pm’, ‘latest’, ‘syria’, ‘aiding’, ‘qaddafi’, ‘sid’, ‘hrc’, ‘memo’, ‘syria’, ‘aiding’, ‘libya’, ‘docx’, ‘hrc’, ‘memo’, ‘syria’, ‘aiding’, ‘libya’, ‘docx’, ‘march’, ‘hillary’]
语料库 --> [(51, 1), (505, 1), (506, 1), (507, 1), (508, 1)]

输出20topic结果 —> [(0, ‘0.019*“sent” + 0.017*“send” + 0.013*“release” + 0.013*“part” + 0.013*“blackberry”’), (1, ‘0.024*“yes” + 0.019*“pm” + 0.011*“percent” + 0.010*“huma” + 0.009*“abedin”’), (2, ‘0.024*“pm” + 0.015*“talk” + 0.013*“tomorrow” + 0.007*“today” + 0.007*“meeting”’), (3, ‘0.012*“us” + 0.008*“state” + 0.006*“said” + 0.006*“senate” + 0.005*“new”’), (4, ‘0.046*“pls” + 0.040*“print” + 0.034*“doc” + 0.017*“strategic” + 0.015*“state”’), (5, ‘0.016*“fyi” + 0.006*“beck” + 0.005*“declassify” + 0.004*“income” + 0.004*“us”’), (6, ‘0.008*“new” + 0.007*“would” + 0.005*“party” + 0.005*“one” + 0.004*“time”’), (7, ‘0.026*“fyi” + 0.008*“cheryl” + 0.008*“mcchrystal” + 0.006*“pm” + 0.006*“mills”’), (8, ‘0.020*“qddr” + 0.018*“clips” + 0.013*“im” + 0.013*“see” + 0.011*“call”’), (9, ‘0.008*“iraq” + 0.006*“israeli” + 0.005*“said” + 0.005*“iraqi” + 0.005*“funded”’), (10, ‘0.008*“miliband” + 0.006*“email” + 0.006*“ex” + 0.005*“interested” + 0.005*“pm”’), (11, ‘0.008*“message” + 0.008*“original” + 0.007*“holiday” + 0.007*“received” + 0.006*“part”’), (12, ‘0.013*“sullivan” + 0.010*“jacob” + 0.006*“pm” + 0.005*“voters” + 0.005*“said”’), (13, ‘0.074*“call” + 0.028*“pis” + 0.025*“thx” + 0.017*“pls” + 0.016*“print”’), (14, ‘0.010*“would” + 0.008*“said” + 0.006*“israel” + 0.006*“mr” + 0.006*“could”’), (15, ‘0.008*“get” + 0.007*“one” + 0.007*“would” + 0.007*“us” + 0.006*“also”’), (16, ‘0.009*“framework” + 0.007*“walter” + 0.005*“course” + 0.005*“panel” + 0.005*“state”’), (17, ‘0.012*“state” + 0.009*“party” + 0.008*“secretary” + 0.007*“assistant” + 0.006*“house”’), (18, ‘0.052*“pm” + 0.027*“office” + 0.021*“secretarys” + 0.018*“meeting” + 0.015*“room”’), (19, ‘0.050*“ok” + 0.017*“sullivan” + 0.010*“waldorf” + 0.010*“december” + 0.009*“assume”’)]

在这里插入图片描述

转载地址:https://codingpark.blog.csdn.net/article/details/105408247 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Pycharm输入字母无法正常显示_小技巧_CodingPark编程公园
下一篇:自定义数据集-Pokenom Go_完整项目_CodingPark编程公园

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月21日 17时07分06秒