RBF神经网络对iris鸢尾花数据集进行分类识别

RBF神经网络对iris鸢尾花数据集进行分类

http://blog.csdn.net/fubin0000

设计要求:

       iris以鸢尾花的特征作为数据来源,数据集包含150个数据集,分为3类(setosa,versicolor, virginica),每类50个数据,每个数据包含4个属性。每一个数据包含4个独立的属性,这些属性变量测量植物的花朵(比如萼片和花瓣的长度等)信息。要求以iris数据为对象,来进行不可测信息(样本类别)的估计。以每一类前30个数据作为学习样本,以后20个样本作为测试样本,对样本进行估计,并和实际结果作比较,使用了RBF神经网络。

一、      RBF神经网络

       Radialbasis function(径向基函数),径向基函数是一个取值仅仅依赖于离原点距离的实值函数,也就是Φ(x)=Φ(‖x‖),或者还可以是到任意一点c的距离,c点成为中心点,也就是Φ(x,c)=Φ(‖x-c‖)。任意一个满足Φ(x)=Φ(‖x‖)特性的函数Φ都叫做径向量函数,标准的一般使用欧氏距离,尽管其他距离函数也是可以的。RBF网络能够逼近任意的非线性函数,可以处理系统内的难以解析的规律性,具有良好的泛化能力,并有很快的学习收敛速度,已成功应用于非线性函数逼近、时间序列分析、数据分类、模式识别、信息处理、图像处理、系统建模、控制和故障诊断等。

       RBF(Radial Basis Function)可以看作是一个高维空间中的曲面拟合(逼近)问题,学习是为了在多维空间中寻找一个能够最佳匹配训练数据的曲面,然后来一批新的数据,用刚才训练的那个曲面来处理(比如分类、回归)。RBF的本质思想是反向传播学习算法应用递归技术,这种技术在统计学中被称为随机逼近。RBF里的basis function(径向基函数里的基函数)就是在神经网络的隐单元里提供了提供了一个函数集,该函数集在输入模式(向量)扩展至隐空间时,为其构建了一个任意的“基”。这个函数集中的函数就被称为径向基函数。 很明显,RBF属于神经网络领域的东西,所以像很多神经网络一样,其结构由:输入层、隐层、输出层 三层组成。

二、      编程步骤、思路

(1)读取训练数据

将需要训练的数据(每一类前30个样本)矩阵存入变量property1、property2、property3、property4中,代码如下:

%读取数据

[property_1, property_2,property_3, property_4, class] = textread('E:\iris_all.txt','%f %f %f %f %s');

%测试样本数据

property1 =[property_1(1:30,:);property_1(51:80,:);property_1(101:130,:)];

property2 =[property_2(1:30,:);property_2(51:80,:);property_2(101:130,:)];

property3 =[property_3(1:30,:);property_3(51:80,:);property_3(101:130,:)];

property4 =[property_4(1:30,:);property_4(51:80,:);property_4(101:130,:)];

class1 =[class(1:30,:);class(51:80,:);class(101:130,:)];

(2)建立一个RBF网络

使用matlab的newrb函数,具体代码如下:

   %构造矩阵PT

for i = 1:length(Class)

    P(i,:) = [property1(i,:),property2(i,:),property3(i,:),property4(i,:)];

    T(i,:) = Class(i,:);

end

    P = P';

    T = T';

    Class = Class';

%构造测试矩阵textp

for i = 1:length(property5*3)

    textP(i,:) =[property5(i,:),property6(i,:),property7(i,:),property8(i,:)];

end

textP = textP';

%RBF网络的建立

net = newrbe(P,Class);

(3)使用新的数据集测试这个网络

              将待识别的样本数据(每一类后20个数据样本)放在参数property5、property6、property7、property8中,然后读取即可。然后通过RBF网络进行测试。

%测试数据

property5 =[property_1(31:50,:);property_1(81:100,:);property_1(131:150,:)];

property6 = [property_2(31:50,:);property_2(81:100,:);property_2(131:150,:)];

property7 =[property_3(31:50,:);property_3(81:100,:);property_3(131:150,:)];

property8 =[property_4(31:50,:);property_4(81:100,:);property_4(131:150,:)];

class2 =[class(31:50,:);class(81:100,:);class(131:150,:)];

%RBF网络的测试

Rbfoutput = sim (net,textP) %textp为测试的输入数据

三、      实验结果及分析

核心指令为:

Rbfoutput = sim (net,textP)

sprintf('识别率是%3.3f%%',100*count/s2)

本程序的实验平台是MATLAB2015,计算机操作系统为Windows7 64位。测试结果见如下截图:

网络结构:


测试结果:


分析:从实验运行结果可以看出,本程序的识别率准确率为93.333%,在对每一类的后20个样本数据的分类识别中,Setosa 集中全部分类正确。Versicolor集中有一个样本被错误分类,Virginica集中有三个样本被错误分类,分别见上面截图的红色矩形框内。可见本程序对Virginica集的分类稍微差一些。

思考:本次使用了RBF神经网络,RBF是一种前馈型的神经网络,它的激励函数一般是高斯函数,高斯函数是通过计算输入与函数中心点的距离来算权重的。BP神经网络学习速率是固定的,因此网络的收敛速度慢,需要较长的训练时间。对于一些复杂问题,BP算法需要的训练时间可能非常长,这主要是由于学习速率太小造成的。而RBF神经网络是种高效的前馈式网络,它具有其他前向网络所不具有的最佳逼近性能和全局最优特性,并且结构简单,训练速度快,所以它也比BP网络更优。

四、附录:程序源码

%%***************************************************

%名称:RBF神经网络对Iris数据集进行分类识别

%训练样本:每一类前30个样本   测试样本:每一类后20个样本

%%****************************************************

clear;clc;

%读取数据

[property_1, property_2,property_3, property_4, class] = textread('E:\iris_all.txt','%f %f %f %f %s');

%处理数据

%训练数据

property1 =[property_1(1:30,:);property_1(51:80,:);property_1(101:130,:)];

property2 =[property_2(1:30,:);property_2(51:80,:);property_2(101:130,:)];

property3 =[property_3(1:30,:);property_3(51:80,:);property_3(101:130,:)];

property4 =[property_4(1:30,:);property_4(51:80,:);property_4(101:130,:)];

class1 =[class(1:30,:);class(51:80,:);class(101:130,:)];

%测试数据

property5 =[property_1(31:50,:);property_1(81:100,:);property_1(131:150,:)];

property6 =[property_2(31:50,:);property_2(81:100,:);property_2(131:150,:)];

property7 =[property_3(31:50,:);property_3(81:100,:);property_3(131:150,:)];

property8 =[property_4(31:50,:);property_4(81:100,:);property_4(131:150,:)];

class2 =[class(31:50,:);class(81:100,:);class(131:150,:)];

for i = 1:length(class1)

   switch class1{i};

       case 'Setosa'   

           Class(i,:)=[1 0 0];

       case 'Versicolor'

           Class(i,:)=[0 1 0];

       otherwise 'Virginica';

           Class(i,:)=[0 0 1];

   end

end

for i = 1:length(class2)

   switch class2{i};

       case 'Setosa'   

           textClass(i,:)=[1 0 0];

       case 'Versicolor'

           textClass(i,:)=[0 1 0];

       otherwise 'Virginica';

           textClass(i,:)=[0 0 1];

   end

end

textClass = textClass';

%%------------------------RBF神经网络算法¨------------------------ %%

%构造矩阵PI

for i = 1:length(Class)

    P(i,:) =[property1(i,:),property2(i,:),property3(i,:),property4(i,:)];

    T(i,:) = Class(i,:);

end

    P = P';

    T = T';

    Class = Class';

%构造测试矩阵textp

for i = 1:length(property5*3)

    textP(i,:) =[property5(i,:),property6(i,:),property7(i,:),property8(i,:)];

end

textP = textP';

%RBF网络的建立和训练

net = newrbe(P,Class);

 

%RBF网络的测试

Rbfoutput = sim (net,textP)

 

%计算识别率

[s1,s2] = size(Rbfoutput);

count = 0;

for i = 1:s2

    [m ,index] = max(Rbfoutput(:,i));

    [l,std] = max(textClass(:,i));

  

    if(index==std)

        count = count + 1;

    end

 

end

sprintf('识别率是%3.3f%%',100*count/s2)

 

 

  • 15
    点赞
  • 97
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
好的,首先我们需要准备鸢尾花数据集。这个数据集可以通过sklearn库中的load_iris函数进行加载。 ```python from sklearn.datasets import load_iris iris = load_iris() X = iris.data y = iris.target ``` 接下来,我们需要将数据集拆分为训练集和测试集。这里我们采用train_test_split函数将数据集随机拆分为70%的训练集和30%的测试集。 ```python from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) ``` 然后,我们需要对数据进行预处理。这里我们使用StandardScaler函数对数据进行标准化。 ```python from sklearn.preprocessing import StandardScaler scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) ``` 接下来,我们可以定义径向基神经网络模型,这里我们使用sklearn库中的SVC函数。 ```python from sklearn.svm import SVC model = SVC(kernel='rbf', C=1.0, gamma='scale') ``` 最后,我们可以使用fit函数对模型进行训练,并使用predict函数对测试集进行预测。 ```python model.fit(X_train, y_train) y_pred = model.predict(X_test) ``` 完整代码如下: ```python from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.svm import SVC # 加载鸢尾花数据集 iris = load_iris() X = iris.data y = iris.target # 拆分数据集为训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) # 数据标准化 scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) # 定义径向基神经网络模型 model = SVC(kernel='rbf', C=1.0, gamma='scale') # 训练模型并进行预测 model.fit(X_train, y_train) y_pred = model.predict(X_test) print("预测结果:", y_pred) ``` 如果需要对模型进行评估,可以使用sklearn库中的metrics函数计算准确率、精确率、召回率等指标。 ```python from sklearn.metrics import accuracy_score, precision_score, recall_score print("准确率:", accuracy_score(y_test, y_pred)) print("精确率:", precision_score(y_test, y_pred, average='macro')) print("召回率:", recall_score(y_test, y_pred, average='macro')) ```
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值