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

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function mlr_plot(X, Y, W, D)
Daniel@0 2
Daniel@0 3 [d, n] = size(X);
Daniel@0 4 %%%
Daniel@0 5 % Color-coding
Daniel@0 6 CODES = { 'b.', 'r.', 'g.', 'c.', 'k.', 'm.', 'y.', ...
Daniel@0 7 'b+', 'r+', 'g+', 'c+', 'k+', 'm+', 'y+', ...
Daniel@0 8 'b^', 'r^', 'g^', 'c^', 'k^', 'm^', 'y^', ...
Daniel@0 9 'bx', 'rx', 'gx', 'cx', 'kx', 'mx', 'yx', ...
Daniel@0 10 'bo', 'ro', 'go', 'co', 'ko', 'mo', 'yo'};
Daniel@0 11
Daniel@0 12 %%%
Daniel@0 13 % First, PCA-plot of X
Daniel@0 14
Daniel@0 15 figure;
Daniel@0 16
Daniel@0 17 z = sum(X,3);
Daniel@0 18 subplot(3,2,[1 3]), pcaplot(z, eye(d), Y, CODES), title('Native');
Daniel@0 19 subplot(3,2,[2 4]), pcaplot(X, W, Y, CODES), title('Learned');
Daniel@0 20
Daniel@0 21 [vecs, vals] = eig(z * z');
Daniel@0 22 subplot(3,2,5), bar(sort(real(diag(vals)), 'descend')), title('X*X'' spectrum'), axis tight;
Daniel@0 23
Daniel@0 24 if size(X,3) == 1
Daniel@0 25 [vecs, vals] = eig(W);
Daniel@0 26 vals = real(diag(vals));
Daniel@0 27 else
Daniel@0 28 if size(W,3) == 1
Daniel@0 29 vals = real(W(:));
Daniel@0 30 else
Daniel@0 31 vals = [];
Daniel@0 32 for i = 1:size(W,3)
Daniel@0 33 [vecs, vals2] = eig(W(:,:,i));
Daniel@0 34 vals = [vals ; real(diag(vals2))];
Daniel@0 35 end
Daniel@0 36 end
Daniel@0 37 end
Daniel@0 38 subplot(3,2,6), bar(sort(vals, 'descend')), title('W spectrum'), axis tight;
Daniel@0 39
Daniel@0 40 if nargin < 4
Daniel@0 41 return;
Daniel@0 42 end
Daniel@0 43
Daniel@0 44 %%%
Daniel@0 45 % Now show some diagnostics
Daniel@0 46
Daniel@0 47 figure;
Daniel@0 48 subplot(2,1,1), plot(D.f), title('Objective');
Daniel@0 49 subplot(2,1,2), barh([D.time_SO, D.time_solver D.time_total]), ...
Daniel@0 50 title('Time% in SO/solver/total');
Daniel@0 51
Daniel@0 52
Daniel@0 53 function pcaplot(X, W, Y, CODES)
Daniel@0 54 if size(X,3) == 1
Daniel@0 55 A = kernelCenter(X' * W * X);
Daniel@0 56 else
Daniel@0 57 % A = zeros(size(X,1));
Daniel@0 58 A = 0;
Daniel@0 59 if size(W,3) == 1
Daniel@0 60 for i = 1:size(X,3)
Daniel@0 61 A = A + X(:,:,i)' * bsxfun(@times, W(:,i), X(:,:,i));
Daniel@0 62 end
Daniel@0 63 else
Daniel@0 64 if size(W,1) == size(W,2)
Daniel@0 65 for i = 1:size(W,1)
Daniel@0 66 for j = i:size(W,2)
Daniel@0 67 A = A + (X(:,:,i) + X(:,:,j)') * bsxfun(@times, squeeze(W(i,j,:)), X(:,:,i) + X(:,:,j));
Daniel@0 68 end
Daniel@0 69 end
Daniel@0 70
Daniel@0 71 else
Daniel@0 72 for i = 1:size(X,3)
Daniel@0 73 A = A + X(:,:,i)' * W(:,:,i) * X(:,:,i);
Daniel@0 74 end
Daniel@0 75 end
Daniel@0 76 end
Daniel@0 77 end
Daniel@0 78 [v,d] = eigs(A, 3);
Daniel@0 79 X2 = d.^0.5 * v';
Daniel@0 80 hold on;
Daniel@0 81 for y = 1:max(Y)
Daniel@0 82 z = Y == y;
Daniel@0 83 scatter3(X2(1, z), X2(2, z), X2(3,z), CODES{y});
Daniel@0 84 end
Daniel@0 85
Daniel@0 86 axis equal;