.mu and .su
3 views (last 30 days)
Show older comments
Jon Camilleri
on 23 Nov 2015
Commented: Walter Roberson
on 23 Nov 2015
What do lines 41-42 mean?
% ICS5110 - Applied Machine Learning
% University of Malta
% Lecturer: Dr. George Azzopardi
% Date: 27 October, 2015
function accuracy = NaiveBayesIris(L2norm)
load('irisData.mat');
load('irisLabels.mat');
% Create a random permutation
if exist('randpermlist.mat')
load('randpermlist.mat');
else
randpermlist = randperm(numel(irisLabels));
save randpermlist randpermlist;
end
if L2norm
irisData = normr(irisData);
end
% Split data set into 50% training and 50% testing
ntraining = floor(0.5*numel(irisLabels));
trainingData = irisData(randpermlist(1:ntraining),:);
trainingLabels = irisLabels(randpermlist(1:ntraining));
testingData = irisData(randpermlist(ntraining+1:end),:);
testingLabels = irisLabels(randpermlist(ntraining+1:end));
% Prior class probabilities
uniqueClasses = unique(trainingLabels);
[classidx,classlbl] = grp2idx(trainingLabels);
h = hist(classidx,numel(uniqueClasses));
prior = h./sum(h);
% Likelihood
likelihood.mu = zeros(numel(uniqueClasses),size(trainingData,2)); _/% explanation required_
likelihood.su = zeros(numel(uniqueClasses),size(trthainingData,2)); /% explanation required
for i = 1:numel(uniqueClasses)
idx = find(classidx == i);
likelihood.mu(i,:) = mean(trainingData(idx,:));
likelihood.su(i,:) = std(trainingData(idx,:));
end
% Classification
for i = 1:size(testingData,1)
for j = 1:numel(uniqueClasses)
% Guassian Function Kernel
squaredDifference = (testingData(i,:) - likelihood.mu(j,:)).^2;
normFactor = 1./(sqrt(2*pi)*likelihood.su(j,:));
likelihood.prob = normFactor .* exp(-squaredDifference/(2.*(likelihood.su(j,:).^2)));
%posterior(j) = prod(likelihood.prob) * prior(j);
posterior(j) = sum(log(likelihood.prob)) + log(prior(j));
end
[mx,mxind] = max(posterior);
predictedLabel(i) = classlbl(mxind);
end
accuracy = sum(strcmp(predictedLabel',testingLabels))/numel(testingLabels);
0 Comments
Accepted Answer
Walter Roberson
on 23 Nov 2015
2 Comments
Walter Roberson
on 23 Nov 2015
The mu are means of each class and the su are standard deviations of each class.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!