Kernel Principal Component Analysis Action Set
Denoising Concentric Circles
This section contains PROC CAS code.
Note: Input data must be accessible in your CAS session, either as a CAS table or as a transient-scope table. A CAS table has a two-level name: the first level is your CAS engine libref, and the second level is the table name. You refer to this table in the CAS procedure by specifying only the second level. For more information about two-level names, see Chapter 2, Shared Concepts (SAS Visual Data Mining and Machine Learning: Procedures). A transient-scope table is called directly from the action and exists in memory for the duration of the action. For more information about accessing data, see SAS Viya: System Programming Guide. For more information about PROC CAS and programming in CASL, see SAS Cloud Analytic Services: CASL Programmer’s Guide and SAS Cloud Analytic Services: CASL Reference.
This example shows how to use the kPca action to get the pre-image of the KPCA projection of the noisy concentric pattern. Because the KPCA projection along the first 10 eigendirections (because the value of the preimageNPC parameter is 10) should contain the major part of the variance of the pattern, the pre-image of this projection in input space is expected to be much less noisy than the original pattern. The noisy pattern is generated using the same code as in Example 21.1, and the noisy concentric plot is shown in Figure 1.
In Example 21.1, the circles data are already loaded into the CAS session called mycas, so here you just call the kPca action with the iterative pre-image method specified and then call the aStore action to find the pre-image of the noisy concentric pattern. The pre-image output is shown in Figure 4.
/* First call KPCA training action */
proc cas;
loadactionset "KernelPCA";
run;
action kPca result = r/
table = {name='circles'},
saveState={name="STATE", replace=TRUE},
input={{name="x"},{name="y"}},
method = "EXACT",
kerType="RBF",
kerParam=0.3,
preimage=True, /* specify preimage=true to run pre-image */
preimageNPC=10, /* use 10 principal components for pre-image calculation */
preimageMethod="ITERATIVE"; /* use iterative preimage method */
run;
print r;
quit;
/* Then use astore.score action to calculate pre-image score.
Here test and train are the same data tables */
proc cas ;
action aStore.score / table={name='circles'},
/* scoring_mode=1 for pre-image scoring, default value is 0 for KPCA scoring */
options={{name="scoring_mode",value=1}, {name="iter_npc",value=10},
{name="iter_thresh",value=1e-5}, {name="iter_num",value=1000}},
out={name='preimage_results', replace=true},
rstore={name='state'},
copyVars={'group','x','y'};
run;
quit;
/* Plot the pre-image of the noisy concentric circles */
proc sgplot data=mycas.preimage_results;
styleattrs datasymbols=(Circle X);
scatter x=_pre_x y=_pre_y / group=group markerattrs=(size=5px);
yaxis min=-1.2 max=1.2;
xaxis min=-1.2 max=1.2;
run;
Figure 4: Denoised Concentric Pattern

In Figure 4, you can see that the pre-image plot of the concentric pattern is indeed smoother (less noisy) than the original noisy concentric pattern.
Denoising Concentric Circles
This section contains Lua code for the analysis in the CASL version of this example, which contains details about the results.
Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the circles data to the comma-separated-value (CSV) file circles.csv and then use the following code to load the CSV file into CAS:
s:loadtable{casLib="casuser", path="circles.csv"}
For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.
-- [[call kernelPca.kPca action with RBF kernel
and iterative pre-image method for training]]
res = s:kernelPca_kPca{table={name="circles"},
method="EXACT",
kerParam= 0.3,
kerType="RBF",
inputs={{name="x1"},{name="x2"}},
preimage=True,
preimageNPC=10,
preimageMethod="ITERATIVE",
savestate={name = "state_denoise_circles"}
}
print (res)
-- get the pre-image score of the training data--
re = s:aStore_score{table={name='circles'},
options={{name="scoring_mode",value=1}, {name="iter_npc",value=10},
{name="iter_thresh",value=1e-5}, {name="iter_num",value=1000}},
out={name='preimage_results',
compress=false,
replace=true,
replication=1,
promote=false},
rstore={name='state_denoise_circles'}
}
print (re)
r=s:fetch{table={name="preimage_results"},to=10}
print (r)
Denoising Concentric Circles
This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.
Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the circles data to the comma-separated-value (CSV) file circles.csv and then use the following code to load the CSV file into CAS:
s.upload_file('circles.csv')
For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
s.loadactionset('kernelPca')
# Train KPCA for circles data with RBF kernel and iterative pre-image method
res = s.kernelPca.kPca(inputs= [{"name":"x1"},{"name":"x2"}]
,table=table('circles')
,method="EXACT"
,kerType="RBF"
,kerParam=0.3
,preimage=True
,preimageNPC=10
,preimageMethod="ITERATIVE"
,savestate = {"name": "state_denoise_circles"})
print(res)
# call aStore action for pre-image scoring
r = s.loadactionset(actionset='astore')
res = s.aStore.score(out = {"name":"preimage_results","replace":True}
,rstore = {"name":"state_denoise_circles"}
,table=table('circles')
,options=[{"name":scoring_mode","value":1},
{"name":iter_npc", "value":10},
{"name":"iter_thresh","value":1e-5},
{"name":iter_num","value":1000}],
,copyVars= [{"name":"x1"},{"name":"x2"},{"name":"group"}])
print (res)
preimage_results = s.fetch(table = {"name":"preimage_results"}, to =10)
print (preimage_results)
Denoising Concentric Circles
This section contains R code for the analysis in the CASL version of this example, which contains details about the results.
Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the circles data to the comma-separated-value (CSV) file circles.csv and then use the following code to load the CSV file into CAS:
m <- cas.read.csv(s, "circles.csv", casOut=list(name="circles"))
For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.
The SAS Scripting Wrapper for Analytics Transfer (SWAT) is an R package that serves as an interface to SAS Cloud Analytic Services (CAS). You can use the SWAT package to write R code to connect to a CAS server and analyze data. For more information about the SWAT package, see Getting Started with SAS Viya for R.
The following code assumes that the training data set and the scoring data set are uploaded to CAS tables by using the appropriate functions available in the SWAT package:
library('swat')
loadActionSet(s, "kernelPca")
# Train KPCA for circles data with RBF kernel and iterative pre-image method
results <- cas.kernelPca.kPca(s,kerParam = 0.3,
method="EXACT",
kerType="RBF",
inputs = list(list(name = "x1"),
list(name = "x2")),
preimage=True,
preimageNPC=10,
preimageMethod="ITERATIVE",
savestate = list(name = "state_denoise_circles",
replace= TRUE),
table=list(name="circles"))
# call aStore action for pre-image scoring
loadActionSet(s, "astore")
results <- cas.astore.score (s, out = list(name = "preimage_results", replace = TRUE),
options=list(list(name="scoring_mode",value=1)
,list(name="iter_npc",value=10)
,list(name="iter_thresh",value=1e-5)
,list(name="iter_num",value=1000)),
rstore = "state_denoise_circles", table = "circles")
preimage_results <- cas.table.fetch(s, table = list(name = "preimage_results"),
maxRows = 5000, to = 5000)
preimage_results = preimage_results$Fetch