贝叶斯分类python实现

贝叶斯分类

weather = ['sunny', 'sunny', 'cloudy', 'rainy', 'rainy', 'rainy', 'cloudy',
           'sunny', 'sunny', 'rainy', 'sunny', 'cloudy', 'cloudy', 'rainy']
temperature = ['high', 'high', 'high', 'moderate', 'cold', 'cold', 'cold',
             'moderate', 'cold', 'moderate', 'moderate', 'moderate', 'high', 'moderate']
humidity = ['high', 'high', 'high', 'high', 'moderate', 'moderate', 'moderate',
           'high', 'moderate', 'moderate', 'moderate', 'high', 'moderate', 'high']
wind_force = ['windless', 'windy', 'windless', 'windless', 'windless', 'windy', 'windy',
             'windless', 'windless', 'windless', 'windy', 'windy', 'windless', 'windy']
action = ['no', 'no', 'yes', 'yes', 'yes', 'no', 'yes',
         'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'no']
len_action = len(weather)
def pos_neg_count():
    count_1 = 0
    count_2 = 0
    for params in action:
        if params == 'yes':
            count_1 += 1
        elif params == 'no':
            count_2 += 1
    return count_1, count_2

def weather_count():
    count_1 = 0
    count_2 = 0
    count_3 = 0
    count_4 = 0
    count_5 = 0
    count_6 = 0
    for weather_p, action_p in zip(weather, action):
        if weather_p == "sunny" and action_p == "yes":
            count_1 += 1
        elif weather_p == "sunny" and action_p == "no":
            count_2 += 1
        elif weather_p == "cloudy" and action_p == "yes":
            count_3 += 1
        elif weather_p == "cloudy" and action_p == "no":
            count_4 += 1
        elif weather_p == "rainy" and action_p == "yes":
            count_5 += 1
        elif weather_p == "rainy" and action_p == "no":
            count_6 += 1
    return count_1, count_2, count_3, count_4, count_5, count_6

def temp_count():
    count_1 = 0
    count_2 = 0
    count_3 = 0
    count_4 = 0
    count_5 = 0
    count_6 = 0
    for temp_p, action_p in zip(temperature, action):
        if temp_p == "high" and action_p == "yes":
            count_1 += 1
        elif temp_p == "high" and action_p == "no":
            count_2 += 1
        elif temp_p == "moderate" and action_p == "yes":
            count_3 += 1
        elif temp_p == "moderate" and action_p == "no":
            count_4 += 1
        elif temp_p == "cold" and action_p == "yes":
            count_5 += 1
        elif temp_p == "cold" and action_p == "no":
            count_6 += 1
    return count_1, count_2, count_3, count_4, count_5, count_6

def humidity_count():
    count_1 = 0
    count_2 = 0
    count_3 = 0
    count_4 = 0
    
    for humidity_p, action_p in zip(humidity, action):
        if humidity_p == "high" and action_p == "yes":
            count_1 += 1
        elif humidity_p == "high" and action_p == "no":
            count_2 += 1
        elif humidity_p == "moderate" and action_p == "yes":
            count_3 += 1
        elif humidity_p == "moderate" and action_p == "no":
            count_4 += 1
    return count_1, count_2, count_3, count_4

def wind_force_count():
    count_1 = 0
    count_2 = 0
    count_3 = 0
    count_4 = 0
    
    for wind_p, action_p in zip(wind_force, action):
        if wind_p == "windless" and action_p == "yes":
            count_1 += 1
        elif wind_p == "windless" and action_p == "no":
            count_2 += 1
        elif wind_p == "windy" and action_p == "yes":
            count_3 += 1
        elif wind_p == "windy" and action_p == "no":
            count_4 += 1
    return count_1, count_2, count_3, count_4

def test_count_function():
    pos_neg_count()
    weather_count()
    temp_count()
    humidity_count()
    wind_force_count()

def weather_probability():
    '''calculate weather probability'''
    weather_probability = []
    pos, neg = pos_neg_count()
    count = 0
    for i in weather_count():
        count += 1
        if count%2 != 0:
            weather_probability.append(i/pos)
        else:
            weather_probability.append(i/neg)
    return weather_probability

def temp_probability():
    '''calculate temperature probability'''
    temp_probability = []
    pos, neg = pos_neg_count()
    count = 0
    for i in temp_count():
        count += 1
        if count%2 != 0:
            temp_probability.append(i/pos)
        else:
            temp_probability.append(i/neg)
    return temp_probability

def humidity_probability():
    '''calculate humidity probability'''
    humidity_probability = []
    pos, neg = pos_neg_count()
    count = 0
    for i in humidity_count():
        count += 1
        if count%2 != 0:
            humidity_probability.append(i/pos)
        else:
            humidity_probability.append(i/neg)
    return humidity_probability

        
def wind_force_probability():
    '''calculate humidity probability'''
    wind_force_probability = []
    pos, neg = pos_neg_count()
    count = 0
    for i in wind_force_count():
        count += 1
        if count%2 != 0:
            wind_force_probability.append(i/pos)
        else:
            wind_force_probability.append(i/neg)
    return wind_force_probability
def pos_neg_probability():
    pos_neg_probability = []
    data_num = len(weather)
    pos, neg = pos_neg_count()
    pos_neg_probability.append(pos/data_num)
    pos_neg_probability.append(neg/data_num)
    return pos_neg_probability

def play_probability(weather, temp, humidity, wind_force):
    play_probability = 0
    pos_probability = pos_neg_probability()[0]
    
    if weather == "sunny":
        weather_pos_p =  weather_probability()[0]
    elif weather == "cloudy":
        weather_pos_p =  weather_probability()[2]
    else:
        weather_pos_p =  weather_probability()[4]
    
    if temp == "high":
        temp_pos_p = temp_probability()[0]
    elif temp == "moderate":
        temp_pos_p = temp_probability()[2]
    else:
        temp_pos_p = temp_probability()[4]
        
    if humidity == "high":
        humidity_pos_p = humidity_probability()[0]
    else: 
        humidity_pos_p = humidity_probability()[2]
    
    if wind_force == "windless":
        wind_pos_p = wind_force_probability()[0]
    else:
        wind_pos_p = wind_force_probability()[2]
    play_probability = weather_pos_p * temp_pos_p * humidity_pos_p * wind_pos_p * pos_probability
    return play_probability

 
def no_play_probability(weather, temp, humidity, wind_force):
    play_probability = 0
    pos_probability = pos_neg_probability()[1]
    
    if weather == "sunny":
        weather_pos_p =  weather_probability()[1]
    elif weather == "cloudy":
        weather_pos_p =  weather_probability()[3]
    else:
        weather_pos_p =  weather_probability()[5]
    
    if temp == "high":
        temp_pos_p = temp_probability()[1]
    elif temp == "moderate":
        temp_pos_p = temp_probability()[3]
    else:
        temp_pos_p = temp_probability()[5]
        
    if humidity == "high":
        humidity_pos_p = humidity_probability()[1]
    else: 
        humidity_pos_p = humidity_probability()[3]
    
    if wind_force == "windless":
        wind_pos_p = wind_force_probability()[1]
    else:
        wind_pos_p = wind_force_probability()[3]
    no_play_probability = weather_pos_p * temp_pos_p * humidity_pos_p * wind_pos_p * pos_probability
    return no_play_probability
    
def play_or_not(weather, temp, humidity, wind_force):
    play = play_probability(weather, temp, humidity, wind_force)
    no_play = no_play_probability(weather, temp, humidity, wind_force)
    play_percentage = (play/(play+no_play)) * 100
    no_play_percentage = (no_play/(play+no_play)) * 100
    if play_percentage > no_play_percentage:
        print("大胆去玩吧,少年!\n")
        print("AI观测:玩指数:{:.2f}%, 不玩指数: {:.2f}%".format(play_percentage, no_play_percentage))
        
    else:
        print("老实呆着吧,骚年!")
        print("AI观测:玩指数:{:.2f}%, 不玩指数: {:.2f}%".format(play_percentage, no_play_percentage))
    
if __name__ == "__main__":
    play_or_not("sunny", "cold", "high", "windy")
  • Result
老实呆着吧,骚年!
AI观测:玩指数: 20.46%, 不玩指数: 79.54%

【参考文献】
[1]https://wenku.baidu.com/view/05d0e30e856a561253d36fdb.html?from=search

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天然玩家

坚持才能做到极致

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值