资源简介

机器学习,BP神经网络进行多分类matlab代码

资源截图

代码片段和文件信息

function glass1=classifyBP()
clc;
clear;
load glass.txt;
data=[glass(:2:11)];
glass_train=[data(1:35:);data(71:108:);data(147:155:);data(164:170:);data(177:181:);data(186:200:)];%人工分成训练集
glass_test=[data(36:70:);data(109:146:);data(156:163:);data(171:176:);data(182:185:);data(201:214:)];%人工分成测试集

train_label=[glass_train(:10)];%训练集标签
test_label=[glass_test(:10)];%测试集标签

%%%%%%%%%%%%%%%%%%%对数据进行出来,于适合BP神经网络函数的调用
glass_train=glass_train‘;
glass_test=glass_test‘;
train_label=train_label‘;
test_label=test_label‘;

%对训练集和测试集进行归一化
[train1minpmaxplabelmintmaxt] = premnmx(glass_traintrain_label);
[test]=tramnmx(glass_testminpmaxp);

%构造神经网络
net = newff( minmax(train1)  [10 1]  { ‘tansig‘ ‘tansig‘ }  ‘traingdx‘ ) ;
%设置训练参数
net.trainparam.show = 50 ;
net.trainparam.epochs =2500;
net.trainparam.goal = 0.00001 ;
net.trainParam.lr = 0.01 ;

%开始训练
net = train(nettrain1label);


%测试集仿真
TEST = sim(nettest);
[y1]=postmnmx(TESTmintmaxt);%反归一化
testresult=tonumber1(y1)
erroroftest=evalu(test_labeltestresult)
%训练集仿真
Z = sim(nettrain1);
[z]=postmnmx(Zmintmaxt);%反归一化
trainresult=tonumber1(z)
erroroftrain1=evalu(train_labeltrainresult)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%子函数
function y=tonumber1(x)
for i=1:size(x2)
   if x(i)<1.5
       x(i)=1;
    end
    if 1.5       x(i)=2;
    end
    if 2.5        x(i)=3;
    end
    if 3.5        x(i)=4;
    end
    if 4.5        x(i)=5;
    end
    if 5.5        x(i)=6;
    end
    if 6.5        x(i)=7;
    end
    y(i)=x(i);
end

function error=evalu(ab)
error=0;%分类错误的个数
for i=1:size(a2)
    if a(i)~=b(i)
        error=error+1;
    end
end

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        1896  2020-09-13 11:13  classifyBP.m
     文件       12119  2020-09-13 11:13  glass.txt

评论

共有 条评论