贝叶斯分类
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