% this is for two class problem for more than two class code changes
% % this is for two class problem for more than two class code changes
% % ————-Parameters————-
% numIteration =1000; The Number of maximum iterations
% % errorBound = 0.0001; This is the permissible error.
% The experiments have been done keep in view both the error condition
% reached or maximum iteration reached whichever comes first.
% % eta = 0.5; This is the learning rate, experiments have been
% done on various learning rates
function logisticRegression2Class()
disp(‘..Starting logistic regression Algorithm….’);
%reading the data A = load('BananaData.mat'); data = A.data; [N,col]= size(data); vtot=[0, 0, 0, 0,0, 0, 0 , 0]; %5 folds with 70-30 ratio
for i = 1:5
P=.3; groups=data(:,3); [train,test] = crossvalind('holdout',groups, P); train1= data(train, 1: 3); test1=data(test, 1:3); [trainLengthRow, trainLengthCol]=size(train1); [rowtest,coltest]= size(test1); trainingSet = train1(1:trainLengthRow, 1 : trainLengthCol -1 ); trainingLabels = train1(1:trainLengthRow, trainLengthCol ); testSet = test1(1:rowtest, 1 : coltest -1 ); testLabels = test1(1:rowtest, coltest ); %initilizating weights weights(1:trainLengthCol) = 0; weight0=0;
[weight0, weights] = trainLogReg(weight0, weights, trainingSet,trainingLabels);
[correctlyClassified,count0,count1,unClassified,v] = testLogReg(testSet,testLabels, weight0, weights)
vtot = vtot +v ;
end
disp(‘TP, TN, FP, FN, TP/(TP+FP), TP/P, 2PR / (P+R) , correctlyClassified/trainLengthRow’);
vtot = vtot ./ 5
end
%This mathod is for tarining the logestic regression problem
% —–Parameters—-
%trainingSet: the training set
%trainingLabels: the labels corresponding to the traiining set
%weights: the initial weights obtained from traiining
%weight0: The initial bias weight
%—–Return Types——
%weights: the final weights obtained from traiining
%weight0: The bias weight
%
function [weight0, weights] = trainLogReg(weight0, weights, trainingSet,trainingLabels)
numIteration =100000; eta = 0.5; errorBound = 0.0001; error =1.0; [trainLengthRow, trainLengthCol] = size(trainingSet); del_l_by_del_w_i(1:trainLengthCol) = 0; weightsFinal(1:trainLengthCol) = 0; k=0 while ((k < numIteration) && (error > errorBound)) error=0.0; for i=1:trainLengthCol Y1_X = 0; del_l_by_del_w_i(i) = 0; del_l_by_del_w_0 = 0; for t=1: trainLengthRow sum = weight0; for j=1: trainLengthCol sum = sum + weights(j)*trainingSet(t,j); end; Y1_X = 1/(1+ exp(-1*sum)); del_l_by_del_w_i(i) = del_l_by_del_w_i(i) + trainingSet(t,i) *(trainingLabels(t) - Y1_X ) ; del_l_by_del_w_0 = del_l_by_del_w_0 + 1 *(trainingLabels(t) - Y1_X ) ; end; end; for i=1:trainLengthCol weightsFinal(i)= weights(i) + eta * del_l_by_del_w_i(i); error = error + (weightsFinal(i)-weights(i))*(weightsFinal(i)-weights(i)); end; weight0new = weight0 + eta * del_l_by_del_w_0; error = error + (weight0new-weight0)*(weight0new-weight0); error=sqrt(error); weights=weightsFinal; weight0 = weight0new; k=k+1; end k %Now computing the final y using the final weights y1(1:trainLengthRow)=0; y0(1:trainLengthRow)=0; for i =1: trainLengthRow sum = weight0; for j=1: trainLengthCol sum = sum + weightsFinal(j)*trainingSet(i,j); end; y1(i) = 1/(1+ exp(-1*sum)); y0(i) = 1/(1+ exp(sum)); end;
% Following is the code for plotting the data
% data(1:trainLengthRow, 1:trainLengthCol+1)=0;
% data(1:trainLengthRow, 1:trainLengthCol)= trainingSet;
% for p=1:trainLengthRow
% data(p, trainLengthCol+1)= y1(p);
% end;
%
% %figure
% % parallelcoords(data,’Labels’,labels);
%
% for p=1:trainLengthRow
% x1(p)= trainingSet(p,1);
% end;
%
% for p=1:trainLengthRow
% x2(p)= trainingSet(p,2);
% end;
%
%
% for p=1:trainLengthRow
% yOrginal(p)= trainingLabels(p);
% end;
%
%
% size(x1)
% size(x2)
% size(trainingLabels)
%
% figure
% scatter3(x1,x2,trainingLabels,10);
% axis([-10,10,-10,10,-10,10])
%
% figure
% plot3(x1,x2,y1);
% axis([-10,10,-10,10,-10,10])
%
%
% xx=[-10:1:10];
% yy=[-10:1:10];
% [xx1,yy1]=meshgrid(xx,yy);
%
% sum = -1 .* (weight0+weightsFinal(1).xx1+weightsFinal(2).yy1);
% zz= 1 ./(1 + expm(sum));
% figure
% surf(xx1,yy1,zz);
% title(‘title’);
% xlabel(‘x’);
% ylabel(‘y’)
% zlabel(‘z’);
end
%This is the method that is called to test the accuracy of the methods
%———————–Parameters————————–
%testSet: the set of samples to be considered for testing
%testLabels: the labels corresponding to testset
%weight0, weight: the weights corresponding to logistic regression
%———————–Return Values————————
%correctlyClassified: The number of correctly classified samples
%unClassified: The array containing 5 unclassified data samples from each
%classification type
%v: The vecor that returns the computed values of TP;TN; FP; FN ,P; R; F, accuracy
function [correctlyClassified,count0,count1,unClassified,v] = testLogReg(testSet,testLabels, weight0, weights)
correctlyClassified = 0; count0 = 0; count1=0; TP=0; TN=0; FP=0; FN =0; P=0; R=0; F=0; [testLengthRow,testLengthCol]=size(testSet); unClassified(1:10 ,1: testLengthCol) = 0; % checking accuracy by number of correctly classified for k=(1: testLengthRow ) x=[1, testSet(k,1:testLengthCol)]; w =[weight0,weights]; O1= x' .* w' ; %computing the value of vector with plane sum =0; for p=1:length(O1) sum = sum +O1(p); end y1x = 1/(1+ exp(-1*sum)); if(y1x>=0.5) %disp('class 1'); O =1; else %disp('class 0'); O =-1; end % error as output approaching target if (O == testLabels(k)) % correctly classified examples correctlyClassified=correctlyClassified+1; %compute TP, TN if(testLabels(k)==1) TP = TP+1; else TN = TN +1; end else % wrongly classified examples if(testLabels(k)==1) FN = FN+1; else FP = FP +1; end %storing 5 misclassified classes from each class if(count1<5 && testLabels(k)==1) count1 = count1 + 1; unClassified(count1,1: testLengthCol) = testSet(k,1: testLengthCol); end if(count0<5 && testLabels(k)==-1 ) count0 = count0 + 1; unClassified(count0,1: testLengthCol) = testSet(k,1: testLengthCol); end end end k P= TP/(TP+FP) R= TP/(TP+FN) v=[TP, TN, FP, FN, P, R, 2*P*R / (P+R) , correctlyClassified/testLengthRow] disp('TP, TN, FP, FN, TP/(TP+FP), TP/P, 2*P*R / (P+R) , correctlyClassified/trainLengthRow'); unClassified; accuracy = correctlyClassified/testLengthRow ; accuracy
end
You must log in to post a comment.