The KPCA Procedure

Getting Started: KPCA Procedure

Note: Input data must be in a CAS table that is accessible in your CAS session. You must refer to this table by using a two-level name. The first level must be a CAS engine libref, and the second level must be the table name. For more information, see the sections Using CAS Sessions and CAS Engine Librefs and Loading a SAS Data Set onto a CAS Server in Chapter 2, Shared Concepts.

This example shows how to use the KPCA procedure to project two concentric data sets onto the first two kernel principal components. The following code shows how to generate these data sets by using the IML procedure and plot it by using the SGPLOT procedure. The output is shown in Figure 1.

proc iml;
start Linspace(a, b, n);
   if n<2 then return( b );
   incr = (b-a) / (n-1);
   return( do(a, b, incr) );
finish;

start makecircles(X, y, n_samples, noise, random_state, factor);

   pi = constant("pi");

   lins=Linspace(0, 2*pi, floor(n_samples/2)+1);
   n_2=floor(n_samples/2);
   lins=remove(lins,n_2+1);
   outer_circ_x=cos(lins);
   outer_circ_y=sin(lins);
   inner_circ_x = outer_circ_x#factor;
   inner_circ_y = outer_circ_y#factor;

   X=(outer_circ_x||inner_circ_x)`||(outer_circ_y||inner_circ_y)`;
   y=(j(1,n_2,0)||j(1,n_2,1))`;
   Xy=X||y;

   /* shuffle observations */
   call randseed(random_state);
   /* sample size=5, rows chosen from 1:NumRows */
   obsIdx = sample(1:nrow(Xy), nrow(Xy), "NoReplace");
       X=Xy[obsIdx,1:ncol(Xy)-1];
       y=Xy[obsIdx,ncol(Xy)];
       Xn=j(nrow(X),2);
       if noise^=. then
       call randgen(Xn, "Normal", 0, noise);
       X=X+Xn;

finish;


 n_samples=1000; /*sample size*/
 noise=0.05;
 random_state=0;
 factor=0.3;  /*radius ratio of inner circle to the outer circle*/

 run makecircles(X, y, n_samples, noise, random_state, factor);

 circles=X||y;

 create  circles from circles[colname={"x" "y" "group"}];
 append from circles;
 close circles;

quit;

 proc sgplot data=circles;
   styleattrs datasymbols=(Circle X);
   scatter x=x y=y / group=group markerattrs=(size=7px);
 run;

Figure 1: Plot of the Two Concentric Circles

Plot of the Two Concentric Circles


You can load the Circles data table into your CAS session by naming your CAS engine libref in the first statement of the following DATA step:

       data mycas.circles;
       set circles;
       run;

The following code uses the KPCA procedure to project the data in the Circles data table onto the first two kernel principal components and then use PROC SGPLOT to plot these data in the reduced dimensions. The output is shown in Figure 2.

proc KPCA data=mycas.circles method=exact;
   input x y;
   output out=mycas.scored copyvar=group npc=2;
run;

proc sgplot data=mycas.scored;
   styleattrs datasymbols=(Circle X);
   scatter x=_PCS_1 y=_PCS_2 / group=group markerattrs=(size=7px);
run;

Figure 2: Projection of the Two Concentric Circles onto the First Two Kernel Principal Components

Projection of the Two Concentric Circles onto the First Two Kernel Principal Components


In Figure 2, you can see that after you project the data onto the first two kernel principal components, the two circles are linearly separable. However, as you can see in Figure 3, the projection of the data onto the first two regular principal components is not linearly separable. (Note that when you use the linear kernel type, a regular PCA is performed.) The following code runs the regular PCA on the Circles data table:

proc KPCA data=mycas.circles method=exact;
   input x y;
   kernel linear;
   output out=mycas.scoredpca copyvar=group npc=2;
run;

proc sgplot data=mycas.scoredpca;
   styleattrs datasymbols=(Circle X);
   scatter x=_PCS_1 y=_PCS_2 / group=group markerattrs=(size=7px);
run;

Figure 3: Projection of the Two Concentric Circles onto the First Two Regular Principal Components

Projection of the Two Concentric Circles onto the First Two Regular Principal Components


Last updated: November 11, 2020