230 likes | 346 Views
Pattern recognition lab 4. TA : Nouf Al-Harbi :: nouf200@hotmail.com. Lab objective:. Illustrate examples on the normal distribution by making the following functions : compute the normal distribution draw the normal distribution draw two normal distribution
E N D
Pattern recognition lab 4 TA : Nouf Al-Harbi :: nouf200@hotmail.com
Lab objective: • Illustrate examples on the normal distribution by making the following functions : • compute the normal distribution • draw the normal distribution • draw two normal distribution • Appling Bayesian Classification • Classify an input feature value into one of two classes
Function in Matlab .. • M-files divided into 2 kinds: • Scripts which do not accept input arguments or return output arguments. • Functions which can accept input arguments and return output arguments. • Function outputArg=funName(inputArg1,inputArg2)
Part 1.A Make a Matlab function that computes the normal distribution for the given values of x, mu, and sigma.
function p = NormalFun(x, mu, sigma) P P(x|w)
Appling part 1.A Practical Learning 1 • Make a Matlab function that computes the normal distribution for the given values of x, mu, and sigma. • Then use it to compute the value of likelihood at x = 15 If you know that : P(x|w) ≈N(20,3)
Full code • In M-File : • function p=NormalFun(x,mu,sigma) • p=(1/sqrt(2*pi*sigma))*exp(-(x-mu)^2/(2*sigma)); • To use the above function: • save it in an m-file with same name as the function name • issue the following command: • >> normalfn(18,20,3) • This will return the value of N(20,3) at x = 18. (p= 0.1183) • (Note that this is the likelihood P(X|W) at x = 18 for the class that has mu = 20 and sigma =3).
Part 1.B Make a Matlab function that draws the normal distribution for the given values of mu and sigma.
Appling part 2 Practical Learning 2 • Make a Matlab function that draws the normal distribution for the given values of mu, and sigma. • Then use it to draw P(x|w) ≈N(20,3)
Full code • function DrawNormal(mu,sigma) • xmin = mu- 4 * sigma; • xmax = mu + 4 * sigma; • x = xmin: 0.1 : xmax; • n = length(x); • p = zeros(1,n); • for i = 1:n; • p(i) = normalfn(x(i),mu,sigma); • end • plot(x,p);
Part 1.C Make a Matlab function that draws two normal distribution for the given values of mu and sigma.
Appling part 3 Practical Learning 3 • Make a Matlab function that draws 2 normal distributions for the given values of mu, and sigma. • Then use it to draw P(x|w1) ≈N(20,3) P(x|w2) ≈N(40,5)
Full code • functionDrawTwoNormals(mu1,sigma1,mu2,sigma2) • xmin1 = mu1- 5 * sigma1; • xmax1 = mu1 + 5 * sigma1; • xmin2 = mu2- 5 * sigma2; • xmax2 = mu2 + 5 * sigma2; • x = min(xmin1,xmin2): 0.1 : max(xmax1,xmax2); • n = length(x); • p1 = zeros(1,n); • p2 = zeros(1,n); • fori = 1:n; • p1(i) = normalfn(x(i),mu1,sigma1); • p2(i) = normalfn(x(i),mu2,sigma2); • end • plot(x,p1, 'r-', x,p2, 'b:');
Part 2 Make a Matlab function that Classifies an input feature value into one of two classes.
Bayesian Decision Theory Decidex1 if P(1 | x)>P(2 | x) and x2if P(1 | x)<P(2 | x)
function rslt = BayesClassifier(x) W1 or w2
Appling part 1 Practical Learning 1 • Make a Matlab function that classify a feature value into one of two classes w1,w2 that have these properties: • P(x|w1) ≈N(20,3) • P(x|w2) ≈N(30,2) • P(w1)=1/3 • P(w2)=2/3 • Then use it to classify the inputs value x=13 x=34
Full code • function rslt = BayesClassifier (x) • Pw1 = 1/3; % P(w1) • Pw2 = 2/3; % P(w2) • mu1 = 20; • sigma1 = 2; • mu2 = 30; • sigma2 = 2; • Pxw1 = NormalFun( x, mu1, sigma1); • Pxw2 = NormalFun( x, mu2, sigma2); • Px = Pxw1 * Pw1 + Pxw2 * Pw2; • Pw1x = Pxw1 * Pw1/ Px; • Pw2x = Pxw2 * Pw2/ Px; • if (Pw1x > Pw2x) • rslt = sprintf('\n%d belongs to w1, with an error = %d\n', x, Pw2x); • else • rslt = sprintf('\n%d belongs to w2, with an error = %d\n', x, Pw1x); • end
H.W 3 It have been uploaded into the website ..