annotate toolboxes/distance_learning/mlr/mlr_plot.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function mlr_plot(X, Y, W, D)
wolffd@0 2
wolffd@0 3 [d, n] = size(X);
wolffd@0 4 %%%
wolffd@0 5 % Color-coding
wolffd@0 6 CODES = { 'b.', 'r.', 'g.', 'c.', 'k.', 'm.', 'y.', ...
wolffd@0 7 'b+', 'r+', 'g+', 'c+', 'k+', 'm+', 'y+', ...
wolffd@0 8 'b^', 'r^', 'g^', 'c^', 'k^', 'm^', 'y^', ...
wolffd@0 9 'bx', 'rx', 'gx', 'cx', 'kx', 'mx', 'yx', ...
wolffd@0 10 'bo', 'ro', 'go', 'co', 'ko', 'mo', 'yo'};
wolffd@0 11
wolffd@0 12 %%%
wolffd@0 13 % First, PCA-plot of X
wolffd@0 14
wolffd@0 15 figure;
wolffd@0 16
wolffd@0 17 z = sum(X,3);
wolffd@0 18 subplot(3,2,[1 3]), pcaplot(z, eye(d), Y, CODES), title('Native');
wolffd@0 19 subplot(3,2,[2 4]), pcaplot(X, W, Y, CODES), title('Learned');
wolffd@0 20
wolffd@0 21 [vecs, vals] = eig(z * z');
wolffd@0 22 subplot(3,2,5), bar(sort(real(diag(vals)), 'descend')), title('X*X'' spectrum'), axis tight;
wolffd@0 23
wolffd@0 24 if size(X,3) == 1
wolffd@0 25 [vecs, vals] = eig(W);
wolffd@0 26 vals = real(diag(vals));
wolffd@0 27 else
wolffd@0 28 if size(W,3) == 1
wolffd@0 29 vals = real(W(:));
wolffd@0 30 else
wolffd@0 31 vals = [];
wolffd@0 32 for i = 1:size(W,3)
wolffd@0 33 [vecs, vals2] = eig(W(:,:,i));
wolffd@0 34 vals = [vals ; real(diag(vals2))];
wolffd@0 35 end
wolffd@0 36 end
wolffd@0 37 end
wolffd@0 38 subplot(3,2,6), bar(sort(vals, 'descend')), title('W spectrum'), axis tight;
wolffd@0 39
wolffd@0 40 if nargin < 4
wolffd@0 41 return;
wolffd@0 42 end
wolffd@0 43
wolffd@0 44 %%%
wolffd@0 45 % Now show some diagnostics
wolffd@0 46
wolffd@0 47 figure;
wolffd@0 48 subplot(2,1,1), semilogy(D.f), title('Objective');
wolffd@0 49 subplot(2,1,2), barh([D.time_SO, D.time_solver D.time_total]), ...
wolffd@0 50 title('Time% in SO/solver/total');
wolffd@0 51
wolffd@0 52
wolffd@0 53 function pcaplot(X, W, Y, CODES)
wolffd@0 54 if size(X,3) == 1
wolffd@0 55 A = X' * W * X;
wolffd@0 56 else
wolffd@0 57 A = 0;
wolffd@0 58 if size(W,3) == 1
wolffd@0 59 for i = 1:size(X,3)
wolffd@0 60 A = A + X(:,:,i)' * bsxfun(@times, W(:,i), X(:,:,i));
wolffd@0 61 end
wolffd@0 62 else
wolffd@0 63 for i = 1:size(X,3)
wolffd@0 64 A = A + X(:,:,i)' * W(:,:,i) * X(:,:,i);
wolffd@0 65 end
wolffd@0 66 end
wolffd@0 67 end
wolffd@0 68 [v,d] = eigs(A, 3);
wolffd@0 69 X2 = d.^0.5 * v';
wolffd@0 70 hold on;
wolffd@0 71 for y = 1:max(Y)
wolffd@0 72 z = Y == y;
wolffd@0 73 scatter3(X2(1, z), X2(2, z), X2(3,z), CODES{y});
wolffd@0 74 end
wolffd@0 75
wolffd@0 76 axis equal;