Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

PATTERN RECOGNITION HOME ASSIGNMENT 2

Submitted by: Sagar G Yadav [CB107EC516], Anand Chandran [CB107EC006],


Arun Kumar [CB107EC017]

AIM:
 To classify the Fisher Iris Standard Data Set into three classes namely “setosa”,
“versicolor” and “virginica” using decision boundaries.
 The parameters: Petal Length and Petal Width is used to classify the data set.

MATLAB CODE:

clc;
clear all;
close all;

load fisheriris; % Load Fisher Iris Data %


%implicit columns: Sepal Length, Sepal Width, Petal Length, Petal Width%

setosa_indices = strcmp('setosa',species);
setosa = meas(setosa_indices,:);

versicolor_indices = strcmp('versicolor',species);
versicolor = meas(versicolor_indices,:);

virginica_indices = strcmp('virginica',species);
virginica = meas(virginica_indices,:);

setosaOBS = setosa(:,3:4);
versicolorOBS = versicolor(:,3:4);
virginicaOBS = virginica(:,3:4);

r_setosa = corrcoef(setosaOBS);
r_versicolor = corrcoef(versicolorOBS);
r_virginica = corrcoef(virginicaOBS);

sig_setosa = [std2(setosaOBS(:,1)), std2(setosaOBS(:,2))];


sig_versicolor = [std2(versicolorOBS(:,1)), std2(versicolorOBS(:,2))];
sig_virginica = [std2(virginicaOBS(:,1)), std2(virginicaOBS(:,2))];

cov_setosa = corr2cov(sig_setosa, r_setosa);


cov_versicolor = corr2cov(sig_versicolor, r_versicolor);
cov_virginica = corr2cov(sig_virginica, r_virginica);

cov = cat(3,cov_setosa, cov_versicolor, cov_virginica);

u_setosa = [mean(setosaOBS(:,1)), mean(setosaOBS(:,2))];


u_versicolor = [mean(versicolorOBS(:,1)), mean(versicolorOBS(:,2))];
u_virginica = [mean(virginicaOBS(:,1)), mean(virginicaOBS(:,2))];
centres = [u_setosa; u_versicolor; u_virginica];
priors = [(1/3), (1/3), (1/3)];
nsamples = 50;

gridres = 100;
x_min = 0;
x_max = 7;
y_min = 0;
y_max = 3;
xspace = linspace(x_min, x_max, gridres);
yspace = linspace(y_min, y_max, gridres);

[X Y] = meshgrid(xspace, yspace);
gridpoints = [X(:) Y(:)];
npoints = size(gridpoints, 1);

px_C = zeros(npoints, 2);


normalfact = (2*pi);

for i = 1:3
dist = gridpoints - (ones(npoints,1)*centres(i,:));
Q = chol(cov(:,:,i));
term = dist*inv(Q);
px_C(:,i) = exp(-0.5*sum(term .* term, 2))./(normalfact*prod(diag(Q)));
end

postnum = (ones(npoints, 1)*priors).*px_C;


px = sum(postnum, 2);
posteriors = postnum ./ (px*ones(1,3));

p1_x = reshape(posteriors(:,1), size(X));


p2_x = reshape(posteriors(:,2), size(X));
p3_x = reshape(posteriors(:,3), size(X));
px_1 = reshape(px_C(:,1), size(X));
px_2 = reshape(px_C(:,2), size(X));
px_3 = reshape(px_C(:,3), size(X));

contour(xspace, yspace, px_1);


hold on;
contour(xspace, yspace, px_2);
hold on;
contour(xspace, yspace, px_3);
hold on;
contour(xspace, yspace, p2_x, [(1/3) (1/3) (1/3)], '-.r*');
hold on;
contour(xspace, yspace, p3_x, [(1/3) (1/3) (1/3)], '--go');
hold on;
contour(xspace, yspace, p1_x, [(1/3) (1/3) (1/3)], ':bs');
hold on;
plot(setosaOBS(:,1), setosaOBS(:,2),'k.');
hold on;
plot(versicolorOBS(:,1), versicolorOBS(:,2),'k*');
hold on;
plot(virginicaOBS(:,1), virginicaOBS(:,2),'ko');

hold on;
annotation('textbox',...
[0.167071428571429 0.20952380952381 0.133928571428571
0.0642857142857143],...
'String',{'SETOSA'},...
'FontWeight','bold',...
'FontSize',9,...
'FontName','Arial',...
'FontAngle','italic',...
'LineStyle','none');

annotation('textbox',...
[0.4135 0.266666666666668 0.189285714285714 0.0642857142857143],...
'String',{'VERSICOLOR'},...
'FontWeight','bold',...
'FontSize',9,...
'FontName','Arial',...
'FontAngle','italic',...
'LineStyle','none');

annotation('textbox',...
[0.595642857142856 0.785714285714289 0.158928571428571
0.0642857142857143],...
'String',{'VIRGINICA'},...
'FontWeight','bold',...
'FontSize',9,...
'FontName','Arial',...
'FontAngle','italic',...
'LineStyle','none');

xlabel('PETAL LENGTH -->','fontsize', 8);


ylabel('PETAL WIDTH -->','fontsize', 8);
title('FISHER IRIS DATA CLASSIFICATION USING DECISION
BOUNDARIES','fontsize', 9,'fontweight', 'b');

RESULT:

FISHER IRIS DATA CLASSIFICATION USING DECISION BOUNDARIES


3

VIRGINICA
2.5

2
PETAL WIDTH -->

1.5

VERSICOLOR

0.5 SETOSA

0
0 1 2 3 4 5 6 7
PETAL LENGTH -->

You might also like