高斯分布型
from sklearn import datasets
iris= datasets.load_iris()
from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
pred = gnb.fit(iris.data,iris.target)
y_pred = pred.predict(iris.data)
print(iris.data.shape[0],(iris.target!=y_pred).sum())
多项式型
from sklearn import datasets
iris= datasets.load_iris()
from sklearn.naive_bayes import MultinomialNB
gnb = MultinomialNB()
pred = gnb.fit(iris.data,iris.target)
y_pred = pred.predict(iris.data)
print(iris.data.shape[0],(iris.target!=y_pred).sum())
伯努利型
from sklearn import datasets
iris= datasets.load_iris()
from sklearn.naive_bayes import BernoulliNB
gnb = BernoulliNB()
pred = gnb.fit(iris.data,iris.target)
y_pred = pred.predict(iris.data)
print(iris.data.shape[0],(iris.target!=y_pred).sum())
2.使用sklearn.model_selection.cross_val_score(),对模型进行验证。
from sklearn.naive_bayes import GaussianNB
from sklearn.model_selection import cross_val_score
gnb = GaussianNB()
scores = cross_val_score(gnb,iris.data,iris.target,cv=10)
print("Accuracy:%.3f"%scores.mean())