数据挖掘-Iris数据集分析-决策边界(六)

来源:互联网 发布:c盘中windows.old 编辑:程序博客网 时间:2024/05/22 05:06
# coding: utf-8  # 使用萼片测量数据绘制 2D散点图,并绘出决策边界import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.colors import ListedColormapfrom sklearn import datasetsfrom sklearn.neighbors import KNeighborsClassifier#matplot显示图例中的中文问题 :   https://www.zhihu.com/question/25404709/answer/67672003import matplotlib.font_manager as fm#mac中的字体问题请看: https://zhidao.baidu.com/question/161361596.htmlmyfont = fm.FontProperties(fname='/Library/Fonts/Xingkai.ttc')iris=datasets.load_iris()x=iris.data[:,:2]   #取出萼片的长和宽 y=iris.target       #取出类别#计算散点图的轴的边界x_min,x_max=x[:,0].min() -.5, x[:,0].max()+.5y_min, y_max=x[:,1].min()-.5, x[:,1].max()+.5#绘制边界cmap_light=ListedColormap(['#AAAAFF','#AAFFAA','#FFAAAA'])h=.02xx,yy=np.meshgrid(np.arange(x_min,x_max,h),np.arange(y_min,y_max,h))knn=KNeighborsClassifier()knn.fit(x,y)Z=knn.predict( np.c_[xx.ravel(),yy.ravel()])Z=Z.reshape(xx.shape)plt.figure()plt.pcolormesh(xx,yy,Z,cmap=cmap_light)plt.title(u'鸢尾花分类预测决策边界_根据萼片长宽',fontproperties=myfont)plt.xlabel(u'萼片长',fontproperties=myfont)plt.ylabel(u'萼片宽',fontproperties=myfont)plt.scatter(x[:,0],x[:,1],c=y)plt.xlim( xx.min(), xx.max() )plt.ylim( yy.min(),yy.max() )plt.savefig('python_8_6_带决策边界的2D散点图_根据萼片数据绘制.png')plt.show()

0 0
原创粉丝点击