朴素贝叶斯预测莺尾花数据
import numpy as np import matplotlib.pyplot as plt from sklearn import datasets from sklearn.naive_bayes import GaussianNB import matplotlib #生成所有测试样本点 def make_meshgrid(x,y,h=0.02): x_min,x_max = x.min()-1,x.max()+1 y_min, y_max = y.min() - 1, y.max() + 1 xx,yy = np.meshgrid(np.arange(x_min,x_max,h),np.arange(y_min,y_max,h)) return xx,yy def plot_test_results(ax,clf,xx,yy,**params): Z = clf.predict(np.c_[xx.ravel(),yy.ravel()]) Z = Z.reshape(xx.shape) ax.contourf(xx,yy,Z, **params) if __name__ == '__main__': #载入iris数据集 iris = datasets.load_iris() #只使用前面两个特征 X = iris.data[:,:2] #样本标签值 y = iris