[LON-CAPA-cvs] cvs: modules /minaeibi c183_10way_3Class.m

minaeibi lon-capa-cvs@mail.lon-capa.org
Tue, 03 Sep 2002 13:16:39 -0000


This is a MIME encoded message

--minaeibi1031058999
Content-Type: text/plain

minaeibi		Tue Sep  3 09:16:39 2002 EDT

  Added files:                 
    /modules/minaeibi	c183_10way_3Class.m 
  Log:
  MATLAB CODE FOR NON-TREE 
  classifiers 3 classes
  
  
--minaeibi1031058999
Content-Type: text/plain
Content-Disposition: attachment; filename="minaeibi-20020903091639.txt"


Index: modules/minaeibi/c183_10way_3Class.m
+++ modules/minaeibi/c183_10way_3Class.m
%This program compares the error rates of PHY183 data by using Byaes, 1NN, Knn, MLP, and Parzen Classifiers.
close all;
clear all;

folder=10;
l_col=8;
ClassNo=3;
load f183.txt;

dataf=f183;
k1=find(dataf(:,l_col)==1);
k2=find(dataf(:,l_col)==2);
k3=find(dataf(:,l_col)==3);
First_Column=1;
Second_Column=6;
Feature_no=Second_Column-First_Column+1;
k_knn=3;%floor(sqrt(Feature_no));

B=[];
nc1=size(k1,1);
nc2=size(k2,1);
nc3=size(k3,1);

for i=1:nc1
    B=[B;dataf(k1(i),:)]; 
end

for i=1:nc2
    B=[B;dataf(k2(i),:)];
end

for i=1:nc3
    B=[B;dataf(k3(i),:)];
end

index1=randperm(nc1);
index2=randperm(nc2)+nc1;
index3=randperm(nc3)+nc1+nc2;

nn1=floor(nc1/folder);
nn_1=nc1-nn1;
nn2=floor(nc2/folder);
nn_2=nc2-nn2;
nn3=floor(nc3/folder);
nn_3=floor(size(k3,1)-size(k3,1)/folder);
nn_3=nc3-nn3;

nn_test=nn1+nn2+nn3;
nn_train=nn_1+nn_2+nn_3;

lab_test = [ones(1,nn1) ones(1,nn2)*2 ones(1,nn3)*3];
lab_train = [ones(1,nn_1) ones(1,nn_2)*2 ones(1,nn_3)*3];

target_train = [repmat([1 0 0],nn_1,1); repmat([0 1 0],nn_2,1); repmat([0 0 1],nn_3,1)];
target_test = [repmat([1 0 0],nn1,1); repmat([0 1 0],nn2,1); repmat([0 0 1],nn3,1)];

Column_No = Second_Column-First_Column + 1;
data = B(:,First_Column:Second_Column);

%Normalize the data
for k=1:Column_No
    data(:,k)=(data(:,k)-mean(data(:,k)))/std(data(:,k));
end

test = zeros(nn_test,Column_No);
train = zeros(nn_train,Column_No);

round_err_bayes = 0;
round_err_1nn = 0;
round_err_knn = 0;
round_err_parzen = 0;
round_err_mlp = 0;
round_err_cmc = 0;
round_err_oracle = 0;

index=randperm(size(B,1));

for round = 1:folder
    train1=[];train2=[];train3=[];
    %Randomly separate each class to training and testing set.
    for i=1:nn1
        idx=index1(i+(round-1)*nn1);
        test1(i,:)=data(idx,:);
        test_index1(i)=idx;
    end
    for i=1:nn2
        idx=index2(i+(round-1)*nn2);
        test2(i,:)=data(idx,:);
        test_index2(i)=idx;
    end
    for i=1:nn3
        idx=index3(i+(round-1)*nn3);
        test3(i,:)=data(idx,:);
        test_index3(i)=idx;
    end
    
    for i=1:(round-1)*nn1
        idx=index1(i);
        train1(i,:)=data(idx,:);
        train_index1(i)=idx;
    end        
    for i=(round*nn1)+1:nc1
        idx=index1(i);
        train1=[train1;data(idx,:)];
        train_index1(i)=idx;
    end        
    
    for i=1:(round-1)*nn2
        idx=index2(i);
        train2(i,:)=data(idx,:);
        train_index2(i)=idx;
    end        
    for i=(round*nn2)+1:(nc2)
        idx=index2(i);
        train2=[train2;data(idx,:)];
        train_index2(i)=idx;
    end        

    for i=1:(round-1)*nn3
        idx=index3(i);
        train3(i,:)=data(idx,:);
        train_index3(i)=idx;
    end        
    for i=(round*nn3)+1:(nc3)
        idx=index3(i);
        train3=[train3;data(idx,:)];
        train_index3(i)=idx;
    end        

    train=[train1;train2;train3];
    test=[test1;test2;test3];
    
    test_index(1:nn1)=test_index1(1:nn1);
    test_index(nn1+1:nn1+nn2)=test_index2(1:nn2);
    test_index(nn1+nn2+1:nn1+nn2+nn3)=test_index3(1:nn3);
    
    %Calculate Sample mean and sample covariance.
    m_1 = mean(train1); v_1 = cov(train1);
    m_2 = mean(train2); v_2 = cov(train2);
    m_3 = mean(train3); v_3 = cov(train3);
    
    %Calculate discriminant functions for every testing samples and count error classifications.
%    k_knn=2;
    error_bayes = 0;
    error_1nn = 0;
    error_knn = 0;
    error_mlp = 0;
    error_parzen = 0;
    error_oracle = 0;
    error_cmc = 0;
    
    %Get the 1nn classification result in eachClass
    [eachClass1, nearestSampleIndex, knnmat] = knn([train lab_train'], [test lab_test'], 1);
  
    %Get the knn classification result in eachClass
    [eachClass, nearestSampleIndex, knnmat] = knn([train lab_train'], [test lab_test'], k_knn);
    clear nearestSampleIndex; clear knnmat;

    %Get the Parzen Window classification result in eachClass
    
    %[m,s,p,sig]=fit_sphere(train, target);
    % sigma=trainparzen(train,target,max(sig),1,max(sig)/8);
    class=flagmax(parzen_classify(test,train,target_train,.1));
    
    %Get the MLP classification result in eachClass
    [w,bias,error]=trainmlp(train,target_train,[3 3],0.01);
    out = flagmax(mlp(test, w, bias));
    clear w; clear bias; clear error;
    
    for i = 1:size(test,1)
        % Bayes decision rule
        x = test(i,:);
        g(1) = (x-m_1)*(-0.5)*inv(v_1)*(x-m_1)'-0.5*log(det(v_1))+log(size(test1,1)/size(test,1));
        g(2) = (x-m_2)*(-0.5)*inv(v_2)*(x-m_2)'-0.5*log(det(v_2))+log(size(test2,1)/size(test,1));            
        g(3) = (x-m_3)*(-0.5)*inv(v_3)*(x-m_3)'-0.5*log(det(v_3))+log(size(test3,1)/size(test,1));
        [C,I] = max([g(1) g(2) g(3)]);
        
        flag(1)=0;flag(2)=0;flag(3)=0;flag(4)=0;flag(5)=0;
        % Calculate error for Bayes
        if I~=B(test_index(i),l_col)
            error_bayes = error_bayes + 1;
            flag(1)=1;
        end   %if
            
        % Calculate error for 1NN
        if (eachClass1(i))~=B(test_index(i),l_col)
            error_1nn = error_1nn + 1;
            flag(2)=1;
        end   %if            
        
        % Calculate error for kNN
        if (eachClass(i))~=B(test_index(i),l_col)
            error_knn = error_knn + 1;
            flag(3)=1;
        end   %if            
        
        % Calculate error for parzen
        if (sum(target_test(i,:)==class(i,:))~=size(class,2))
            error_parzen = error_parzen + 1;
            flag(4)=1;
        end   %if            
        
        % Calculate error for MLP
        if (sum(target_test(i,:)==out(i,:))~=size(class,2))
            error_mlp = error_mlp + 1;
            flag(5)=1;
        end   %if
    
        s_flag=sum(flag);
        if(s_flag>3)
            error_cmc=error_cmc+1;
            if(s_flag==5)
                error_oracle=error_oracle+1;
            end %if
        end %if
        
    end   %for
    
    % Calculate error rate for plug-in
    error_rate_bayes(round) = error_bayes/size(test,1); round_err_bayes = round_err_bayes + error_rate_bayes(round);
    
    % Calculate error rate for 1nn
    error_rate_1nn(round) = error_1nn/size(test,1);round_err_1nn = round_err_1nn + error_rate_1nn(round);

    % Calculate error rate for knn
    error_rate_knn(round) = error_knn/size(test,1);round_err_knn = round_err_knn + error_rate_knn(round);

    % Calculate error rate for parzen
    error_rate_parzen(round) = error_parzen/size(test,1); round_err_parzen = round_err_parzen + error_rate_parzen(round);

    % Calculate error rate for mlp
    error_rate_mlp(round) = error_mlp/size(test,1); round_err_mlp = round_err_mlp + error_rate_mlp(round);

    % Calculate error rate for cmc
    error_rate_cmc(round) = error_cmc/size(test,1); round_err_cmc = round_err_cmc + error_rate_cmc(round);

    % Calculate error rate for oracle
    error_rate_oracle(round) = error_oracle/size(test,1); round_err_oracle = round_err_oracle + error_rate_oracle(round);

end   %round

avg_err_bayes = round_err_bayes/folder; std_dev_bayes = std(error_rate_bayes);
avg_err_1nn = round_err_1nn/folder; std_dev_1nn = std(error_rate_1nn);
avg_err_knn = round_err_knn/folder; std_dev_knn = std(error_rate_knn);
avg_err_parzen = round_err_parzen/folder; std_dev_parzen = std(error_rate_parzen);
avg_err_mlp = round_err_mlp/folder; std_dev_mlp = std(error_rate_mlp);
avg_err_cmc = round_err_cmc/folder; std_dev_cmc = std(error_rate_cmc);
avg_err_oracle = round_err_oracle/folder; std_dev_oracle = std(error_rate_oracle);


fprintf('\n\n=======================================================\n');
fprintf('Bayes\t%5.4f\t%5.4f\n', avg_err_bayes, std_dev_bayes );
fprintf('1NN\t%5.4f\t%5.4f\n', avg_err_1nn, std_dev_1nn );
fprintf('KNN\t%5.4f\t%5.4f\n', avg_err_knn, std_dev_knn );
fprintf('Parzen\t%5.4f\t%5.4f\n', avg_err_parzen, std_dev_parzen );
fprintf('MLP\t%5.4f\t%5.4f\n', avg_err_mlp, std_dev_mlp );
fprintf('CMC\t%5.4f\t%5.4f\n', avg_err_cmc, std_dev_cmc );
fprintf('Oracle\t%5.4f\t%5.4f\n', avg_err_oracle, std_dev_oracle );
%fprintf('KNN Performance = %5.2f%%\


plot(error_rate_bayes,'k','Marker','s');
hold on;
plot(error_rate_1nn,'m','LineStyle','-');
hold on;
plot(error_rate_knn,'r','Marker','*');
hold on;
plot(error_rate_parzen,'b','LineStyle',':');
hold on;
plot(error_rate_mlp,'g','Marker','o');
hold on;
plot(error_rate_cmc,'k','Marker','v');
hold on;
plot(error_rate_oracle,'k','Marker','.');

legend('Bayes','1-NN','K-NN','Parzen','MLP','CMC','Oracle');
xlabel('Test no. in 10-fold Cross-Validation');
ylabel('Error Rate');
title('LON-CAPA: Comparison of classifiers on PHY183 Data, 10-fold CV (3 Classes)');
--minaeibi1031058999--