【数据分析】基于粒子群优化的模糊专家系统附matlab代码

✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。

🍎个人主页:Matlab科研工作室

🍊个人信条:格物致知。

⛄ 内容介绍

 PSO 神经网络分类 % 所以,网上没有合适的进化分类 Matlab 代码, % 决定用 PSO 做一个。% 此代码获取用于分类的数据输入,其中包含数据和 % 标签并将其存储到“netdata”中。数据由 6% 类的 300 个样本组成,其中包括 40 个特征。您可以提取您的特征并 % 标记它,因为它是一个监督模型。这些特征是从小物体图像中提取的 SURF % 特征。现在,System 是 PSO % 和典型的浅层神经网络的组合。神经网络本身构成了系统的 % 初始结构或主体,但 PSO 有责任对训练中的 % 神经元进行加权,进化算法的真正力量在这里体现。最后与SVM、KNN的结果对比,和 TREE 分类 % 算法作为混淆矩阵和最终识别准确率。% 有三个重要参数 'NH'(隐藏层数),% 'SwarmSize' 和 'MaxIteration' 会显着影响系统的性能。因此,为了获得理想的结果,您应该根据您的数据使用这些参数来玩 %。这里唯一的缺点是 % 标签是手动完成的,你可以很容易地自己修复它,但 % 是在训练的主要阶段之后。此代码可以扩展为使用其他进化算法(例如 GA 或 DE)进行训练。如果您发现 % 有任何问题,请按以下方式与我联系:这会显着影响您 % 系统的性能。因此,为了获得理想的结果,您应该根据您的数据使用这些参数来玩 %。这里唯一的缺点是 % 标签是手动完成的,你可以很容易地自己修复它,但 % 是在训练的主要阶段之后。此代码可以扩展为使用其他进化算法(例如 GA 或 DE)进行训练。如果您发现 % 有任何问题,请按以下方式与我联系:这会显着影响您 % 系统的性能。因此,为了获得理想的结果,您应该根据您的数据使用这些参数来玩 %。这里唯一的缺点是 % 标签是手动完成的,你可以很容易地自己修复它,但 % 是在训练的主要阶段之后。此代码可以扩展为使用其他进化算法(例如 GA 或 DE)进行训练。

⛄ 部分代码

%% PSO Neural Network Classification

% So, there was no proper evolutionary classification Matlab code in the web,

% Which decided to make one with PSO.

% This code gets data input for classification which contains data and 

% labels and stores it into 'netdata'. data consists of 300 samples for 6 

% classes which includes 40 features. You can extract your features and

% label it as it is a supervised model. These features are extracted SURF

% features out of small objects images. Now, System is combination of PSO

% and typical shallow neural network. Neural network itself makes the

% initial structure or body of the system but PSO has duty of weighting the

% neurons in training, which true power of evolutionary algorithms present

% here. Finally, result compared with SVM, KNN, and TREE classification 

% algorithms as confusion matrix and final recognition accuracy.

% There are three important parameters of 'NH' (number of hidden layers),

% 'SwarmSize' and 'MaxIteration' which effect the performance of thee

% system significantly. So, in order to get desired result, you should play

% with these parameters based on your data. The only drawback here is that

% labeling is done manually, which you can fix it yourself easily, but it

% is after main stage of training. This code could be expanded to be

% trained with other evolutionary algorithms such as GA or DE. If you find

% any problem, please contact me as below:

% Email: mosavi.a.i.buali@gmail.com

% Author: Seyed Muhammad Hossein Mousavi

% Also this code is part of the following project, so please cite below 

% after using the code: 

% Mousavi, Seyed Muhammad Hossein, et al. "A PSO fuzzy-expert system: As an

% assistant for specifying the acceptance by NOET measures, at PH. D 

% level." 2017 Artificial Intelligence and Signal Processing Conference

% (AISP). IEEE, 2017.

% Thank you for citing the paper and enjoy the code (hope it help you (Be happy :)

%%

warning('off');

% Data Loading

clear;

netdata=load('fortest2.mat');

netdata=netdata.FinalReady;

% Data and Label

network=netdata(:,1:end-1);

netlbl=netdata(:,end);

% Var Change

inputs = network;

targets = netlbl;

% Dim Size

InputNum = size(inputs,2);

OutputNum = size(targets,2);

pr = [-1 1];

PR = repmat(pr,InputNum,1);

% NN Structure (log-sigmoid transfer function)

NH=5;     % Number of Hidden Layers (more better)

Network1 = newff(PR,[NH OutputNum],{'tansig' 'tansig'});

% Train with PSO on Networks Weights

Network1 = TrainPSO(Network1,inputs,targets);

view(Network1)

% Generating Outputs from Our PSO + NN Network Model

outputs = Network1(inputs');

outputs=outputs';

% Size

sizenet=size(network);

sizenet=sizenet(1,1);

% Outputs Error

MSE=mse(outputs);

% Bias Output for Confusion Matrix

outputs=outputs-(MSE*0.1)/2;

% Detecting Mislabeled Data

for i=1 : 50

            if outputs(i) <= 0.9

               out(i)=0;

        elseif outputs(i) >= 0.9

               out(i)=1;            end;end;

for i=51 : 100

            if outputs(i) <= 0.9

               out(i)=0;

        elseif outputs(i) >= 0.9

               out(i)=2;            end;end;

for i=101 : 150

            if outputs(i) <= 0.9

               out(i)=0;

        elseif outputs(i) >= 0.9

               out(i)=3;            end;end;

for i=151 : 200

            if outputs(i) <= 0.9

               out(i)=0;

        elseif outputs(i) >= 0.9

               out(i)=4;            end;end;

for i=201 : 250

            if outputs(i) <= 0.9

               out(i)=0;

        elseif outputs(i) >= 0.9

               out(i)=5;            end;end;

for i=251 : 300

            if outputs(i) <= 0.9

               out(i)=0;

        elseif outputs(i) >= 0.9

               out(i)=6;            end;end;

       out1=single(out');

% PSO Final Accuracy

       psomse=mse(out1,targets);

       MSEError=abs(mse(targets)-mse(out1));

       cnt=0;

       for i=1:sizenet

           if out1(i)~= targets(i)

               cnt=cnt+1;

           end;

       end;

      fin=cnt*100/ sizenet;

      psoacc=(100-fin)-psomse;

%

%% KNN for Comparison 

lblknn=netdata(:,end);

dataknn=netdata(:,1:end-1);

Mdl = fitcknn(dataknn,lblknn,'NumNeighbors',8,'Standardize',1);

rng(1); % For reproducibility

knndat = crossval(Mdl);

classError = kfoldLoss(knndat);

% Predict the labels of the training data.

predictedknn = resubPredict(Mdl);

ctknn=0;

for i = 1 : sizenet(1,1)

if lblknn(i) ~= predictedknn(i)

    ctknn=ctknn+1;

end;

end;

finknn=ctknn*100/ sizenet;

KNN=(100-finknn)-classError;

%

%% SVM for Comparison

tsvm = templateSVM('KernelFunction','polynomial');

svmclass = fitcecoc(dataknn,lblknn,'Learners',tsvm);

svmerror = resubLoss(svmclass);

CVMdl = crossval(svmclass);

genError = kfoldLoss(CVMdl);

% Predict the labels of the training data.

predictedsvm = resubPredict(svmclass);

ct=0;

for i = 1 : sizenet(1,1)

if lblknn(i) ~= predictedsvm(i)

    ct=ct+1;

end;

end;

% Compute Accuracy

finsvm=ct*100/ sizenet;

SVMAccuracy=(100-finsvm);

%% Tree for Comparison

Mdl2 = fitctree(dataknn,lblknn);

rng(1); % For reproducibility

treedat = crossval(Mdl2);

classErrortree = kfoldLoss(treedat);

% Predict the labels of the training data.

predictedtree = resubPredict(Mdl2);

cttree=0;

for i = 1 : sizenet(1,1)

if lblknn(i) ~= predictedtree(i)

    cttree=cttree+1;

end;

end;

fintree=cttree*100/ sizenet;

TREE=(100-fintree)-classErrortree;

%% Plots and Results

% Confusion Matrix

figure

% set(gcf, 'Position',  [50, 100, 1300, 300])

subplot(2,2,1)

cmsvm = confusionchart(lblknn,predictedsvm);

cmsvm.Title = (['SVM Classification =  ' num2str(SVMAccuracy) '%']);

subplot(2,2,2)

cmknn = confusionchart(lblknn,predictedknn);

cmknn.Title = (['KNN Classification =  ' num2str(KNN) '%']);

subplot(2,2,3)

cmtree = confusionchart(lblknn,predictedtree);

cmtree.Title = (['Tree Classification =  ' num2str(TREE) '%']);

subplot(2,2,4)

cmpso = confusionchart(out1,targets);

cmpso.Title = (['PSO-NN Classification =  ' num2str(psoacc) '%']);

% Regression

figure

set(gcf, 'Position',  [50, 150, 450, 350])

[population2,gof] = fit(targets,out1,'poly4');

plot(targets,out1,'o',...

    'LineWidth',3,...

    'MarkerSize',5,...

    'Color',[0.3,0.9,0.2]);

    title(['PSO - R =  ' num2str(1-gof.rmse)]);

    xlabel('Train Target');

    ylabel('Train Output');   

hold on

plot(population2,'b-','predobs');

    xlabel(' Target');

    ylabel(' Output');   

hold off

% ACC and Metrics Results

fprintf('The SVM Accuracy is = %0.4f.\n',SVMAccuracy)

fprintf('The KNN Accuracy is = %0.4f.\n',KNN)

fprintf('The Tree Accuracy is = %0.4f.\n',TREE)

fprintf('The PSO Accuracy is = %0.4f.\n',psoacc)

fprintf('PSO MSE is = %0.4f.\n',MSEError)

fprintf('PSO RMSE is = %0.4f.\n',sqrt(MSEError))

fprintf('PSO MAE is = %0.4f.\n',mae(targets,out1))

⛄ 运行结果

⛄ 参考文献

⛄ Matlab代码关注

❤️部分理论引用网络文献,若有侵权联系博主删除

❤️ 关注我领取海量matlab电子书和数学建模资料

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
模糊c均值聚类是一种广泛应用于数据挖掘和模式识别领域中的聚类算法。它的主要优点是能够在处理样本集时考虑到样本内部的相似性和差异性,从而可以准确地确定各个样本所属的聚类类别。此外,模糊c均值聚类还可以有效地处理非线性分布的数据,对异常数据有较好的适应性。 基于粒子群优化模糊c均值聚类,是一种针对传统模糊c均值聚类算法进行优化的方法。该算法将群体智能优化思想引入到聚类分析中,通过不断优化输入数据和聚类策略来提高算法的聚类性能。 在matlab环境下运行基于粒子群优化模糊c均值聚类算法,需要先安装相应的matlab工具箱,在此基础上,进行以下步骤: 1. 定义聚类的数量和样本属性。 2. 根据所选的数据,确定粒子的个数和维度,并初始化群体。 3. 根据粒子的位置和速度信息,更新群体信息,计算适应度值。 4. 通过适应度值选择最优的粒子,并根据其位置信息重新调整聚类中心。 5. 根据粒子的速度和位置信息更新当前群体。 6. 重复执行步骤3-5,直到达到指定的迭代次数或满足停止准则为止。 输出结果将是所得到的聚类结果和对应的聚类中心。 总之,基于粒子群优化模糊c均值聚类matlab是一种聚类算法,通过引入群体智能优化思想,来优化传统的模糊c均值聚类算法,从而提高了聚类的准确性和效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

matlab科研助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值