LDA主题模型通俗理解并进行文本分类

LDA主题模型通俗理解并进行文本分类

在这里插入图片描述令人头秃
**
数据集下载:链接:https://pan.baidu.com/s/1zxrKtTYli2iQgK1iNVP9PQ 提取码:la3w


LDA通俗理解:
LDA就是在给定的分类数K下,可以把数据集分成K类,并且给出每个类别所包含的主要的特征


具体LDA实现的细节可以自行搜索。

假设具有以下句子集:

1.我喜欢吃西兰花和香蕉。
2.我早餐吃了香蕉和菠菜冰沙。
3.龙猫和小猫很可爱。
4.我姐姐昨天收养了一只小猫。
5.看看这只可爱的仓鼠在西兰花上嚼。

什么是潜在的Dirichlet分配? 这是一种自动发现这些句子包含的主题的方法。 例如,给定这些句子并要求2个主题,LDA可能会产生类似

句子1和2:100%主题A
句子3和4:100%主题B
句子5:主题A为60%,主题B为40%

主题A:30%的西兰花,15%的香蕉,10%的早餐,10%的咀嚼……(此时,您可以将主题A解释为与食物有关)
主题B:20%的龙猫,20%的小猫,20%的可爱,15%的仓鼠……(此时,您可以将主题B解释为与可爱的动物有关)

这样就相当于把数据集进行了一个聚类,分出了句子之间的关系,并且给出了句子类别所具有的特征。

下面是一个分类古代史、近代史、和现代史的demo

import os
import re
import jieba
import pandas as pd
import tensorflow as tf
import numpy as np
tf.__version__
#设置根目录
root='data/百度题库/高中_历史/origin'
ancient_his_df=pd.read_csv(os.path.join(root,'古代史.csv'))
contemporary_his_df=pd.read_csv(os.path.join(root,'现代史.csv'))
modern_his_df=pd.read_csv(os.path.join(root,'近代史.csv'))
#添加标签
ancient_his_df['label']='__label__古代史'
contemporary_his_df['label']='__label__现代史'
modern_his_df['label']='__label__近代史'
#停用词加载函数
def load_stop_words(stop_word_path):
    file=open(stop_word_path,'r',encoding='utf-8')
    stop_words=file.readlines()
    stop_words=[stop_word.strip() for stop_word in stop_words]
    return stop_words
stopwords_path='data/stopwords/哈工大停用词表.txt'
#数据预处理+分词函数
def clean_sentence(line):
    line = re.sub(
            "[a-zA-Z0-9]|[\s+\-\|\!\/\[\]\{\}_,.$%^*(+\"\')]+|[::+——()?【】《》“”!,。?、~@#¥%……&*()]+|题目", '',line)
    tokens = jieba.cut(line, cut_all=False)
    return tokens  
#进行数据预处理
stop_words=load_stop_words(stopwords_path)
def sentence_proc(sentence):
    words=clean_sentence(sentence)
    words=[word for word in words if word not in stop_words]
    return ' '.join(words) 
ancient_his_df['item']=ancient_his_df['item'].apply(sentence_proc)
contemporary_his_df['item']=contemporary_his_df['item'].apply(sentence_proc)
modern_his_df['item']=modern_his_df['item'].apply(sentence_proc)
#整合结果
dataset_df=pd.concat([ancient_his_df,contemporary_his_df,modern_his_df])
#创建LDA模型
#转换格式[[]]
common_texts=dataset_df['item'].apply(lambda x:x.split()).tolist()
import numpy as np
from gensim.corpora.dictionary import Dictionary
from gensim.models import LdaModel
#把文章转换成list
dictionary=Dictionary(common_texts)
#print(type(common_texts))
#print(common_texts[0])
#把文本转换成词袋的形式  id:freq
corpus=[dictionary.doc2bow(text) for text in common_texts]
lda=LdaModel(corpus,id2word=dictionary,num_topics=3)

#结果:
lda.print_topic(0,topn=50)
#'0.020*"答案" + 0.016*"解析" + 0.015*"知识点" + 0.011*"中国" + 0.009*"发展" + 0.009*"材料" + 0.009*"年" + 0.008*"使用" + 0.008*"空间" + 0.008*"难度" + 0.008*"查看" + 0.008*"加入" + 0.008*"复制" + 0.008*"次数" + 0.008*"选题" + 0.008*"单选题" + 0.008*"篮" + 0.008*"收藏" + 0.008*"纠错" + 0.008*"题型" + 0.007*"排除" + 0.007*"中" + 0.007*"经济" + 0.006*"错误" + 0.005*"不" + 0.005*"出现" + 0.005*"英国" + 0.005*"新" + 0.004*"世界" + 0.004*"正确" + 0.004*"主要" + 0.004*"故项" + 0.004*"本题" + 0.004*"美国" + 0.004*"资本主义" + 0.004*"民族" + 0.004*"符合" + 0.004*"国家" + 0.004*"项" + 0.004*"考查" + 0.004*"可知" + 0.004*"知识" + 0.003*"世界市场" + 0.003*"所学" + 0.003*"影响" + 0.003*"题意" + 0.003*"反映" + 0.003*"工业" + 0.003*"世纪" + 0.003*"后"'
lda.print_topic(1,topn=50)
lda.print_topic(2,topn=50)
#'0.016*"答案" + 0.013*"知识点" + 0.011*"解析" + 0.011*"材料" + 0.010*"制度" + 0.009*"中国" + 0.007*"选题" + 0.007*"查看" + 0.007*"空间" + 0.007*"收藏" + 0.007*"题型" + 0.006*"难度" + 0.006*"次数" + 0.006*"纠错" + 0.006*"复制" + 0.006*"民主" + 0.006*"使用" + 0.006*"篮" + 0.006*"加入" + 0.006*"排除" + 0.006*"思想" + 0.005*"单选题" + 0.005*"人" + 0.005*"政治" + 0.005*"中" + 0.005*"不" + 0.004*"错误" + 0.004*"项" + 0.004*"知识" + 0.004*"年" + 0.004*"本题" + 0.004*"正确" + 0.003*"可知" + 0.003*"文化" + 0.003*"考查" + 0.003*"所学" + 0.003*"发展" + 0.003*"体现" + 0.003*"新" + 0.003*"出现" + 0.003*"能力" + 0.003*"符合" + 0.003*"主要" + 0.003*"西方" + 0.002*"反映" + 0.002*"近代" + 0.002*"民族" + 0.002*"故项" + 0.002*"主张" + 0.002*"没有"'
#预测:
bow_sample=[(370,1)]#随便找一个样本
lda.get_document_topics(bow_sample)#预测
预测结果:[(0, 0.2056313), (1, 0.2160506), (2, 0.5783181)]

这样就得到了LDA模型,再对别的样本进行分类时只要调用这个模型就能给出此样本所属类别的概率。

发布了12 篇原创文章 · 获赞 1 · 访问量 193

猜你喜欢

转载自blog.csdn.net/weixin_42813521/article/details/104987458
今日推荐