首页 > 分享 > 2.Iris数据集:感知器模型的简单实战(分类)

2.Iris数据集:感知器模型的简单实战(分类)

一、程序

from sklearn import datasets

import numpy as np

iris=datasets.load_iris()

X=iris.data[:,[2,3]]

y=iris.target

a=np.unique(y)

from sklearn.cross_validation import train_test_split

X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3,random_state=0)

from sklearn.preprocessing import StandardScaler

sc=StandardScaler()

sc.fit(X_train)

X_train_std=sc.transform(X_train)

X_test_std=sc.transform(X_test)

from sklearn.linear_model import Perceptron

ppn=Perceptron(n_iter=40,eta0=0.1,random_state=0)

ppn.fit(X_train_std,y_train)

y_pred=ppn.predict(X_test_std)

print('Misclassified samples:%d' %(y_test!=y_pred).sum())

from sklearn.metrics import accuracy_score

print('Accuracy:%.2f' % accuracy_score(y_test,y_pred))

from matplotlib.colors import ListedColormap

import matplotlib.pyplot as plt

def 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))])

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())

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]: xnums

Out[67]: array([0,1, 2, 3])

In [68]: ynums

Out[68]: array([0,1, 2, 3, 4])

In [69]: data_list= np.meshgrid(xnums,ynums)

In [70]: data_list

Out[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_list

In [72]: x.shape

Out[72]: (5L, 4L)

In [73]: y.shape

Out[73]: (5L, 4L)

In [74]: x

Out[74]:

array([[0, 1, 2,3],

[0, 1, 2, 3],

[0, 1, 2, 3],

[0, 1, 2, 3],

[0, 1, 2, 3]])

In [75]: y

Out[75]:

array([[0, 0, 0,0],

[1, 1, 1, 1],

[2, 2, 2, 2],

[3, 3, 3, 3],

[4, 4, 4, 4]])

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])

'

相关知识

使用新颖的基于离散学习的多层感知器模型 (DMLP) 增强空气质量分类,International Journal of Environmental Science and Technology
深度学习实战(1):花的分类任务|附数据集与源码
基于多尺度数据集的虫害检测模型
TensorFlow 2建立神经网络分类模型——以iris数据为例
pytorch——AlexNet——训练花分类数据集
【深度学习】 图像识别实战 102鲜花分类(flower 102)实战案例
基于YOLOv8深度学习的水稻害虫检测与识别系统【python源码+Pyqt5界面+数据集+训练代码】目标检测、深度学习实战
细粒度分类常用数据集介绍及下载
基于YOLOv8深度学习的102种花卉智能识别系统【python源码+Pyqt5界面+数据集+训练代码】目标识别、深度学习实战
flower花朵识别数据集

网址: 2.Iris数据集:感知器模型的简单实战(分类) https://m.huajiangbk.com/newsview164670.html

所属分类:花卉
上一篇: 大众花卉消费习惯与需求变化——2
下一篇: 蝴蝶花...阅读答案 现代文阅读