数据分析之贝叶斯算法案例

1.贝叶斯定理

       是一个经典的条件概率定理,其在机器学习中主要用来通过结果推算出原因产生的概率。P(A/B)*P(B)=P(B/A)*P(A)

2.字符串分类案例

#案例:随机输入一个字符串,判定其最可能属于哪个类别?
#若计算P(cat/str)=P(cat)*P(str/cat)/P(str)
#由于P(str)概率相同,因此公式可以简化为:P(cat/str)=P(cat)*P(str/cat)
cat1=["a","b","c","d","e","j"]
cat2=["a","d","o","h","e"]
cat3=["a","b","l","e","h","f"]
a="abcd"

def predict(str1):

    cat=[cat1,cat2,cat3]
    p={0:0,1:0,2:0}
    p1=[len(cat1)/26,len(cat2)/26,len(cat3)/26]#26个字母中出现的概率


    for i in  str1:
        for j in range(len(cat)):
            if i in cat[j]:
                p[j]+=1/len(cat[j])*p1[j]   #在cat1中字符串产生的概率,
    return sorted(p.items(),key= lambda p:p[1],reverse=True )



if __name__ == '__main__':
    print(predict(a)[0])

 3.判断单词属于好评的简单案例

from collections import  Counter
import numpy as np
class Bayers():
    def __init__(self):
        self.good = [['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'],
                     ['my', 'dalmation', 'is', 'so', 'cute', 'I', 'love', 'him'],
                     ['mr', 'licks', 'ate', 'my', 'steak', 'how', 'to', 'stop', 'him'],
                     ]
        self.bad = [['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid'],
                       ['stop', 'posting', 'stupid', 'worthless', 'garbage'],
                       ['quit', 'buying', 'worthless', 'dog', 'food', 'stupid']
                       ]
        self.good_counter=Counter([word for words in self.good for word in words])
        self.bad_counter = Counter([word for words in self.bad for word in words])
        self.good_chance=len(self.good)/len(self.good+self.bad)
        self.bad_chance = 1-self.good_chance


    def predict(self, data):
        """
        统计单词在好或坏中出现的概率,为避免单词出现概率为0,我们分子加1,分母加上查询单词的长度,进行修正

        :param data:
        :return:
        """
        p_good,p_bad=0,0
        for word in data:
            p_good+= (self.good_counter.get(word,0)+1)/(len(data)+sum(self.good_counter.values()))*self.good_chance
            p_bad += (self.bad_counter.get(word, 0) + 1) / (
            len(data) + sum(self.bad_counter.values())) * self.bad_chance
        if p_good>p_bad:
            return True
        return  False

if __name__ == '__main__':
    bayer=Bayers()
    print(bayer.predict(['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid']))
    

 

posted @ 2019-10-08 11:37  fjc0000  阅读(1030)  评论(0编辑  收藏  举报