分类预测 | MATLAB实现基于PSO-NN、SVM、KNN、DT的多特征数据分类预测,二分类及多分类

分类预测 | MATLAB实现基于PSO-NN、SVM、KNN、DT的多特征数据分类预测,二分类及多分类

分类效果

1
2
3

基本描述

Matlab实现基于PSO-NN、SVM、KNN、DT的多特征数据分类预测,二分类及多分类(完整程序和数据)
基于PSO-NN、SVM、KNN、DT的多特征数据分类预测,二分类及多分类(Matlab完整程序和数据)
此代码获取用于分类的数据输入。
数据由 6 个 300 个样本组成,包含 40 个特征的类。 你可以提取你的特征和将其标记为监督模型。
PSO-NN-粒子群优化神经网络
SVM-支持向量机,
KNN-k近邻,
DT-决策树。
结果与SVM、KNN、TREE分类算法作为混淆矩阵和最终识别准确率进行比较。
“NH”(隐藏数)、“SwarmSize”和“MaxIteration”这三个重要参数会显着影响系统的性能。

程序设计

function [Network2  BestCost] = TrainPSO(Network,Xtr,Ytr)
% Statement
IW = Network.IW{
    
    1,1}; IW_Num = numel(IW);
LW = Network.LW{
    
    2,1}; LW_Num = numel(LW);
b1 = Network.b{
    
    1,1}; b1_Num = numel(b1);
b2 = Network.b{
    
    2,1}; b2_Num = numel(b2);
TotalNum = IW_Num + LW_Num + b1_Num + b2_Num;
NPar = TotalNum;
VarMin = -1*ones(1,TotalNum);
VarMax = +1*ones(1,TotalNum);
CostFuncName = 'NNCost';
%% PSO Parameters
SwarmSize = 9;
MaxIteration = 20;
C1 = 2; % Cognition Coefficient;
C2 = 4 - C1; % Social Coefficient;
%
% Initial Population
GBest.Cost = inf;
GBest.Position = [];
GBest.CostMAT = [];
for p = 1:SwarmSize
    Particle(p).Position = rand(1,NPar) .* (VarMax - VarMin) + VarMin;
    Particle(p).Cost = feval(CostFuncName,Particle(p).Position,Xtr,Ytr,Network);
    Particle(p).Velocity = [];
    Particle(p).LBest.Position = Particle(p).Position;
    Particle(p).LBest.Cost = Particle(p).Cost;
if Particle(p).LBest.Cost < GBest.Cost
        GBest.Cost = Particle(p).LBest.Cost;
        GBest.Position = Particle(p).LBest.Position;
    end
end
% Optimization
for Iter = 1:MaxIteration
    % Velocity update
for p = 1:SwarmSize
        Particle(p).Velocity = C1 * rand * (Particle(p).LBest.Position - Particle(p).Position) + C2 * rand * (GBest.Position - Particle(p).Position);
        Particle(p).Position = Particle(p).Position + Particle(p).Velocity;
                Particle(p).Position = max(Particle(p).Position , VarMin);
        Particle(p).Position = min(Particle(p).Position , VarMax);        
                Particle(p).Cost = feval(CostFuncName,Particle(p).Position,Xtr,Ytr,Network);
if Particle(p).Cost < Particle(p).LBest.Cost
            Particle(p).LBest.Position = Particle(p).Position;
            Particle(p).LBest.Cost = Particle(p).Cost;
if Particle(p).LBest.Cost < GBest.Cost
                GBest.Cost = Particle(p).LBest.Cost;
                GBest.Position = Particle(p).LBest.Position;
            end
        end
    end
% Plot
    disp(['In Itration Number = ' num2str(Iter) '; Highest Cost Is  = ' num2str(GBest.Cost) ';'])
    GBest.CostMAT = [GBest.CostMAT GBest.Cost];
end
GBest.Position;
figure
set(gcf, 'Position',  [450, 250, 900, 350])
plot(GBest.CostMAT,'-.',...
    'LineWidth',2,...
    'MarkerSize',8,...
    'MarkerEdgeColor','g',...
    'Color',[0.6,0.1,0]);
title('Particle Swarm Pptimization Train')
xlabel('PSO Iteration Number','FontSize',10,...
       'FontWeight','bold','Color','m');
ylabel('PSO Best Cost Result','FontSize',10,...
       'FontWeight','bold','Color','m');
legend({
    
    'PSO Train'});
Network2 = NetConstruct(Network,GBest.Position);
BestCost = GBest.Cost;
end
  • 运行结果
In Itration Number = 1; Highest Cost Is  = 1.0549;
In Itration Number = 2; Highest Cost Is  = 0.78452;
In Itration Number = 3; Highest Cost Is  = 0.77275;
In Itration Number = 4; Highest Cost Is  = 0.73094;
In Itration Number = 5; Highest Cost Is  = 0.69577;
In Itration Number = 6; Highest Cost Is  = 0.69271;
In Itration Number = 7; Highest Cost Is  = 0.67064;
In Itration Number = 8; Highest Cost Is  = 0.64104;
In Itration Number = 9; Highest Cost Is  = 0.63716;
In Itration Number = 10; Highest Cost Is  = 0.63231;
In Itration Number = 11; Highest Cost Is  = 0.62888;
In Itration Number = 12; Highest Cost Is  = 0.62852;
In Itration Number = 13; Highest Cost Is  = 0.62841;
In Itration Number = 14; Highest Cost Is  = 0.62823;
In Itration Number = 15; Highest Cost Is  = 0.62802;
In Itration Number = 16; Highest Cost Is  = 0.62742;
In Itration Number = 17; Highest Cost Is  = 0.62742;
In Itration Number = 18; Highest Cost Is  = 0.62734;
In Itration Number = 19; Highest Cost Is  = 0.62728;
In Itration Number = 20; Highest Cost Is  = 0.62718;
The SVM Accuracy is = 100.0000.
The KNN Accuracy is = 93.1264.
The Tree Accuracy is = 99.5358.
The PSO Accuracy is = 76.3849.
PSO MSE is = 0.4075.
PSO RMSE is = 0.6384.
PSO MAE is = 0.2906.
历时 3.714446 秒。

参考资料

[1] https://blog.csdn.net/kjm13182345320/article/details/129036772?spm=1001.2014.3001.5502
[2] https://blog.csdn.net/kjm13182345320/article/details/128690229

猜你喜欢

转载自blog.csdn.net/kjm13182345320/article/details/130473037