/// // Neurovorgud. 7. praktikumi 4. ylesande lahendus // clear; xbasc(); // Genereerime 2 hulga punkte m1 = [0 0]'; cov1 = diag([1 1]); //m1 = [0 0]'; cov1 = diag([1 0.25]); X1 = grand(300, 'mn', m1, cov1); m2 = [1 3]'; cov2 = diag([2 1]); //m2 = [0 2]'; cov2 = diag([0.25 1]); X2 = grand(300, 'mn', m2, cov2); // Normaaljaotuse tihedus function p = gaussian(x, y, m, cov) p = 1/(2*%pi*sqrt(det(cov)))*exp(-1/2*(([x;y]-m)'*inv(cov)*([x;y]-m))); endfunction // Bayesi klassifitseerija klasside jaoks function [o]=g(x,y) if gaussian(x,y,m1,cov1) > gaussian(x,y,m2,cov2) then, o=1, else, o=-1, end endfunction // Plottime eraldusjoont t=[-4:0.1:7]; fgrayplot(t,t,g); // Teine viis, mis kahjuks monikord failib // fcontour2d(t,t,g,2,style=1); // ... paneme punktid ka //xset("mark size", 3); xset("color", 2); // Blue plot2d(X1(1,:)', X1(2,:)', style=-5); xset("color", 5); // Red plot2d(X2(1,:)', X2(2,:)', style=-6); // Hindame bayesi riski errors = 0; for p = X1 if (g(p(1), p(2)) == -1) then errors = errors + 1; end end for p = X2 if (g(p(1), p(2)) == 1) then errors = errors + 1; end end n = size(X1, 'c') + size(X2, 'c'); printf("Classifier risk is %f\n", errors/n);