"""樸素貝葉斯算法的實現(xiàn)"""
"""2019/4/12"""
import numpy as np
import pandas as pd
class NaiveBayes():
def __init__(self,lambda_):
self.lambda_=lambda_ #貝葉斯系數(shù) 取0時,即為極大似然估計
self.y_types_count=None #y的(類型:數(shù)量)
self.y_types_proba=None #y的(類型:概率)
self.x_types_proba=dict() #(xi 的編號,xi的取值秉继,y的類型):概率
def fit(self,X_train,y_train):
self.y_types=np.unique(y_train) #y的所有取值類型
X=pd.DataFrame(X_train) #轉(zhuǎn)化成pandas DataFrame數(shù)據(jù)格式捣炬,下同
y=pd.DataFrame(y_train)
# y的(類型:數(shù)量)統(tǒng)計
self.y_types_count=y[0].value_counts()
# y的(類型:概率)計算
self.y_types_proba=(self.y_types_count+self.lambda_)/(y.shape[0]+len(self.y_types)*self.lambda_)
# (xi 的編號,xi的取值而钞,y的類型):概率的計算
for idx in X.columns: # 遍歷xi
for j in self.y_types: # 選取每一個y的類型
p_x_y=X[(y==j).values][idx].value_counts() #選擇所有y==j為真的數(shù)據(jù)點的第idx個特征的值,并對這些值進行(類型:數(shù)量)統(tǒng)計
for i in p_x_y.index: #計算(xi 的編號,xi的取值扶叉,y的類型):概率
self.x_types_proba[(idx,i,j)]=(p_x_y[i]+self.lambda_)/(self.y_types_count[j]+p_x_y.shape[0]*self.lambda_)
def predict(self,X_new):
res=[]
for y in self.y_types: #遍歷y的可能取值
p_y=self.y_types_proba[y] #計算y的先驗概率P(Y=ck)
p_xy=1
for idx,x in enumerate(X_new):
p_xy*=self.x_types_proba[(idx,x,y)] #計算P(X=(x1,x2...xd)/Y=ck)
res.append(p_y*p_xy)
for i in range(len(self.y_types)):
print("[{}]對應(yīng)概率:{:.2%}".format(self.y_types[i],res[i]))
#返回最大后驗概率對應(yīng)的y值
return self.y_types[np.argmax(res)]
def main():
X_train=np.array([
[1,"S"],
[1,"M"],
[1,"M"],
[1,"S"],
[1,"S"],
[2,"S"],
[2,"M"],
[2,"M"],
[2,"L"],
[2,"L"],
[3,"L"],
[3,"M"],
[3,"M"],
[3,"L"],
[3,"L"]
])
y_train=np.array([-1,-1,1,1,-1,-1,-1,1,1,1,1,1,1,1,-1])
clf=NaiveBayes(lambda_=0)
clf.fit(X_train,y_train)
X_new=np.array([2,"S"])
y_predict=clf.predict(X_new)
print("{}被分類為:{}".format(X_new,y_predict))
if __name__=="__main__":
main()
樸素貝葉斯法通過訓(xùn)練數(shù)據(jù)集學(xué)習(xí)聯(lián)合概率分布P(X,Y),具體做法是學(xué)習(xí)先驗概率分布P(Y)與條件概率分布P(X|Y)(二者相乘就是聯(lián)合概率分布)帕膜,所以它屬于生成模型枣氧。
image.png
image.png
# load the iris dataset
from sklearn.datasets import load_iris
iris = load_iris()
# store the feature matrix (X) and response vector (y)
X = iris.data
y = iris.target
# splitting X and y into training and testing sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=1)
# training the model on training set
from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
gnb.fit(X_train, y_train)
# making predictions on the testing set
y_pred = gnb.predict(X_test)
# comparing actual response values (y_test) with predicted response values (y_pred)
from sklearn import metrics
print("Gaussian Naive Bayes model accuracy(in %):", metrics.accuracy_score(y_test, y_pred)*100)
image.png
image.png
后驗概率最大化
image.png
image.png
image.png