PATTERN RECOGNITION LAB 4 TA Nouf AlHarbi nouf
PATTERN RECOGNITION LAB 4 TA : Nouf Al-Harbi : : nouf 200@hotmail. com
Lab objective: 2 1. Illustrate examples on the normal distribution by making the following functions : A. B. C. 2. compute the normal distribution draw two normal distribution Appling Bayesian Classification A. Classify an input feature value into one of two classes
Function in Matlab. . 3 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 output. Arg=fun. Name(input. Arg 1, input. Arg 2)
4 Part 1. A Make a Matlab function that computes the normal distribution for the given values of x, mu, and sigma.
function p = Normal. Fun(x, mu, sigma) 5 X Mu Sigma Normal. Fun P P(x|w)
Appling part 1. A 6 � 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 7 In M-File : function p=Normal. Fun(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 Draw. Normal(mu, sigma) Mu Draw. Normal Sigma
Appling part 2 10 � 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 11 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. function Draw. Normal(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.
function Draw. Two. Normals(mu 1, sigma 1, mu 2, sigma 2) 13 sigma 1 mu 2 Draw Two Normals sigma 2
Appling part 3 14 � 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|w 1) ≈N(20, 3) P(x|w 2) ≈N(40, 5)
Full code 15 1. function Draw. Two. Normals(mu 1, sigma 1, mu 2, sigma 2) 2. xmin 1 = mu 1 - 5 * sigma 1; 3. xmax 1 = mu 1 + 5 * sigma 1; 4. xmin 2 = mu 2 - 5 * sigma 2; 5. xmax 2 = mu 2 + 5 * sigma 2; 6. x = min(xmin 1, xmin 2): 0. 1 : max(xmax 1, xmax 2); 7. n = length(x); 8. p 1 = zeros(1, n); 9. p 2 = zeros(1, n); 10. for i = 1: n; 11. p 1(i) = normalfn(x(i), mu 1, sigma 1); 12. p 2(i) = normalfn(x(i), mu 2, sigma 2); 13. end 14. plot(x, p 1, 'r-', x, p 2, '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
Bayesian Decision Theory 18 Decide and x 1 if P( 1 | x) > P( 2 | x) x 2 if P( 1 | x) < P( 2 | x)
19 function rslt = Bayes. Classifier(x) X Bayes. Classifi er W 1 or w 2
Appling part 1 20 � Make a Matlab function that classify a feature value into one of two classes w 1, w 2 that have these properties: Practical Learning 1 � P(x|w 1) ≈N(20, 3) P(x|w 2) ≈N(30, 2) P(w 1)=1/3 P(w 2)=2/3 Then use it to classify the inputs value x=13 x=34
Full code 21 function rslt = Bayes. Classifier (x) Pw 1 = 1/3; % P(w 1) Pw 2 = 2/3; % P(w 2) mu 1 = 20; sigma 1 = 2; mu 2 = 30; sigma 2 = 2; Pxw 1 = Normal. Fun( x, mu 1, sigma 1); Pxw 2 = Normal. Fun( x, mu 2, sigma 2); Px = Pxw 1 * Pw 1 + Pxw 2 * Pw 2; Pw 1 x = Pxw 1 * Pw 1/ Px; Pw 2 x = Pxw 2 * Pw 2/ Px; if (Pw 1 x > Pw 2 x) rslt = sprintf('n%d belongs to w 1, with an error = %dn', x, Pw 2 x); else rslt = sprintf('n%d belongs to w 2, with an error = %dn', x, Pw 1 x); end
22
23 q H. W 3 It have been uploaded into the website. .
- Slides: 23