import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn import tree
from sklearn.model_selection import train_test_split
from sklearn import neighbors
from sklearn.metrics import classification_report,confusion_matrix
from sklearn.naive_bayes import MultinomialNB,BernoulliNB,GaussianNB
def main():
print ("----------ing-------------")
iris_data = load_iris()
x_data= iris_data.data
y_data= iris_data.target
x_train,x_test,y_train,y_test=train_test_split(x_data,y_data)
x_ = (x_data[-1])#[5.9 3. 5.1 1.8]
x_ = x_[np.newaxis,:]#[[5.9 3. 5.1 1.8]]
y_ = (y_data[-1])
mul_nb = MultinomialNB()#贝叶斯模型
mul_nb.fit(x_train,y_train)
mul_nb_result=(classification_report(mul_nb.predict(x_test),y_test))
mul_nb_matrix=(confusion_matrix(mul_nb.predict(x_test),y_test))
print("mul_nb_result:\n",mul_nb_result)
print("mul_nb_matrix:\n",mul_nb_matrix)
mul_predict = mul_nb.predict(x_)
print("mul_predict result = {}".format(mul_predict))
ber_nb = BernoulliNB()#伯努利模型
ber_nb.fit(x_train,y_train)
ber_nb_result=(classification_report(ber_nb.predict(x_test),y_test))
ber_nb_matrix=(confusion_matrix(ber_nb.predict(x_test),y_test))
print("ber_nb_result:\n",ber_nb_result)
print("ber_nb_matrix:\n",ber_nb_matrix)
ber_predict = ber_nb.predict(x_)
print("ber_predict result = {}".format(ber_predict))
gau_nb = GaussianNB()#高斯模型
gau_nb.fit(x_train,y_train)
gau_nb_result=(classification_report(gau_nb.predict(x_test),y_test))
gau_nb_matrix=(confusion_matrix(gau_nb.predict(x_test),y_test))
print("gau_nb_result:\n",gau_nb_result)
print("gau_nb_matrix:\n",gau_nb_matrix)
gau_predict = gau_nb.predict(x_)
print("gau_predict result = {}".format(gau_predict))
print ("----------end-------------")
main()
贝叶斯模型(贝叶斯,高斯,伯努利)
最新推荐文章于 2023-10-26 20:42:00 发布