batch_test_toys.m

来自「clustering_code ,Clustering Through Rank」· M 代码 · 共 327 行

M
327
字号
function test_toys%% Clustering Through Ranking On Manifolds% Version 0.2%% Copyright by Markus Breitenbach and Gregory Z. Grudic% This code is for your personal and research use only.%% http://www.cs.colorado.edu/~grudic/% http://ucsu.colorado.edu/~breitenm/%% This software is provided "as is," without warranty of any kind, express% or implied.  In no event shall the authors be held liable% for any direct, indirect, incidental, special or consequential damages% arising out of the use of or inability to use this software.%close all;dbstop if error;dbstop if warning;rand('seed',7654321);H=figure;fprintf(1,'2 moons\n');load Moon_Data.mat;Data_X=Gen_Scale(Data_X,0,1);[Data_X_us,True_Y_us]=getTestData(Data_X,True_Y);figure; scatter(Data_X_us(:,1),Data_X_us(:,2),20,True_Y_us*(1:2)');test(Data_X,True_Y,Data_X_us,True_Y_us,0.1,2 ,H,1);fprintf(1,'Spiral\n');[Data_X2,True_Y2]=getSpiral;[Data_X_us2,True_Y_us2]=getTestData(Data_X2,True_Y2);figure; scatter3(Data_X_us2(:,1),Data_X_us2(:,2),Data_X_us2(:,3),20,True_Y_us2*(1:3)');test(Data_X2,True_Y2,Data_X_us2,True_Y_us2,0.1,3 ,H,2);%save toy_stuff_spectral.matfunction test(Data_X,True_Y,Data_X_us,True_Y_us,scale,clusters, figureHandle, subplotRow)maxRows=2;maxCols=2;%% CHANGE STUFF HEREneighbor_num=3;nGroups=clusters;X=Data_X;clear Data_X;    %% centralize and scale the data    X = X - repmat(mean(X),size(X,1),1);    X = X/max(max(abs(X)));    Data_X_us = Data_X_us - repmat(mean(Data_X_us),size(Data_X_us,1),1);    Data_X_us = Data_X_us/max(max(abs(Data_X_us)));lrn_par = Set_Default_Learning_Paramters;% Optimization for both ALPHA and SIGMA togetherlrn_par.OPT_S_A = 1; % set to 1 to optimize for both alpha and sigmalrn_par.SIGMA_MEDIAN = 0;lrn_par.Cluster_Search = [2,3,4]; % This defines the clusters to be evaluatedlrn_par.Cluster_Search = clusters;lrn_par.my_sigma = 0;%scale;lrn_par.my_alpha = 0;model = LG_Cluster(X,lrn_par);error_thing(True_Y,model.Y);[F_norm,FA,FB] = Gen_Scale(model.F_norm.^2,0.1,1);figure(figureHandle);subplot(maxRows,maxCols, (subplotRow-1)*maxRows+1);hold on;if size(X,2)==2    %scatter(X(:,1),X(:,2),20*F_norm(model.Y*(1:nGroups)'),model.Y*(1:nGroups)');    %scatter(X(model.ind_clust,1),X(model.ind_clust,2),100,model.Y(model.ind_clust,:)*(nGroups+(1:nGroups))','*');    labels=model.Y*(1:nGroups)';    ind = find(labels==1);    plot3(X(ind,1),X(ind,2),(model.Class_Outlier(1).val)*2-1,'bx','MarkerSize',10);    plot3(X(model.ind_clust(1),1),X(model.ind_clust(1),2),max(model.Class_Outlier(1).val),'k*','MarkerSize',20);    ind = find(labels==2);    plot3(X(ind,1),X(ind,2),(model.Class_Outlier(2).val)*2-1,'r+','MarkerSize',10);    plot3(X(model.ind_clust(2),1),X(model.ind_clust(2),2),max(model.Class_Outlier(2).val),'k*','MarkerSize',20);    view(45,50);    xlabel('X_1','FontSize',14);    ylabel('X_2','FontSize',14);    zlabel('Outlier Measure','FontSize',14);else    scatter3(X(:,1),X(:,2),X(:,3),200*F_norm(model.Y*(1:nGroups)'),model.Y*(1:nGroups)');    scatter3(X(model.ind_clust,1),X(model.ind_clust,2),X(model.ind_clust,3),100,model.Y(model.ind_clust,:)*(nGroups+(1:nGroups))','*');    view(45,50);end;title(sprintf('Clustering sigma=%.2f',model.my_sigma),'FontSize',18);%% now try unseen digitsfprintf(1,'Now unseen data...\n\n\n');%Y=mapSpectralOutOfSample(Data_X_us,diag(misc.ss(1:nGroups,1:nGroups)),misc.V(:,1:nGroups),X,scale,nGroups,misc.mu);[un_class] = Classify_New_Data(Data_X_us,model);error_thing(True_Y_us,un_class.Y);F_norm=(un_class.F.^2) .* repmat(FA',size(un_class.F,1),1)+repmat(FB',size(un_class.F,1),1);figure(figureHandle);subplot(maxRows,maxCols, (subplotRow-1)*maxRows+2);if size(X,2)==2    scatter(Data_X_us(:,1),Data_X_us(:,2),20*F_norm(un_class.Y*(1:nGroups)'),un_class.Y*(1:nGroups)');else    scatter3(Data_X_us(:,1),Data_X_us(:,2),Data_X_us(:,3),200*F_norm(un_class.Y*(1:nGroups)'),un_class.Y*(1:nGroups)');end;title('Out-of-sample data','FontSize',14);return;function [X,Y]=getTestData(X,Y)    distort=0.2;    X=X+2*distort*rand(size(X))-distort;    X(find(X<0))=0;    X(find(X>1))=1;    return;function error_thing(True_Y,Y)    nGroups = size(True_Y,2);    tab=crosstab(True_Y*(1:nGroups)',Y*(1:nGroups)');    xmax=max(tab);    dcount=1;    for i=1:length(xmax)         xx=find(tab(:,i)==xmax(i));        if length(xx)>1            myperm(i)=xx(dcount);            dcount=dcount+1;        else            myperm(i)=xx(1);        end;    end;    for i=1:length(myperm)        xx=find(myperm==i);        if length(xx)<1            xmyperm(i)=1;        else            xmyperm(i)=xx(1);        end;    end;    %myperm=xmyperm;    tab=[tab(:,xmyperm), tab]    if length(myperm)~=nGroups        myperm = 1:nGroups;        fprintf(1,'Error with the error thing\n');    end;    True_Y=True_Y(:,myperm);        errors = abs(Y - True_Y);    err_ind = find(sum(abs(Y - True_Y)')'>0);    num_errors = length(err_ind);    err_rate = num_errors / length(True_Y)return;function [X,Y]=getSpiraldeg = 0:0.05:2*pi;rad_min = 0.95;rad_max = 1.0;X1 = zeros(length(deg),3);for i=1:length(deg)    r = rad_min + rand(1,1)*(rad_max-rad_min);    X1(i,1) = r * cos(deg(i));    X1(i,2) = r * sin(deg(i));    X1(i,3) = (i/length(deg));endX2 = zeros(length(deg),3);for i=1:length(deg)    r = rad_min + rand(1,1)*(rad_max-rad_min);    X2(i,1) = r * cos(deg(i) + (2 * pi/3));    X2(i,2) = r * sin(deg(i) + (2 * pi/3));    X2(i,3) = (i/length(deg));endX3 = zeros(length(deg),3);for i=1:length(deg)    r = rad_min + rand(1,1)*(rad_max-rad_min);    X3(i,1) = r * cos(deg(i) + (4 * pi/3));    X3(i,2) = r * sin(deg(i) + (4 * pi/3));    X3(i,3) = (i/length(deg));endX1=Gen_Scale(X1,0,1);X2=Gen_Scale(X2,0,1);X3=Gen_Scale(X3,0,1);figure;hold onplot3(X1(:,1),X1(:,2),X1(:,3),'r+');plot3(X2(:,1),X2(:,2),X2(:,3),'g*');plot3(X3(:,1),X3(:,2),X3(:,3),'bs');hold offpause(1);t99 = 0;X = [X1;X2;X3];[num_ex,dim] = size(X);Y=eye(3);Y=Y([ones(length(X1),1),2*ones(length(X2),1),3*ones(length(X3),1)],:);return;% >> 2 moons%  %  Func-count     x          f(x)         Procedure%     1      0.0543197    -0.914932        initial%     2      0.0566803    -0.909387        golden%     3      0.0528607    -0.915232        golden%     4       0.053407    -0.915372        parabolic%     5      0.0533737    -0.915372        parabolic%     6      0.0534404    -0.915371        parabolic% % Optimization terminated successfully:%  the current x satisfies the termination criteria using OPTIONS.TolX of 1.000000e-004 % % %                                max                   Directional   First-order %  Iter F-count        f(x)   constraint    Step-size   derivative   optimality Procedure %     1      8    -0.963494       -0.005          0.5      -0.0952        0.868   %     2     13    -0.985601      -0.0025          0.5      -0.0345         3.33   %     3     18    -0.994939     -0.00125          0.5      -0.0152         2.69   %     4     23    -0.996957    -0.000625          0.5     0.000125         5.27   %     5     28    -0.999199   -0.0003125          0.5     -0.00253         3.37   %     6     33    -0.999458   -0.0001562          0.5    -0.000116         2.28   %     7     37    -0.999875  -5.868e-005            1    -0.000171         1.03   %     8     41    -0.999921  -7.718e-005            1    -1.4e-005        0.572  Hessian modified  %     9     45    -0.999973  -2.487e-005            1   -1.45e-005        0.456  Hessian modified  %    10     49    -0.999984  -1.702e-005            1   -8.36e-006        0.442  Hessian modified  %    11     53    -0.999993  -1.751e-005            1   -4.22e-006        0.171  Hessian modified  %    12     57    -0.999996    -3.5e-006            1    7.43e-006        0.981  Hessian modified  %    13     61    -0.999997  -9.909e-006            1    8.22e-007        0.141  Hessian modified  % Optimization terminated successfully:%  Magnitude of directional derivative in search direction %   less than 2*options.TolFun and maximum constraint violation %   is less than options.TolCon%  No Active Constraints% % tab =% %    105     0   105     0%      0   105     0   105% % % err_rate =% %      0% % Now unseen data...% % % % tab =% %    105     0   105     0%      0   105     0   105% % % err_rate =% %      0% % Spiral%  %  Func-count     x          f(x)         Procedure%     1      0.0443197    -0.522318        initial%     2      0.0466803    -0.512971        golden%     3      0.0428607    -0.520102        golden%     4      0.0441196     -0.52232        parabolic%     5      0.0442159    -0.522332        parabolic%     6      0.0442493    -0.522331        parabolic%     7      0.0441826    -0.522331        parabolic% % Optimization terminated successfully:%  the current x satisfies the termination criteria using OPTIONS.TolX of 1.000000e-004 % % %                                max                   Directional   First-order %  Iter F-count        f(x)   constraint    Step-size   derivative   optimality Procedure %     1      8    -0.626496       -0.005          0.5        -0.23         15.2   %     2     13    -0.675386      -0.0025          0.5       -0.014         42.8  Hessian modified  %     3     18    -0.785077     -0.00125          0.5       -0.198         46.3   %     4     23    -0.843684    -0.000625          0.5        0.195         87.2   %     5     27    -0.999999            0            1        0.029          251   %     6     48           -1  -2.984e-007    7.63e-006       0.0233         1.78   %     7     52           -1  -2.946e-007            1   -2.23e-009        0.583   % Optimization terminated successfully:%  Search direction less than 2*options.TolX and%   maximum constraint violation is less than options.TolCon%  No Active Constraints% % tab =% %    126     0     0     0   126     0%      0   126     0   126     0     0%      0     0   126     0     0   126% % % err_rate =% %      0% % Now unseen data...% % % % tab =% %    126     0     0     0   126     0%      0   126     0   126     0     0%      0     0   126     0     0   126% % % err_rate =% %      0

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?