Question
*I need help on how to run this I have the saved data but I cant figure which parameters to invoke* function [accuracy] = lab2ldf(TrainingFactor,
*I need help on how to run this I have the saved data but I cant figure which parameters to invoke*
function [accuracy] = lab2ldf(TrainingFactor, set1, set2,a,nu) %Function can select Datasets A and B, A and C, or B and C % 'a' is the initial weight vector, 'nu' is learning rate load irisdata.mat
%Initialize Dataset A, B, and C A=irisdata_features(1:50,2:3); B=irisdata_features(51:100,2:3); C=irisdata_features(101:150,2:3);
%Determine Training Set #1 if set1 == 'A' Dataset1= A; elseif set1 == 'B' Dataset1 = B; elseif set1 == 'C' Dataset1 = C; end
%Determine Training Set #2 if set2 == 'A' Dataset2= A; elseif set2 == 'B' Dataset2 = B; elseif set2 == 'C' Dataset2 = C; end
trainSize=TrainingFactor*50; testSize=50-trainSize;
Train1=Dataset1(1:trainSize,1:2); Train2=Dataset2(1:trainSize,1:2);
Test1=Dataset1((trainSize+1):50,1:2); Test2=Dataset2((trainSize+1):50,1:2);
%Construct y=[1 x2 x3]' and normalize it y=ones(3,trainSize*2); y(2:3,1:trainSize)=Train1'; y(2:3,(trainSize+1):trainSize*2)=(-1)*Train2'; y(1,(trainSize+1):trainSize*2) = (-1)*y(1,(trainSize+1):trainSize*2);
DJp = zeros(3,1); g=(a')*y; iterations=[1:1:300]; perceptron=zeros(3,trainSize);
for i=1:300 count=0; for j=1:2*trainSize if g(j) <= 0 DJp=DJp-y(1:3,j); count=count+1; %Counts the number of misclassifications end end perceptron(1:3,i)=DJp; missrate=(count/trainSize)*100; a=a-nu*DJp; g=a'*y; if (abs(nu*DJp) <= 0) break; end end
figure; plot(iterations,perceptron(1,:)); hold on; plot(iterations,perceptron(2,:)); hold on; plot(iterations,perceptron(3,:)); hold on; grid; xlabel('Number of Iterations'); ylabel('Perceptron Criterion DJp(a)'); legend('wo','w1','w2');
disp('Number of iterations to convergence'); i disp('Misclassification rate at final iteration'); missrate
%Reconstruct matrix y so it contains test data instead of traning data y=ones(3,testSize*2); y(2:3,1:testSize)=Test1'; y(2:3,(testSize+1):testSize*2)=(-1)*Test2'; y(1,(testSize+1):testSize*2) = (-1)*y(1,(testSize+1):testSize*2); g=(a')*y;
count=0; %Find the number of misclassifications using the test data for i=1:2*testSize if g(i) <= 0 count=count+1; end end
accuracy=((testSize-count)/testSize)*100;
disp('Classification accuracy for test data'); accuracy
disp('Final Weight Vector a'); a
disp('Slope m of boundary line'); m=-a(2,1)/a(3,1)
disp('Y inercept b of boundary line'); b=-a(1,1)/a(3,1)
figure; fplot (@(x)m*x+b, [0 6]); hold; for i=1:trainSize scatter ( Train2 (i ,1) ,Train2 (i ,2) ,10 ,[0 0 1],'*') ;% blue scatter ( Train1 (i ,1) ,Train1 (i ,2) ,10, [1 0 0],'o') ;% Red end
if set1 == 'A' && set2 == 'B' legend('Boundary Line','Iris Setosa','Iris Versicolor'); end if set1 == 'B' && set2 == 'C' legend('Boundary Line','Iris Versicolor','Iris Virginica'); end if set1 == 'A' && set2 == 'C' legend('Boundary Line','Iris Setosa','Iris Virginica'); end
xlabel('Sepal Width (cm)'); ylabel('Petal Length (cm)'); hold on; grid;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started