2012-05-18 29 views
-1

我想申请感知算法fisheriris数据和我尝试这种代码fisheriris数据和感知

function [ ] = Per() 
%PERCEPTON_NN Summary of this function goes here 
% Detailed explanation goes here 
%%%%%%%%%%%STEP ONE INPUT DATA PREPERATION 
%N=3000; 
load fisheriris 
tr=50; %traning 
te=50; %test 
epochs =150; 

data=meas; 
%N = size(meas,1); 
%species=nonomil(species) 
%figure,plot(data_shuffeled(1,:),data_shuffeled(2,:),'rx'); 
%%%%%%%%%%%%%%%%%%%STEP TWO INTIALIZE WEIGHT 
baise=1; 
w=[baise; 1 ; 1;1 ; 1]; 
eta=0.9; %%learning rate 
%%%%%%%%%%%%%%%%%%%%%%%% Rosen Blatt learning Algo 
for epoch=1 : epochs 
for i=1 : tr 
    x=[1;data(i,1);data(i,2);data(i,3);data(i,4)]; % input vector 
    N = size(species,i); %desiard output 
    y=w'*x; % y=actual output ,w'= transpose w , mmoken y=w.*x 
    %%%%%%%%%%%%%%% Activation function (hardlimit)(step fn) 
    y=1*(y>=0)+(-1)*(y<0); % da badl el if 
    %%%%%%%%%%% Error calcualtion %%%%%%% 
    err(i)=N-y; 
    %%%%%%%%%%%%%% update weight %%%%%%%%%5 
    wnew=w+ eta*err(i)*x; 
    w=wnew; 
end 
mse(epoch)=mean(err.^2); 
end 
%%%%%%%%%%%%%%%%%%%%%% step four classification (testing) %%%%%%%%%%%%%%%%%%5 
hold on 
for i=1 : te 
    %x=[1;data(i,1);data(i,2),data(i,3);data(i,4)]; 
    x=[1;data(i,1);data(i,2);data(i,3);data(i,4)]; 
    % d=data_shuffeled(3,i+tr); 
    N = size(species,i); 
    y=w'*x; 
    y=1*(y>=0)+(-1)*(y<0); 
    if (y==1) 
     plot(x(2),x(3),x(4),x(5),'rx'); 
    elseif y==-1 
     plot(x(2),x(3),x(4),x(5),'r&'); 
    end 
end 
hold off 

if abs(N-y)>1E-6 
    testerro=testerro+1; 

end 

我写了这个代码,以使与fisheriris数据“MEAS”感知算法作为输入和物种“输出”

对代码的任何帮助或对此代码的任何修改。

谢谢。

+0

这个问题太笼统了。将来,请提供可管理的小代码片段,指出您正面临的特定问题,然后准确指出您要查找的内容 - 性能改进,获取的错误等等。 – kitchenette

回答

0

首先,你可知道,MATLAB有东西给神经网络训练称为Neural network toolbox?

其次,认为data_shuffeled是你自己的功能。在MATLAB中有一个叫做randperm的东西,你应该用它来混洗你的数据。

第三,当您可以在MATLAB中使用矢量/矩阵时,您希望避免使用for-loops。

而不是做(测试)

for i = 1:te, 
    .... 
end 

的你可能想要做

X = [ones(te,1), data]; %X is [50x5] so each row of X is x' 
y = X*w; %y is [50x1], X is [50x5], w is [5x1] 
idx_p1 = y==1; %all the indices of y where y is +1 
idx_m1 = y==-1; %all the indicies of y where y is -1 

plot(X(idx_p1,1),y(idx_p1),'rx'); 
plot(X(idx_m1,1),y(idx_m1),'r&'); 

我不知道你是如何使用plot 4维X所以上面的只是地块与X的第一个特征(列)。

此外,培训对我来说看起来很奇怪。首先,我不认为你应该用N来表示数据矩阵的大小和期望的输出。 'yhat'或'ybar'是一个更好的名字。另外,如果N是期望的输出,那么为什么它是size(species,i)其中我通过1:50循环?物种是[150x1]载体。 size(species,1) = 150。并且size(species,x)其中x是2到50将是1.您确定要这样吗?它不应该是这样的:

yhat = -ones(50,1); %Everything is -1 
yhat(strmatch('virginica,species)) = 1; %except virginicas which are +1 
+0

感谢重播 – Mostafa

+0

但我需要训练fisheriris数据 – Mostafa

+0

如何用感知器训练算法做到这一点 – Mostafa