matlab神经网络多分类(模式识别神经网络nprtool)
一、模式識(shí)別神經(jīng)網(wǎng)絡(luò)
在matlab命令窗口輸入:nnstart 或 nprtool 就可以進(jìn)入matlab神經(jīng)網(wǎng)絡(luò)GUI
二、鳶尾花數(shù)據(jù)集iris示例
1.輸入數(shù)據(jù)集,劃分訓(xùn)練集、測(cè)試集
load fisheriris;
[m,n]=size(meas);
data=zeros(m,n+1);
data(:,1:n)=meas;
for i=1:m %將字符串類(lèi)別標(biāo)簽用數(shù)值形式表示
if strcmp(species{i},'setosa') %strcmp('A','B')用于比較字符串,找出特定的字符串;類(lèi)比f(wàn)ind(a==b)用來(lái)找出特定數(shù)值
data(i,n+1)=1;
elseif strcmp(species{i},'versicolor')
data(i,n+1)=2;
elseif strcmp(species{i},'virginica')
data(i,n+1)=3;
end
end
%選擇訓(xùn)練樣本個(gè)數(shù)
num_train = 60; %共150個(gè)樣本,60個(gè)訓(xùn)練集,90個(gè)測(cè)試集
%構(gòu)造隨機(jī)選擇序列
choose = randperm(length(data)); %隨機(jī)種子打亂數(shù)據(jù)樣本的順序
train_data = data(choose(1:num_train),:); %隨機(jī)選取60個(gè)樣本
label_temp = train_data(:,end); %提取訓(xùn)練數(shù)據(jù)的標(biāo)簽 train(:,end)提取最后一列;
label_train = zeros(length(train_data),3); %創(chuàng)建矩陣以儲(chǔ)存向量形式的標(biāo)簽;
%把輸出分類(lèi)標(biāo)簽1,2,3 改為工具箱要求的格式 1=[1 0 0],2=[0 1 0],3=[0 0 1]
for i = 1:length(train_data)
label_train(i,label_temp(i)) = 1;
end
train_data = train_data(:,1:end-1)'; %提取數(shù)據(jù)集特征(剔除標(biāo)簽),并進(jìn)行轉(zhuǎn)置(轉(zhuǎn)置也可以不必,后續(xù)GUI中轉(zhuǎn)化為行形式即可)
label_train = label_train'; %將向量形式表示的標(biāo)簽進(jìn)行轉(zhuǎn)置(也而不必,理由同上)
test_data = data(choose(num_train+1:end),:); %提取測(cè)試集數(shù)據(jù)
label_temp = test_data(:,end); %提取測(cè)試集數(shù)據(jù)的標(biāo)簽
label_test = zeros(length(test_data),3); %創(chuàng)建矩陣,準(zhǔn)備存放向量形式的測(cè)試數(shù)據(jù)的標(biāo)簽
%把輸出分類(lèi)標(biāo)簽改為工具箱要求的格式
for i = 1:length(test_data)
label_test(i,label_temp(i)) = 1;
end
test_data = test_data(:,1:end-1)'; %提取測(cè)試數(shù)據(jù)的特征,并進(jìn)行轉(zhuǎn)置
label_test = label_test'; %提取測(cè)試數(shù)據(jù)的標(biāo)簽,并進(jìn)行轉(zhuǎn)置
2. 三種方法進(jìn)行模式識(shí)別神經(jīng)網(wǎng)絡(luò)搭建
2.1 手動(dòng)編寫(xiě)m函數(shù)法
法1操作方法:手動(dòng)編寫(xiě)m函數(shù)如下(借鑒參考資料)
%有三種方式 %法1.命令窗口輸入nnstart,選擇pattern recognition app,用matlab自帶GUI進(jìn)行網(wǎng)絡(luò)設(shè)置(最簡(jiǎn)單) %法2.完成法1后,自動(dòng)生成代碼,將創(chuàng)建網(wǎng)絡(luò)的代碼用m文件保存,下次要調(diào)用該網(wǎng)絡(luò)可直接調(diào)用該m文件 %法3.編寫(xiě)如下代碼 % Create a Pattern Recognition Network hiddenLayerSize = 10; %隱藏層神經(jīng)元個(gè)數(shù)設(shè)置為10 net = patternnet(hiddenLayerSize); %創(chuàng)建模式識(shí)別神經(jīng)網(wǎng)絡(luò)patternnet % 將訓(xùn)練集再按比例70:15:15分為訓(xùn)練集、驗(yàn)證集、測(cè)試集 net.divideParam.trainRatio = 70/100; net.divideParam.valRatio = 15/100; net.divideParam.testRatio = 15/100; % Train the Network [net,tr] = train(net,train_data,label_train); %tr為訓(xùn)練過(guò)程參數(shù)? % Test the Network predict = net(test_data); %得到每個(gè)樣本屬于第一類(lèi)、第二類(lèi)、第三類(lèi)的概率 [~,predict] = max(predict); %選擇概率最大的類(lèi)別作為某一個(gè)測(cè)試樣本的類(lèi)別 %% show the result --testings fig=figure; gscatter(test_data(1,:),test_data(2,:),predict); [~,label_test] = max(label_test); accuracy = length(find(predict==label_test))/length(test_data); title(['predict the testing data and the accuracy is :',num2str(accuracy)]);
法1結(jié)果:
準(zhǔn)確率:93.3%,分類(lèi)效果不錯(cuò)。
2.2 GUI法
法2操作方法:
輸入nnstart:四種形式的神經(jīng)網(wǎng)絡(luò):擬合/分類(lèi)/聚類(lèi)/時(shí)間序列。分類(lèi)選擇nprtool
輸入nprtool:
注意這里的samples是按行還是按列,如果選擇錯(cuò)誤則無(wú)法點(diǎn)擊next
法2結(jié)果:訓(xùn)練集的混淆矩陣
可以重點(diǎn)看訓(xùn)練集(分為0.7:0.15:0.15)中的測(cè)試集 (是否有必要將訓(xùn)練集也這么分?);
得到準(zhǔn)確率為88.9%,較高,分類(lèi)效果不錯(cuò)。
法2操作:加入測(cè)試集
法2結(jié)果:測(cè)試集的混淆矩陣
準(zhǔn)確率為96.7%,分類(lèi)效果很好。
2.3 自動(dòng)生成代碼法
法3操作方法:由法1GUI得到的網(wǎng)絡(luò)自動(dòng)生成m代碼如下,之后可直接調(diào)用該m文件,不需要用GUI。
操作如下:
點(diǎn)擊Simple Scrip即可自動(dòng)生成代碼
代碼如下:
% Solve a Pattern Recognition Problem with a Neural Network % Script generated by Neural Pattern Recognition app % Created 21-May-2020 20:32:42 % % This script assumes these variables are defined: % % train_data - input data. % label_train - target data. x = train_data; t = label_train; % Choose a Training Function % For a list of all training functions type: help nntrain % 'trainlm' is usually fastest. % 'trainbr' takes longer but may be better for challenging problems. % 'trainscg' uses less memory. Suitable in low memory situations. trainFcn = 'trainscg'; % Scaled conjugate gradient backpropagation. % Create a Pattern Recognition Network hiddenLayerSize = 10; net = patternnet(hiddenLayerSize); % Setup Division of Data for Training, Validation, Testing net.divideParam.trainRatio = 70/100; net.divideParam.valRatio = 15/100; net.divideParam.testRatio = 15/100; % Train the Network [net,tr] = train(net,x,t); % Test the Network y = net(x); e = gsubtract(t,y); performance = perform(net,t,y) tind = vec2ind(t); yind = vec2ind(y); percentErrors = sum(tind ~= yind)/numel(tind); % View the Network view(net) % Plots % Uncomment these lines to enable various plots. %figure, plotperform(tr) %figure, plottrainstate(tr) %figure, ploterrhist(e) %figure, plotconfusion(t,y) %figure, plotroc(t,y)
參考資料:
1.監(jiān)督算法之BP,SVM,adaboost的非線性多分類(lèi)實(shí)驗(yàn),https://blog.csdn.net/on2way/article/details/48006539,作者:on2way
2.matlab神經(jīng)網(wǎng)絡(luò)工具箱:https://blog.csdn.net/on2way/article/details/47428201
3.adaboost分類(lèi):https://www.cnblogs.com/litthorse/p/9332370.html
總結(jié)
以上是生活随笔為你收集整理的matlab神经网络多分类(模式识别神经网络nprtool)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Photoshop软件破解补丁安装方法
- 下一篇: c++ 指针(一)