1 / 23

Pattern recognition lab 4

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

lucie
Télécharger la présentation

Pattern recognition lab 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pattern recognition lab 4 TA : Nouf Al-Harbi :: nouf200@hotmail.com

  2. 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

  3. 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)

  4. Part 1.A Make a Matlab function that computes the normal distribution for the given values of x, mu, and sigma.

  5. function p = NormalFun(x, mu, sigma) P P(x|w)

  6. 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)

  7. 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).

  8. Part 1.B Make a Matlab function that draws the normal distribution for the given values of mu and sigma.

  9. function DrawNormal(mu, sigma)

  10. 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)

  11. 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);

  12. Part 1.C Make a Matlab function that draws two normal distribution for the given values of mu and sigma.

  13. function DrawTwoNormals(mu1,sigma1,mu2,sigma2)

  14. 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)

  15. 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:');

  16. Part 2 Make a Matlab function that Classifies an input feature value into one of two classes.

  17. Quick review of Bayesian classification

  18. Bayesian Decision Theory Decidex1 if P(1 | x)>P(2 | x) and x2if P(1 | x)<P(2 | x)

  19. function rslt = BayesClassifier(x) W1 or w2

  20. 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

  21. 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

  22. H.W 3 It have been uploaded into the website ..

More Related