樸素貝葉斯分類演算法的Python實現
阿新 • • 發佈:2019-01-04
貝葉斯分類演算法以貝葉斯定理為基礎,通過先驗概率計算後驗概率,再由最大厚顏概率決定分類(同學們還是當沒看到過這句話吧)。
數學學得不咋地,就多說了,實驗報告怎麼寫也是頭疼,就直接貼程式碼了。
# 訓練例項
dataset = [['Sunny', 'Hot', 'High', 'Weak', 'no'],
['Sunny', 'Hot', 'High', 'Strong', 'no'],
['Overcast', 'Hot', 'High', 'Weak', 'yes'],
['Rainy', 'Mild', 'High', 'Weak', 'yes'],
['Rainy', 'Cool' , 'Normal', 'Weak', 'yes'],
['Rainy', 'Cool', 'Normal', 'Strong', 'no'],
['Overcast', 'Cool', 'Normal', 'Strong', 'yes'],
['Sunny', 'Mild', 'High', 'Weak', 'no'],
['Sunny', 'Cool', 'Normal', 'Weak', 'yes'],
['Rainy', 'Mild', 'Normal', 'Weak', 'yes'],
['Sunny', 'Mild', 'Normal', 'Strong', 'yes'],
['Overcast' , 'Mild', 'High', 'Strong', 'yes'],
['Overcast', 'Hot', 'Normal', 'Weak', 'yes'],
['Rainy', 'Mild', 'High', 'Strong', 'no']]
# 測試例項
attributes = ('Sunny','Cool','High','Strong')
def classify(data, attrIndex):
classes = {}
for item in data:
try:
classes[item[attrIndex]].append(item)
except :
classes[item[attrIndex]] = [item]
return classes
def p(data, index, key):
count = 0
for item in data:
if item[index] == key:
count += 1
return 1.0 * count / len(data)
def max(p):
keys = list(p.keys())
maxKey = keys[0]
for key in keys:
if p[key] > p[maxKey]:
maxKey = key
return maxKey
classes = classify(dataset, 4)
p0 = []
p1 = {}
for key in classes.keys():
p1[key] = 1.0 * len(classes[key]) / len(dataset)
p0.append(p1)
for index in range(0, len(attributes)):
p1 = {}
for key in classes.keys():
p1[key] = p(classes[key], index, attributes[index])
print (key, attributes[index], p1[key])
p0.append(p1)
p1 = {}
for key in classes.keys():
P = 1.0;
for item in p0:
P *= item[key]
p1[key] = P
print (key,'\t', P)
print (max(p1))