-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnaivebayes.m
30 lines (30 loc) · 978 Bytes
/
naivebayes.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
load fisheriris
X = meas(:,3:4);
Y = species;
tabulate(Y)
Mdl = fitcnb(X,Y,'ClassNames',{'setosa','versicolor','virginica'})
setosaIndex = strcmp(Mdl.ClassNames,'setosa');
estimates = Mdl.DistributionParameters{setosaIndex,1}
figure
gscatter(X(:,1),X(:,2),Y);
h = gca;
cxlim = h.XLim;
cylim = h.YLim;
hold on
Params = cell2mat(Mdl.DistributionParameters);
Mu = Params(2*(1:3)-1,1:2); % Extract the means
Sigma = zeros(2,2,3);
for j = 1:3
Sigma(:,:,j) = diag(Params(2*j,:)).^2; % Create diagonal covariance matrix
xlim = Mu(j,1) + 4*[-1 1]*sqrt(Sigma(1,1,j));
ylim = Mu(j,2) + 4*[-1 1]*sqrt(Sigma(2,2,j));
f = @(x,y) arrayfun(@(x0,y0) mvnpdf([x0 y0],Mu(j,:),Sigma(:,:,j)),x,y);
fcontour(f,[xlim ylim]) % Draw contours for the multivariate normal distributions
end
h.XLim = cxlim;
h.YLim = cylim;
title('Naive Bayes Classifier -- Fisher''s Iris Data')
xlabel('Petal Length (cm)')
ylabel('Petal Width (cm)')
legend('setosa','versicolor','virginica')
hold off