2.Iris数据集:感知器模型的简单实战(分类)
来源:互联网 发布:python appium 编辑:程序博客网 时间:2024/06/11 23:00
一、程序
from sklearn import datasetsimport numpy as npiris=datasets.load_iris()X=iris.data[:,[2,3]]#2:3才不算3y=iris.targeta=np.unique(y)#返回类标值#交叉验证from sklearn.cross_validation import train_test_splitX_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3,random_state=0)#数据标准化from sklearn.preprocessing import StandardScalersc=StandardScaler()sc.fit(X_train)#计算得到标准化要的u,方差X_train_std=sc.transform(X_train)#通过前面计算得到的参数对训练集、测试集分别进行处理X_test_std=sc.transform(X_test)#利用训练集进行建模from sklearn.linear_model import Perceptronppn=Perceptron(n_iter=40,eta0=0.1,random_state=0)ppn.fit(X_train_std,y_train)#在测试集上运用predict方法进行预测y_pred=ppn.predict(X_test_std)print('Misclassified samples:%d' %(y_test!=y_pred).sum())#计算分类准确率from sklearn.metrics import accuracy_scoreprint('Accuracy:%.2f' % accuracy_score(y_test,y_pred))#绘图from matplotlib.colors import ListedColormapimport matplotlib.pyplot as pltdef plot_decison_regions(X,y,classifier,test_idx=None,resolution=0.02): #设置标记生成器和颜色映射 markers=('s','x','o','^','v') colors=('red','blue','lightgreen','gray','cyan')#一维 cmap=ListedColormap(colors[:len(np.unique(y))]) #plot the decision surface x1_min,x1_max=X[:,0].min()-1,X[:,0].max()+1 x2_min,x2_max=X[:,1].min()-1,X[:,0].max()+1 xx1,xx2=np.meshgrid(np.arange(x1_min,x1_max,resolution),np.arange(x2_min,x2_max,resolution)) Z=classifier.predict(np.array([xx1.ravel(),xx2.ravel()]).T) Z=Z.reshape(xx1.shape) plt.contourf(xx1,xx2,Z,alpha=0.4,cmap=cmap) plt.xlim(xx1.min(),xx1.max()) plt.ylim(xx2.min(),xx2.max()) #plot all samples X_test,y_test=X[test_idx,:],y[test_idx] for idx,c1 in enumerate(np.unique(y)): plt.scatter(x=X[y==c1,0],y=X[y==c1,1],alpha=0.8,c=cmap(idx),marker=markers[idx],label=c1) #用于区分哪些是测试集的结果 if test_idx: X_test,y_test=X[test_idx,:],y[test_idx] plt.scatter(X_test[:,0],X_test[:,1],c='',alpha=1.0,linewidth=1,marker='o',s=55,label='test set') #呈现图像X_combined_std=np.vstack((X_train_std,X_test_std))y_combined=np.hstack((y_train,y_test))plot_decison_regions(X=X_combined_std,y=y_combined,classifier=ppn,test_idx=range(105,150))plt.xlabel('petal length [standardized]')plt.ylabel('petal length [standardized]')plt.legend(loc='upper left')plt.show()
二、展示结果
可以看出,45个测试集中有4个分类错误。
附录:
np.meshgrid函数与ravel函数
meshgrid的作用适用于生成网格型数据,可以接受两个一维数组生成两个二维矩阵,对应两个数组中所有的(x,y)对。接下来通过简单的shell交互来演示一下这个功能的使用,并做一下小结。
In [65]: xnums =np.arange(4) In [66]: ynums =np.arange(5) In [67]: xnumsOut[67]: array([0,1, 2, 3]) In [68]: ynumsOut[68]: array([0,1, 2, 3, 4]) In [69]: data_list= np.meshgrid(xnums,ynums) In [70]: data_listOut[70]:[array([[0, 1, 2,3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]), array([[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], [4, 4, 4, 4]])] In [71]: x,y =data_listIn [72]: x.shapeOut[72]: (5L, 4L) In [73]: y.shapeOut[73]: (5L, 4L) In [74]: xOut[74]:array([[0, 1, 2,3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]) In [75]: yOut[75]:array([[0, 0, 0,0], [1, 1, 1, 1], [2, 2, 2, 2],#如果第一个参数是xarray,维度是xdimesion,第二个参数是yarray,维度是ydimesion。 [3, 3, 3, 3],# 那么生成的第一个二维数组是以xarray为行,ydimesion行的向量; [4, 4, 4, 4]])# 而第二个二维数组是以yarray的转置为列,xdimesion列的向量。In [13]: x.ravel()Out[13]: array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3])In [14]: y.ravel()Out[14]: array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4])
阅读全文
0 0
- 2.Iris数据集:感知器模型的简单实战(分类)
- Scikit-Learn 实战 iris数据集分类
- Spark 机器学习实践 :Iris数据集的分类
- 85、使用TFLearn实现iris数据集的分类
- 大数据入门——Iris数据集类别预测(K近邻分类器:KNN)
- python 实现 knn分类算法 (Iris 数据集)
- 利用BP神经网络分类iris数据集
- iris数据集进行KNN分类
- matlab实现简单的感知器分类
- 基于简单感知器的分类
- iris数据集的可视化
- 数据挖掘-K-近邻分类器-Iris数据集分析-使用K-近邻分类器进行分类预测(四)
- 感知器数据分类算法
- 机器学习练习(一)——简单的Iris分类练习
- Caffe 之 使用非图片的鸢尾花(IRIS)数据集(hdf5格式) 训练网络模型
- 线性分类器之感知器模型(Perceptron)
- 使用不同的SVM对iris数据集进行分类并绘出结果
- 用深度神经网络对Iris数据集进行分类的程序--tensorflow
- CentOS 6上编译安装LAMP
- SQL字段的被包含查询
- CodeForces 197D 搜索
- Hadoop完全分布式环境配置及 Word Count 程序运行
- inflate()引发NullPointerException
- 2.Iris数据集:感知器模型的简单实战(分类)
- 搭建 Seafile 专属网盘
- WCF基本实现(Server和Client)
- SIFT特征提取分析
- 上手上路之 js 数据类型 number和parseInt..的代码笔记
- (82)IO概述、字符流写入、异常处理、续写、两种读取方式、读写练习
- scrapy爬虫返回403
- 【转】阿里巴巴 Java 开发手册 (第一部分:编程规约)
- 初学者的Selenium自动化测试指南,基于Python(六)——如何设置元素等待