Nonparametric Bayes Action Set

Classification Using the gpClass Action

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 Viya: 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 uses the Toy data set to demonstrate how to use the gpClass action to perform classification, and then how to use the store action in the aStore action set to save the trained model and use the saved model for future classification.

The Toy data set contains 40 observations and three variables. Among these variables, label contains the two binary classes "1" and "0", and the other two numeric variables contain statistics about the location.

The following DATA steps load the Toy data set into your CAS session and then divide the data into training and testing data tables. The gpClass action then uses the first 30 observations in data for training and the next 10 observations for testing. Because the aStore action set is specified for this example, the trained model is saved for future prediction.

data toy;
   input id x y label;
   datalines;
   1 0.7562437251693241 -0.4449246304395207 1
   2 1.6425910060420805 -0.10293958159084055 1
   3 0.5036628219528629 -0.3756019302569773 1
   4 1.9356458876375808 0.1508435933753537 1
   5 -0.48929352371915874 0.9004153510746639 0
   6 1.9612377920931525 0.3688561398322367 1
   7 -0.4457973476327784 0.923703991351446 0
   8 0.897645351230446 0.13625291562699787 0
   9 1.0428050419382913 0.2538945316161072 0
   10 1.7841025147461618 -0.03250971343343456 1
   11 0.6525149362580007 -0.5043431552576471 1
   12 1.8526407982796789 0.13718466658924755 1
   13 0.7736555917316961 -0.48367703386369615 1
   14 -0.9816672335041904 0.33290929557635607 0
   15 0.24612298119546702 -0.1868663956140018 1
   16 1.99621877108224 0.3245177413955755 1
   17 -0.04791750586561572 0.2659542532295433 1
   18 2.110087224576905 0.5250574025280107 1
   19 -0.9602167257824953 0.3787188727510752 0
   20 -0.6841219033906107 0.7364079763792437 0
   21 -0.9920556836181335 0.20730041712702757 0
   22 2.0187106442510587 0.329144855526437 1
   23 -0.9363848220016563 0.5820604427678648 0
   24 0.3176242827173276 -0.23130675115061858 1
   25 -0.08766070923125874 0.9228708936342618 0
   26 0.9503505174789424 0.22930242945171128 0
   27 1.9137065930212924 0.04727138786088236 1
   28 -0.5740857513898902 0.7860024177122371 0
   29 1.7311039665579235 -0.15822368653296057 1
   30 0.15601420930527587 -0.012971094201072592 1
   31 0.03440178911461349 0.35665294277687765 1
   32 1.734352820043718 -0.16711805469096147 1
   33 0.9743086385469273 -0.03306556437399488 0
   34 1.3646032525150116 -0.33201208895864687 1
   35 -1.000588826462477 0.057717691210846855 0
   36 0.0937572740953208 -0.0973344952572724 1
   37 -0.2791878004436257 0.9850672774312587 0
   38 -0.9994507007532529 0.33048792628967455 0
   39 0.9990673318735382 -0.505133345971595 1
   40 0.8997014125100633 0.5246863891252541 0
;

data mycas.toy_train;
   set toy (obs=30);
run;

data mycas.toy_test;
set toy(firstobs=31 obs=35);
run;

data mycas.toy_test1;
   set toy(firstobs=36);
run;

These statements assume that your CAS engine libref is named mycas, but you can substitute any appropriately named CAS engine libref.

The following PROC CAS statements use the gpClass action to perform classification on the training data:

proc cas;
   loadactionset "nonparametricBayes";
   action nonParametricBayes.gpClass status=s
      table={name="toy_train"},
      testTable={name="toy_test"},
      inputs={"x", "y"},
      target="label",
      nominal="label",
      seed=1234567890,
      nThreads=32,
      link="Logit",
      kernel={type="Gaussian", sigma=1},
      infer={method="LA", maxLaIter=100, threshold=0.000001},
      display={names={"Nobs", "DescStats", "ModelInfo", "IterStats", "PredVarName"}},
      output={casOut={name="score", replace=true}, copyVars={"x", "y"}},
      savestate={name='astore', replace=true};
run;

The table parameter names the input training data table to be analyzed. The testTable parameter names the input testing data table to be analyzed. The inputs parameter specifies the input variables to use in classification. The target parameter specifies the variable where the class label is stored. The nominal parameter specifies the target variable type as nominal. The seed parameter specifies the random seed to use in the cluster assignment initialization. The nThreads parameter specifies the number of threads to use in the parallel computation. The kernel parameter specifies the kernel type for the Gaussian process. The inference parameter specifies the model inference setting. The method subparameter, which is set to LA, indicates that the parallel Laplacian approximation (LA) method is to be used for the model inference. The maxLaIter subparameter specifies the maximum number of LA iterations; and the threshold subparameter specifies the threshold to indicate convergence of the LA iterations. The display parameter specifies the ODS table to display. The output parameter outputs the classification scores to the mycas.score data table. The saveState parameter saves the trained Gaussian classification model to the mycas.astore data table for future testing.

The gpClass action generates five ODS tables, which are shown in Output 31.3.1 through Output 31.3.5.

Output 31.3.1: Number of Observations

Results from nonParametricBayes.gpClass

Number of Observations
Observations TypesN
Number of Training Observations Read30
Number of Training Observations Used30
Number of Testing Observations Read5
Number of Testing Observations Used5


Output 31.3.2: Descriptive Statistics

Data Variable Statistics
Variable NameMeanStandard DeviationMinimumMaximum
x0.63478278171.079816849-0.9920556842.1100872246
y0.20017685340.4126373793-0.5043431550.9237039914


Output 31.3.3: Model Information

Model Information
Classification AlgorithmGaussian Process Classification
Inference MethodLaplacian Approximation
Random Number Seed1234567890
Kernel TypeGaussian Kernel
Kernel Sigma1
Link TypeLogit Likelihood
LA Iterations5
LA Threshold1E-6
LA ConvergeYes


Output 31.3.4: Iteration Statistics

Iteration History
Iteration NumberLog Likelihood
1-16.19795993
2-15.8703101
3-15.82855491
4-15.82786298
5-15.82786275


Output 31.3.5: Predicted Variable Names

Predicted Variable Names
Predicted Class LabelProbability of the First ClassProbability of the Second ClassPredicted Variance
I_labelP_label0P_label1P_VAR_


In this example, the gpClass action generates the classification score table mycas.score. The following statements print the table in Output 31.3.6:

proc print noobs data=mycas.score;
run;

Output 31.3.6: Classification Score Table

xyP_label0P_label1I_labelP_VAR_
0.034400.356650.572630.4273700.37342
1.36460-0.332010.229190.7708110.40313
1.73435-0.167120.214020.7859810.38873
-1.000590.057720.679360.3206400.52435
0.97431-0.033070.292240.7077610.33329


The gpClass action also generates the analytic store table by using the saveState parameter. This table saves the trained model and uses it for testing, as shown in the following code for the score action in the aStore action set:

proc cas;
   action aStore.score
      table={name='toy_test1'},
      out={name='gpcnewscore'},
      rstore={name='astore'};
run;

The following statements print the prediction for the testing data in the mycas.gpcnewscore CAS table shown in Output 31.3.7:

proc print noobs data=mycas.gpcnewscore;
run;

Output 31.3.7: Classification Scores for Testing Data

P_label0P_label1I_labelP_VAR_
0.417290.5827110.37123
0.242540.7574610.42171
0.737340.2626600.46789
0.436400.5636010.46223
0.730100.2699000.45856


Classification Using the gpClass Action

This section contains Lua code for the analysis in the CASL version of this example, which contains details about the results.

For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.

The following Lua code loads the Toy data:

 s:upload('toy_train.csv')
 s:upload('toy_test.csv')

The following Lua code loads the nonParametricBayes action set and then uses the gpClass action to perform classification on the Toy data:

 s:loadactionset{actionset="nonParametricBayes"}
 r=s:gpClass{
  table={name="toy_train"},
  testTable={name="toy_test"},
  inputs={"x", "y"},
  target="label",
  nominal="label",
  seed=1234567890,
  nThreads=32,
  link="Logit",
  kernel={type="Gaussian", sigma=1},
  infer={method="LA", maxLaIter=100, threshold=0.000001},
  display={names={"Nobs", "DescStats",
  		"GpcModelInfo", "IterStats", "PredVarName"}},
  output={casOut={name="score", replace=true}, copyVars={"x", "y"}},
  savestate={name='astore', replace=true};
 }

The r variable contains metadata about the model information. To show these metadata, simply enter r:

 r

The following commands display the mycas.score table of the prediction for the testing data:

 score=s:fetch{table={name="score"}}
 print(score)

Classification Using the gpClass Action

This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.

For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.

The following Python code loads the Toy data:

 s.upload('toy_train.csv')
 s.upload('toy_test.csv')

The following Python code loads the nonParametricBayes action set and then uses the gpClass action to perform classification on the Toy data. The parameters that are used in this Python example are the same as in the Lua version of the example.

 s.loadactionset(actionset="nonParametricBayes")
 s.gpClass(
 table={"name":"toy_train"},
 testTable={"name":"toy_test"},
 inputs={"x", "y"},
 target="label",
 nominal="label",
 seed=1234567890,
 nThreads=32,
 link="Logit",
 kernel={"type":"Gaussian", "sigma":1},
 inference={"method":"LA", "maxLaIter":100, "threshold":0.000001},
 output={"casout":{"name":"score", "replace":"TRUE"},
 "copyvars":{"x", "y"}},
 display={"names": ["NObs", "DescStats", "GpcModelInfo", "IterStats", "PredVarName"]},
 saveState={"name":"astore", "replace":"TRUE"}
 );

The following commands display the score table of the prediction for the testing data:

 score=s.fetch(table={"name":"score"})
 print(score)

Classification Using the gpClass Action

This section contains R code for the analysis in the CASL version of this example, which contains details about the results.

For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.

The following R code loads the Toy data:

 cas.read.csv(s, "toy_train.csv", header=TRUE,
              casOut=list(name="toy_train", replace=TRUE))
 cas.read.csv(s, "toy_test.csv", header=TRUE,
              casOut=list(name="toy_test", replace=TRUE))

The following R code loads the nonParametricBayes action set and then uses the gpClass action to perform classification:

 loadActionSet(s, "nonParametricBayes")
 result <- cas.nonParametricBayes.gpClass(s,
 table         = list(name="toy_train"),
 testTable     = list(name="toy_test"),
 inputs        = list("x", "y"),
 seed          = 1234567890,
 nThreads      = 32,
 link          = list(name="Logit"),
 kernel        = list(type="Gaussian", sigma=1),
 infer         = list(method="LA", maxLaIter=1000,
                 threshold=0.000001),
 display       = list(names=list("Nobs", "DescStats",
                 "GpcModelinfo", "IterStats", "PredVarName")),
 output        = list(casOut=list(name="score", replace=TRUE),
                 copyVars=list("x", "y")),
 saveState     = list(name="astore", replace=TRUE)
 )

The following commands display the score table of the prediction for the testing data:

 newscore <- cas.table.fetch(s, table=list(name="score"))
 print(newscore)
Last updated: August 04, 2026