% Toy problem clear; marker2d_size = 50; marker3d_size = 50; %-------------------------------------------------------------------------- % Generate random 2D data rand('state',0); n = 2; m = 2000; width = 1; rA = (rand(m,n)-0.5)*width*2; %Decision function %Coeff. for [x^2; y^2; sqrt(2)*x*y] w = [1;2;1]*10; gamma = 2.5; % Classifier fh = @(x,y) w(1)*x.^2 + w(3)*sqrt(2)*x.*y + w(2)*y.^2-gamma; j = 1; for i=1:m if(fh(rA(i,1),rA(i,2)) >= 1) % Accept as + A(j,:) = rA(i,:); d(j,1) = 1; j = j+1; elseif(fh(rA(i,1),rA(i,2)) <= -1) % Accept as - A(j,:) = rA(i,:); d(j,1) = -1; j = j+1; end % Discard otherwise end m = length(d); idx_p = find(d == 1); idx_m = find(d == -1); % Map to a 3D feature space mA(:,1) = A(:,1).^2; mA(:,2) = A(:,2).^2; mA(:,3) = sqrt(2)*A(:,1).*A(:,2); %-------------------------------------------------------------------------- %-------------------------------------------------------------------------- % Run CRSVM function opts.save_history = true; opts.index_basis = 'omega'; machine = CRSVM(mA, d, opts); %-------------------------------------------------------------------------- fprintf('# of SVs: %d\n', length(machine.support_vector_index)); fprintf('# of on boundary SVs: %d\n', length(machine.on_boundary_support_vector_index)); hf2d = figure; set(hf2d,'nextplot','replacechildren'); for i=0:machine.iterations, % break; if( i == 0 ) Q = 1:m; else Q = machine.history.Q{i}; end w = machine.history.w{i+1}; gamma = machine.history.bias(i+1); idx_p = Q(find(d(Q) == 1)); idx_m = Q(find(d(Q) == -1)); figure(hf2d); % plus scatter(A(idx_p,1), A(idx_p,2), marker2d_size+10, 'rs', 'filled', 'MarkerEdgeColor','k'); axis([-width width -width width]); hold all; % minus scatter(A(idx_m,1), A(idx_m,2), marker2d_size, 'g^', 'filled', 'MarkerEdgeColor','k', 'MarkerFaceColor', 'w'); fh = @(x,y) w(1)*x.^2 + w(3)*sqrt(2)*x.*y + w(2)*y.^2-gamma; h = ezplot(fh); linewidth = 2; set(h,'LineStyle','-', 'LineWidth', linewidth); hold all; fhp = @(x,y) fh(x,y)-1; h = ezplot(fhp); set(h,'Color', 'k','LineStyle','-.', 'LineWidth', linewidth); fhm = @(x,y) fh(x,y)+1; h = ezplot(fhm); set(h,'Color', 'k','LineStyle','--', 'LineWidth', linewidth); title(sprintf('Iteration: %2d, # of obs: %4d', i, length(Q)), 'FontSize', 28); axis off; set(hf2d,'PaperPositionMode','auto'); Mv2d(i+1) = getframe(hf2d); hold off; end close(hf2d); hf3d = figure; az = -45; el = 50; hf3d = gcf; ha3d = gca; set(hf3d,'nextplot','replacechildren'); for i=0:machine.iterations, cla; hold all; if( i == 0 ) Q = 1:m; else Q = machine.history.Q{i}; end w = machine.history.w{i+1}; gamma = machine.history.bias(i+1); idx_p = Q(find(d(Q) == 1)); idx_m = Q(find(d(Q) == -1)); f3h = @(x,y) (w(1)*x + w(2)*y - gamma)/-w(3); h = ezmesh(f3h,[0 width 0 width],6); colormap([0 0 1]); scatter3(mA(idx_p,1), mA(idx_p,2), mA(idx_p,3), marker3d_size+10, 'rs', 'filled', 'MarkerEdgeColor','k'); scatter3(mA(idx_m,1), mA(idx_m,2), mA(idx_m,3), marker3d_size, 'g^', 'filled', 'MarkerEdgeColor','k', 'MarkerFaceColor', 'w'); zlabel('sqrt(2)*x*y'); axis([0 1 0 1 -1.414 1.414]); grid off; view(az, el); title(sprintf('Iteration: %2d, # of obs: %4d', i, length(Q)), 'FontSize', 28); set(hf2d,'PaperPositionMode','auto'); Mv3d(i+1) = getframe(hf3d); end close(hf3d); Mv = Mv2d; for i = 1:length(Mv2d), Mv(i).cdata = [Mv2d(i).cdata Mv3d(i).cdata]; Mv(i).colormap = []; end movie(Mv2d, 2, 2);