《量化投资:以MATLAB为工具》

MATLAB技术论坛

 找回密码
 注册账号
查看: 1399|回复: 0
收起左侧

[源码] 基于k均值聚类学习算法的rbf神经网络实现

  [复制链接]
发表于 2015-1-20 11:10:29 | 显示全部楼层 |阅读模式
function main()
SamNum=100;
TestSamNum=101;
InDim=1;
ClusterNum=10;
Overlap=1.0;
rand('state',sum(100*clock))
NoidseVar=0.1;
Noise=NoidseVar*rand(1,SamNum);
SamIn=8*rand(1,SamNum)-4;
SamOutNoNoise=1.1*(1-SamIn+2*SamIn.^2).*exp(-SamIn.^2/2);
SamOut=SamOutNoNoise+Noise;

TestSamIn=-4:0.08:4;
TestSamOut=1.1*(1-TestSamIn+2*TestSamIn.^2).*exp(-TestSamIn.^2/2);

figure
hold on  
grid
plot(SamIn,SamOut,'k+')
plot(TestSamIn,TestSamOut,'k--')
xlabel('Input x');
ylabel('Output y');


Centers=SamIn(:,1:ClusterNum);
NumberInClusters=zeros(ClusterNum,1);
IndexInClusters=zeros(ClusterNum,SamNum);
while 1,
    NumberInClusters=zeros(ClusterNum,1);
IndexInClusters=zeros(ClusterNum,SamNum);
for i=1:SamNum
    AllDistance=dist(Centers',SamIn(:,i));
    [MinDist,Pos]=min(AllDistance);
    NumberInClusters(Pos)=NumberInClusters(Pos)+1;
    IndexInClusters(Pos,NumberInClusters(Pos))=i;
end

OldCenters=Centers;

for i=1:ClusterNum
    Index=IndexInClusters(i,1:NumberInClusters(i));
    Centers(:,i)=mean(SamIn(:,Index)')';
end

EqualNum=sum(sum(Centers==OldCenters));
if EqualNum==InDim*ClusterNum,
    break
end
end

AllDistances=dist(Centers',Centers)
Maximun=max(max(AllDistance));
for i=1:ClusterNum
   AllDistances(i,i)=Maximun+1;
end
Spreads=Overlap*min(AllDistances)';

Distance=dist(Centers',SamIn);
SpreadsMat=repmat(Spreads,1,SamNum);
HiddenUnitOut=radbas(Distance./SpreadsMat);
HiddenUnitOutEx=[HiddenUnitOut' ones(SamNum,1)]';
W2Ex=SamOut*pinv(HiddenUnitOutEx);
W2=W2Ex(:,1:ClusterNum);
B2=W2Ex(:,ClusterNum+1);

TestDistance=dist(Centers',TestSamIn);
TestSpreadsMat=repmat(Spreads,1,TestSamNum);
TestHiddenUnitOut=radbas(TestDistance./TestSpreadsMat);
TestNNOut=W2*TestHiddenUnitOut+B2;
plot(TestSamIn,TestNNOut,'r-')

W2
B2
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

QQ|网站地图|MATLAB技术论坛|Simulink仿真论坛 ( 蜀ICP备19014457号-2 

GMT+8, 2022-8-18 19:46 , Processed in 0.051043 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表