2012-05-18 36 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