一、模式識別神經網絡
在matlab命令窗口輸入:nnstart 或 nprtool 就可以進入matlab神經網絡GUI
二、鳶尾花數據集iris示例
1.輸入數據集,划分訓練集、測試集
load fisheriris; [m,n]=size(meas); data=zeros(m,n+1); data(:,1:n)=meas; for i=1:m %將字符串類別標簽用數值形式表示 if strcmp(species{i},'setosa') %strcmp('A','B')用於比較字符串,找出特定的字符串;類比find(a==b)用來找出特定數值 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
%選擇訓練樣本個數 num_train = 60; %共150個樣本,60個訓練集,90個測試集 %構造隨機選擇序列 choose = randperm(length(data)); %隨機種子打亂數據樣本的順序 train_data = data(choose(1:num_train),:); %隨機選取60個樣本 label_temp = train_data(:,end); %提取訓練數據的標簽 train(:,end)提取最后一列; label_train = zeros(length(train_data),3); %創建矩陣以儲存向量形式的標簽;
%把輸出分類標簽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)'; %提取數據集特征(剔除標簽),並進行轉置(轉置也可以不必,后續GUI中轉化為行形式即可) label_train = label_train'; %將向量形式表示的標簽進行轉置(也而不必,理由同上)
test_data = data(choose(num_train+1:end),:); %提取測試集數據 label_temp = test_data(:,end); %提取測試集數據的標簽 label_test = zeros(length(test_data),3); %創建矩陣,准備存放向量形式的測試數據的標簽 %把輸出分類標簽改為工具箱要求的格式 for i = 1:length(test_data) label_test(i,label_temp(i)) = 1; end test_data = test_data(:,1:end-1)'; %提取測試數據的特征,並進行轉置 label_test = label_test'; %提取測試數據的標簽,並進行轉置
2. 三種方法進行模式識別神經網絡搭建
2.1 手動編寫m函數法
法1操作方法:手動編寫m函數如下(借鑒參考資料)
%有三種方式 %法1.命令窗口輸入nnstart,選擇pattern recognition app,用matlab自帶GUI進行網絡設置(最簡單) %法2.完成法1后,自動生成代碼,將創建網絡的代碼用m文件保存,下次要調用該網絡可直接調用該m文件 %法3.編寫如下代碼 % Create a Pattern Recognition Network hiddenLayerSize = 10; %隱藏層神經元個數設置為10 net = patternnet(hiddenLayerSize); %創建模式識別神經網絡patternnet % 將訓練集再按比例70:15:15分為訓練集、驗證集、測試集 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為訓練過程參數? % Test the Network predict = net(test_data); %得到每個樣本屬於第一類、第二類、第三類的概率 [~,predict] = max(predict); %選擇概率最大的類別作為某一個測試樣本的類別 %% 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結果:

准確率:93.3%,分類效果不錯。
2.2 GUI法
法2操作方法:
輸入nnstart:四種形式的神經網絡:擬合/分類/聚類/時間序列。分類選擇nprtool

輸入nprtool:

注意這里的samples是按行還是按列,如果選擇錯誤則無法點擊next
法2結果:訓練集的混淆矩陣

可以重點看訓練集(分為0.7:0.15:0.15)中的測試集 (是否有必要將訓練集也這么分?);
得到准確率為88.9%,較高,分類效果不錯。
法2操作:加入測試集

法2結果:測試集的混淆矩陣

准確率為96.7%,分類效果很好。
2.3 自動生成代碼法
法3操作方法:由法1GUI得到的網絡自動生成m代碼如下,之后可直接調用該m文件,不需要用GUI。
操作如下:

點擊Simple Scrip即可自動生成代碼
代碼如下:
% 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.監督算法之BP,SVM,adaboost的非線性多分類實驗,https://blog.csdn.net/on2way/article/details/48006539,作者:on2way
2.matlab神經網絡工具箱:https://blog.csdn.net/on2way/article/details/47428201
3.adaboost分類:https://www.cnblogs.com/litthorse/p/9332370.html
