樸素貝葉斯大家都知道了,我也就不贅述了,可參考
http://scikit-learn.org/stable/modules/naive_bayes.html
- 如何使用
天氣 | 打球 |
晴天 | no |
多雲 | yes |
雨天 | yes |
晴天 | yes |
晴天 | yes |
多雲 | yes |
雨天 | no |
雨天 | no |
晴天 | yes |
雨天 | yes |
晴天 | no |
多雲 | yes |
多雲 | yes |
雨天 | no |
- 將數據集轉換爲頻率表:
天氣 | 不打球 | 打球 |
多雲 | 4 | |
雨天 | 3 | 2 |
晴天 | 2 | 3 |
總數 | 5 | 9 |
天氣 | 不打球 | 打球 | |
多雲 | 4 | 0.29 | |
雨天 | 3 | 2 | 0.36 |
晴天 | 2 | 3 | 0.36 |
總數 | 5 | 9 | |
0.36 | 0.64 |
- 應用
- 採用python構建基本模型(採用sklearn包)
- python案例
#Import Library of Gaussian Naive Bayes model
from sklearn.naive_bayes import GaussianNB
import numpy as np
#assigning predictor and target variables
x = np.array([[-3,7],[1,5], [1,2], [-2,0], [2,3], [-4,0], [-1,1], [1,1], [-2,2], [2,7], [-4,1], [-2,7]])
y = np.array([3, 3, 3, 3, 4, 3, 3, 4, 3, 4, 4, 4])
構造模型:#Create a Gaussian Classifier
model = GaussianNB()
# Train the model using the training sets
model.fit(x, y)
#Predict Output
predicted= model.predict([[1,2],[3,4]])
print predicted
輸出爲預測結果:[3, 4]
- 提升貝葉斯表現