zoukankan      html  css  js  c++  java
  • 数据分析之贝叶斯算法案例

    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']))
        
  • 相关阅读:
    php 判断字符串是否以某字符串结尾的方法
    且看新手如何快速新站优化(经验+求指导+找喷) 西安
    xml 序列化和反序列化 西安
    反射+xml 序列化 实际应用 西安
    使用自定义文件缓存提升ASP.NET项目性能 西安
    反射的简单应用(记录) 西安
    java web 中的 controller、service、persistence(mapper)、domain 分别是什么作用?
    nodesass 替换为 sass
    github 访问不了 访问 github,修改 hosts 文件方法
    win10 如何使用管理员权限编辑文件
  • 原文地址:https://www.cnblogs.com/xuehaiwuya0000/p/11634507.html
Copyright © 2011-2022 走看看