朴素贝叶斯概述

贝叶斯公式

首先我们用一个例子来解释一下贝叶斯公式的使用。

有一个村子,里面一共有三个小偷,他们的偷窃能力分别是,如何求出这个村子出现失窃案件的概率P(B)?

分析:

首先我们知道,这个村子中只有三个小偷,因此三个小偷构成了一个完备事件组,即他们的能力的总和为全集,且相互之间独立。因此我们可以建立等式:




相互独立


如果这个村子里有n个小偷的话,我们可以总结成以下公式:

以上便可以求得村子失窃的概率P(B),而这个公式也被称为全概率公式或者全集分解

那么我们如果想知道,如果村子已经失窃,这三个小偷的谁偷的概率最大呢?按照常理来讲,谁的偷盗能力最强,其实谁偷窃的概率就最大。这是我们通过日常生活总结出的经验。那么如何用理论计算呢?
按照警察的描述,一定是:在村子失窃的基础上去查看他们三个偷盗的可能性
以计算Aj小偷的可能性为例:

而P(B)可以带入全概率公式,得:

以上公式便是我们的主角——贝叶斯公式。
如果小伙伴对以上推导过程有疑问,请参考概率公式部分汇总。

贝叶斯在机器学习中化简

对于上述三个小偷,都面临对于此案件的调查,也就是说对于计算三个小偷实施盗窃可能性的贝叶斯公式里,P(B)始终是恒定的。因此。
下面通俗的介绍两个概念:

最大似然估计:最符合观测数据的(P(B|A)最大),最有优势。
奥卡姆剃刀:P(A)越大的模型具有较大优势。

朴素贝叶斯

在上面我们已经完整介绍了一下贝叶斯公式,接下来我们把问题细化:
假设案件B中包含个小案件(如,b1=丢了一个尿壶,b2=门被撬坏了等等),那么:

接下来我们可以把公式按照概率公式部分汇总中的第6个公式进行拆分:

朴素二字如何理解?假设这些小案件全部都是独立的,也就是说撬门和偷尿壶毫无关系时,我们继续根据概率公式部分汇总第7个公式继续把问题简化:

那么我们就可以把最原始的复杂问题变成了,统计出小偷Aj偷尿壶的概率,撬锁的概率,然后再把它们乘起来即可!这就是朴素贝叶斯。把一个复杂的问题化简成了一个个的可统计的独立概率,然后再乘起来。⚠️:但是前提是,这些小案件要尽量做到相互独立!

利用朴素贝叶斯的文本分析和关键词提取

Tf-idf:词频-逆词频
  1. 首先在文本中去掉匹配到的停用词
  2. Tf-idf=TF·IDF
如何判断语句的相似度
词向量相似度:
  1. 将文档分词。
  2. 将分好词去重,组装成语料库。
  3. 统计句子中的词在语料库中的词频。
  4. 利用词频构建词频向量。
n维空间下余弦相似度(值越小越相似):

利用朴素贝叶斯进行新闻分类

我们拿到了一批新闻数据,经过加工之后,变成了如下格式:

一批新闻数据

其中,category就是这批数据的标签,也就是说,这次我们的任务:根据content中描述的内容,预测是数据哪种category

  • 我们首先对content的每一个新闻进行分词,这里我们使用jieba分词器:
content_S = []
for line in content:
    current_segment = jieba.lcut(line)
    if len(current_segment) > 1 and current_segment != '\r\n': #换行符
        content_S.append(current_segment)

#将结果集转换为dataFrame
df_content=pd.DataFrame({'content_S':content_S})
print(df_content.head())

我们拿到了一个结果集df_content,每一条都是分好词的句子,样子如下:

                                           content_S
0  [经销商,  , 电话,  , 试驾, /, 订车, U, 憬, 杭州, 滨江区, 江陵, ...
1  [呼叫, 热线,  , 4, 0, 0, 8, -, 1, 0, 0, -, 3, 0, 0...
2  [M, I, N, I, 品牌, 在, 二月, 曾经, 公布, 了, 最新, 的, M, I...
3  [清仓, 大, 甩卖, !, 一汽, 夏利, N, 5, 、, 威志, V, 2, 低至, ...
4  [在, 今年, 3, 月, 的, 日内瓦, 车展, 上, ,, 我们, 见到, 了, 高尔夫...
  • 导入停用词表,随便在网上搜一搜就有了哈,将每条分好词的新闻中,过滤掉相应的停用词,以免影响我们的训练分析。
#读取停用词表 index_col=False新加入一列,作为行号,quoting=3是可以正确解读文本里的引号
stopwords=pd.read_csv("stopwords.txt",index_col=False,sep="\t",quoting=3,names=['stopword'], encoding='utf-8')


# 过滤掉停用词
def drop_stopwords(contents, stopwords):
    contents_clean = []
    all_words = []
    for line in contents:
        line_clean = []
        for word in line:
            if word in stopwords:
                continue
            line_clean.append(word)
            all_words.append(str(word))
        contents_clean.append(line_clean)
    return contents_clean, all_words


contents = df_content.content_S.values.tolist()
stopwords = stopwords.stopword.values.tolist()
contents_clean, all_words = drop_stopwords(contents, stopwords)
#干干净净的分词后的列表
df_content=pd.DataFrame({'contents_clean':contents_clean})
  • 细心的小伙伴可以看到,上述代码中,我们将全部采纳的词汇保存到了all_words变量中,而我们并未对其使用,那么接下来我们利用此变量进行词频统计,并且绘制词云图片。
#统计全词列表中的词频
result = pd.value_counts(all_words)
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['figure.figsize'] = (10.0, 5.0)
wordcloud = WordCloud(font_path="simhei.ttf", background_color="white", max_font_size=80,)
word_frequence = result.to_dict()
wordcloud = wordcloud.fit_words(word_frequence)
plt.imshow(wordcloud)
d = path.dirname(__file__)
wordcloud.to_file(path.join(d, 'alice_colored2.png'))
词云

我又用别的文章,单独玩了一下词云:


另一篇词频统计
  • 接下来,我们尝试对其中一条新闻进行关键词提取
#关键词提取
import jieba.analyse
#分析第二十条新闻
index = 20
print(df_news['content'][index])
content_S_str = "".join(content_S[index])
print("  ".join(jieba.analyse.extract_tags(content_S_str, topK=5, withWeight=False)))
  • 利用LDA(主题,文章,词)模型,对全体文章进行主题归类
from gensim import corpora, models, similarities
import gensim
#做映射,相当于词袋
dictionary = corpora.Dictionary(contents_clean)
corpus = [dictionary.doc2bow(sentence) for sentence in contents_clean] #将词映射成数字
lda = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=20) #20个大分类
for topic in lda.print_topics(num_topics=20, num_words=5):
    print(topic)

部分主题打印如下:

(0, '0.016*"a" + 0.015*"e" + 0.013*"i" + 0.012*"o" + 0.011*"n"')
(1, '0.010*"i" + 0.007*"e" + 0.007*"a" + 0.006*"中" + 0.005*"o"')
(2, '0.006*"选手" + 0.006*"万" + 0.006*"中国" + 0.006*"奢侈品" + 0.004*"申花"')
(3, '0.005*"中" + 0.005*"女人" + 0.004*"男人" + 0.003*"D" + 0.003*"说"')
(4, '0.005*"中" + 0.004*"肌肤" + 0.004*"时间" + 0.003*"吃" + 0.003*"水"')
(5, '0.006*"工作" + 0.006*"说" + 0.005*"学生" + 0.005*"发展" + 0.005*"文化"')
(6, '0.008*"中" + 0.005*"L" + 0.004*"电影" + 0.004*"D" + 0.004*"T"')
(7, '0.009*"中国" + 0.006*"撒" + 0.006*"电影" + 0.004*"产品" + 0.004*"中"')
  • 下面我们针对category对数据进行预测啦!
#组装格式,并把label映射成数字,切分训练集和测试集
df_train=pd.DataFrame({'contents_clean':contents_clean,'label':df_news['category']})
label_mapping = {"汽车": 1, "财经": 2, "科技": 3, "健康": 4, "体育":5, "教育": 6,"文化": 7,"军事": 8,"娱乐": 9,"时尚": 0}
df_train['label'] = df_train['label'].map(label_mapping)
x_train, x_test, y_train, y_test = train_test_split(df_train['contents_clean'].values, df_train['label'].values, random_state=1)
  • 此时我们拿到的x格式我list of list,而我们需要类似['a b c','d e f']的格式即将内部的list变成一个字符串:
#改变训练集数据格式,方便下一步的数据输入
words = []
for line_index in range(len(x_train)):
    try:
        words.append(' '.join(x_train[line_index]))
    except:
        print(line_index)
  • 构建词向量
#构建词向量
from sklearn.feature_extraction.text import CountVectorizer
vec = CountVectorizer(analyzer='word', max_features=4000,  lowercase = False)
vec.fit(words)
  • 导入朴素贝叶斯,训练分类器:
#贝叶斯训练
from sklearn.naive_bayes import MultinomialNB
classifier = MultinomialNB()
classifier.fit(vec.transform(words), y_train)
  • 同样的,我们需要改变测试集的输入格式:
#改变测试集数据格式,方便下一步的数据输入
test_words = []
for line_index in range(len(x_test)):
    try:
        #x_train[line_index][word_index] = str(x_train[line_index][word_index])
        test_words.append(' '.join(x_test[line_index]))
    except:
         print (line_index)
  • 预测
print(classifier.score(vec.transform(test_words), y_test))
  • 同样我们也可以使用Tf-idf的形式构建词向量:
#使用tf-idf构造向量
from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer(analyzer='word', max_features=4000,  lowercase = False)
vectorizer.fit(words)

classifier = MultinomialNB()
classifier.fit(vectorizer.transform(words), y_train)
print(classifier.score(vectorizer.transform(test_words), y_test))
  • 推荐阅读:
    概率公式部分汇总

完整代码

import pandas as pd
import jieba
import numpy
from os import path
from sklearn.model_selection import train_test_split


df_news = pd.read_table('data.txt',names=['category','theme','URL','content'],encoding='utf-8')
df_news = df_news.dropna()#去掉含有缺失值的行
print(df_news.head())

content = df_news.content.values.tolist(); # 把表格的一列提取出来搞成一个list
print(content[100])


#分词,把分词后的结果放到结果集content_S里
content_S = []
for line in content:
    current_segment = jieba.lcut(line)
    if len(current_segment) > 1 and current_segment != '\r\n': #换行符
        content_S.append(current_segment)

#将结果集转换为dataFrame
df_content=pd.DataFrame({'content_S':content_S})
print(df_content.head())

#读取停用词表 index_col=False新加入一列,作为行号,quoting=3是可以正确解读文本里的引号
stopwords=pd.read_csv("stopwords.txt",index_col=False,sep="\t",quoting=3,names=['stopword'], encoding='utf-8')


# 过滤掉停用词
def drop_stopwords(contents, stopwords):
    contents_clean = []
    all_words = []
    for line in contents:
        line_clean = []
        for word in line:
            if word in stopwords:
                continue
            line_clean.append(word)
            all_words.append(str(word))
        contents_clean.append(line_clean)
    return contents_clean, all_words


contents = df_content.content_S.values.tolist()
stopwords = stopwords.stopword.values.tolist()
contents_clean, all_words = drop_stopwords(contents, stopwords)

#干干净净的分词后的列表
df_content=pd.DataFrame({'contents_clean':contents_clean})

#全部词汇,也整一个全词列表
df_all_words=pd.DataFrame({'all_words':all_words})
#统计全词列表中的词频
result = pd.value_counts(all_words)
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import matplotlib
matplotlib.rcParams['figure.figsize'] = (10.0, 5.0)
wordcloud = WordCloud(font_path="simhei.ttf", background_color="white", max_font_size=80,)
word_frequence = result.to_dict()
wordcloud = wordcloud.fit_words(word_frequence)
plt.imshow(wordcloud)
d = path.dirname(__file__)
wordcloud.to_file(path.join(d, 'alice_colored2.png'))



#关键词提取
import jieba.analyse
index = 20
print(df_news['content'][index])
content_S_str = "".join(content_S[index])
print("  ".join(jieba.analyse.extract_tags(content_S_str, topK=5, withWeight=False)))


#LDA模型:主题,文章,词
from gensim import corpora, models, similarities
import gensim
#做映射,相当于词袋
dictionary = corpora.Dictionary(contents_clean)
corpus = [dictionary.doc2bow(sentence) for sentence in contents_clean] #将词映射成数字
lda = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=20) #20个大分类
for topic in lda.print_topics(num_topics=20, num_words=5):
    print(topic)





#组装格式,并把label映射成数字,切分训练集和测试集
df_train=pd.DataFrame({'contents_clean':contents_clean,'label':df_news['category']})
label_mapping = {"汽车": 1, "财经": 2, "科技": 3, "健康": 4, "体育":5, "教育": 6,"文化": 7,"军事": 8,"娱乐": 9,"时尚": 0}
df_train['label'] = df_train['label'].map(label_mapping)
x_train, x_test, y_train, y_test = train_test_split(df_train['contents_clean'].values, df_train['label'].values, random_state=1)

#改变训练集数据格式,方便下一步的数据输入
words = []
for line_index in range(len(x_train)):
    try:
        words.append(' '.join(x_train[line_index]))
    except:
        print(line_index)
print(words)

#构建词向量
from sklearn.feature_extraction.text import CountVectorizer
vec = CountVectorizer(analyzer='word', max_features=4000,  lowercase = False)
vec.fit(words)

#贝叶斯训练
from sklearn.naive_bayes import MultinomialNB
classifier = MultinomialNB()
classifier.fit(vec.transform(words), y_train)


#改变测试集数据格式,方便下一步的数据输入
test_words = []
for line_index in range(len(x_test)):
    try:
        #x_train[line_index][word_index] = str(x_train[line_index][word_index])
        test_words.append(' '.join(x_test[line_index]))
    except:
         print (line_index)

print(classifier.score(vec.transform(test_words), y_test))


#使用tf-idf构造向量
from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer(analyzer='word', max_features=4000,  lowercase = False)
vectorizer.fit(words)

classifier = MultinomialNB()
classifier.fit(vectorizer.transform(words), y_train)
print(classifier.score(vectorizer.transform(test_words), y_test))

你可能感兴趣的:(朴素贝叶斯概述)