Attachment 'sheet04.m'
Download 1 function sheet04
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % DO NOT EDIT STARTING HERE, EDIT ONLY BELOW
4
5 % Generate some data.
6 [X, Y] = generate_data(1000, 500);
7
8 % and test data
9 [XE, YE] = generate_data(2000, 1000);
10
11 plot_data(X, Y);
12
13 % estimate mean and covariance function for the two classes
14 [C.MP, C.CP] = ml_estimate(X, Y, 1);
15 [C.MN, C.CN] = ml_estimate(X, Y, -1);
16
17 % estimate the class priors
18 C.PP = prior_estimate(X, Y, 1);
19 C.PN = prior_estimate(X, Y, -1);
20
21 C
22
23 % predict class labels for the training and test data
24 YH = predict(C, X);
25 YEH = predict(C, XE);
26
27 % display errors
28 fprintf('training error: %.2f%%\n', mean(YH ~= Y)*100);
29 fprintf('test error: %.2f%%\n', mean(YEH ~= YE)*100);
30
31 % plot decision function
32 [MX, MY] = meshgrid(linspace(-20, 20, 30), linspace(-10, 20, 30));
33 Z = posterior_grid(C, MX, MY, 1);
34
35 hold on;
36 contour(MX, MY, Z, [0.5]);
37 colormap([0 0 0]);
38 hold off;
39
40 function [X, Y] = generate_data(n1, n2)
41 X = [randn(n1, 2)*diag([1, 2])*rotmat(pi/4) + repmat([-7, 0], n1, 1);...
42 randn(n2, 2)*diag([3,4])*rotmat(3*pi/4) + repmat([4, 4], n2, 1)];
43 Y = [ones(n1, 1); -ones(n2, 1)];
44
45 function R = rotmat(phi)
46 R = [cos(phi), sin(phi); -sin(phi), cos(phi)];
47
48 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 % EDIT BELOW
50
51 % 1. Plot the data set by plotting the class for Y = 1 with red crosses and
52 % the class with Y == -1 with blue circles. Make sure axes are equally
53 % scaled and add a grid (5 Punkte).
54 function plot_data(X, Y)
55 % ...
56
57 % 2. Estimate the mean and covariance using the ml-estimator for
58 % multivariate Gaussians for the given class (5 Punkte).
59 function [M, C] = ml_estimate(X, Y, class)
60 % ...
61
62 % 3. Estimate the class priors for the given class (5 Punkte)
63 function P = prior_estimate(X, Y, class)
64 % ...
65
66 % 4. Compute the multivariate density (5 Punkte)
67 function P = gaussian(M, C, X)
68 % ...
69
70 % 5. Predict the more likely class based on the class posterior. (5 Punkte)
71 function YH = predict(C, X)
72 % ...
73
74 % 6. Compute class posteriors on a grid. (5 Punkte)
75 function P = posterior_grid(C, MX, MY, class)
76 X = [reshape(MX, prod(size(MX)), 1), ...
77 reshape(MY, prod(size(MY)), 1)];
78 % ...
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.