#1.使用朴素贝叶斯模型对iris数据集进行花分类
#尝试使用3种不同类型的朴素贝叶斯:
#高斯分布型,多项式型,伯努利型
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=gnb.predict(iris.data) print(iris.data.shape[0],(iris.target != y_pred).sum())
150 6
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=gnb.predict(iris.data) print(iris.data.shape[0],(iris.target != y_pred).sum())
150 100
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=gnb.predict(iris.data) print(iris.data.shape[0],(iris.target != y_pred).sum())
150 7
#2.使用sklearn.model_selection.cross_val_score(),对模型进行验证。
#检测模型的好坏BernoulliNB from sklearn.naive_bayes import BernoulliNB from sklearn.model_selection import cross_val_score gnb = BernoulliNB() scores=cross_val_score(gnb,iris.data,iris.target,cv=10) print("Accuray:%.3f"%scores.mean())
Accuray:0.333
from sklearn.naive_bayes import MultinomialNB from sklearn.model_selection import cross_val_score gnb = MultinomialNB() scores=cross_val_score(gnb,iris.data,iris.target,cv=10) print("Accuray:%.3f"%scores.mean())
Accuray:0.953
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("Accuray:%.3f"%scores.mean())
Accuray:0.953