标签:使用 mod .com sel val python print ima 分享
1.使用朴素贝叶斯模型对iris数据集进行花分类
尝试使用3种不同类型的朴素贝叶斯:
高斯分布型
多项式型
伯努利型
from sklearn import datasets iris = datasets.load_iris() ##高斯分布型 from sklearn import datasets iris = datasets.load_iris() from sklearn.naive_bayes import GaussianNB clf = GaussianNB() #构造 pred = clf.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 clf = BernoulliNB() #构造 pred = clf.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 clf = MultinomialNB() #构造 pred = clf.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(),对模型进行验证。
##使用sklearn.model_selection.cross_val_score()对高斯分布型进行验证 from sklearn.naive_bayes import GaussianNB from sklearn.model_selection import cross_val_score clf = GaussianNB() scores = cross_val_score(clf,iris.data,iris.target,cv=10) print("Accuracy:%.3f"%scores.mean()) ##使用sklearn.model_selection.cross_val_score()对伯努利型进行验证 from sklearn.naive_bayes import BernoulliNB from sklearn.model_selection import cross_val_score clf= BernoulliNB() scores = cross_val_score(clf,iris.data,iris.target,cv=10) print("Accuracy:%.3f"%scores.mean()) ##使用sklearn.model_selection.cross_val_score()对多项式型进行验证 from sklearn.naive_bayes import BernoulliNB from sklearn.model_selection import cross_val_score clf= MultinomialNB() scores = cross_val_score(clf,iris.data,iris.target,cv=10) print("Accuracy:%.3f"%scores.mean())
标签:使用 mod .com sel val python print ima 分享
原文地址:https://www.cnblogs.com/1158Z/p/10019398.html