Bayesian Net Classifier Action Set

German Credit Benchmark Data

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 trains the model by using German credit benchmark data, which are available in the sampsio.dmagecr data set. This data set contains 1,000 observations, each of which contains an applicant’s information, including the applicant’s credit rating (GOOD or BAD). The binary target is named GOOD_BAD. Other input variables are Checking, Duration, History, and so on.

The contents of the sampsio.dmagecr data set are described at http://support.sas.com/documentation/cdl/en/emgs/59885/HTML/default/a001026918.htm.

You can load the sampsio.dmagecr data set into your CAS session by specifying your CAS engine libref in the first statement in the following DATA step:

data mycas.dmagecr;
   set sampsio.dmagecr;
run;

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

The following statements use the bnet action to learn a Bayesian network classifier on the mycas.dmagecr data table:

    proc cas;
    action bayesianNetClassifier.bnet/
        table="dmagecr"
        structure={"NAIVE" "TAN" "PC" "MB"}
        bestmodel=true
        outnetwork={name="network" replace=1}
        nominals={"checking" "history" "purpose" "savings" "employed"
                  "installp" "marital" "coapp" "resident" "property"
                  "other" "housing" "existcr" "job"
                  "depends" "telephon" "foreign" "good_bad"}
        inputs={"duration" "amount" "age"
                "depends"  "checking" "history" "purpose" "savings"
                "employed" "installp" "marital" "coapp" "resident"
                "property" "other" "job"
                "existcr" "housing" "telephon" "foreign"}
        target="good_bad"
        partbyfrac = {valid = 0.3 test=0 seed=12345}
        outputTables = {names = { FITSTATISTICS = "fit"
                                  VALIDINFO = "validinfo"
                                  NOBS ="nobs"}}
         ;
    run;
    quit;

The table parameter names the input data table to be analyzed. The structures parameter lists the network structures to be trained. Set the value of the bestmodel parameter to TRUE when you want the action to select the best network structure. The outnetwork parameter names the table that saves the output network structure and probability table. The nominals parameter lists the nominal input variables and the target variable to use in the training. The inputs parameter lists all the input variables to use in the training. The target parameter specifies the variable to be predicted. The partbyfrac parameter specifies that 30% of the input data is to be used for validation and the rest for training. The outputTables parameter specifies ODS tables to be saved in the CAS library.

The following statements produce Output 8.1.1, which shows the number of observations in the input data, including the fact that 691 observations are used for training and 309 are used for validation.

 proc print data=mycas.nobs noobs label;
 run;

Output 8.1.1: Model Selection: Number of Obs

RowIdDescriptionValue
NREADNumber of Observations Read1000
NUSEDNumber of Observations Used1000
NTRAINUSEDNumber of Observations Used for Training691
NVALUSEDNumber of Observations Used for Validation309
NTESTUSEDNumber of Observations Used for Testing0


The following statements produce Output 8.1.2, which shows the fit statistics, including the fact that there are five nodes, five links between the nodes, and the number of parameters is 63 in the resulting network.

 proc print data=mycas.fit noobs label;
 run;

Output 8.1.2: Model Selection: Fit Statistics

RowIdDescriptionValue
NUMNODESNumber of Nodes5.00
NUMLINKSNumber of Links5.00
AVGDEGREEAverage Degree2.00
MAXNUMPARENTSMaximum Number of Parents in Network2.00
NUMPARMSNumber of Parameters63.00
SCOREScore-3446.84
VALIDERRORSRATEValidation Misclassification Rate0.28
TESTERRORSRATETest Misclassification Rate.


The following statements produce Output 8.1.3, which shows that the bnet action has learned a PC Bayesian network structure. In the structure, Checking is the parent of Good_Bad, and Good_Bad is the parent of all the other input variables.

 proc print data=mycas.network noobs label;
     var _parentnode_ _childnode_;
     where _type_="STRUCTURE";
 run;

Output 8.1.3: Model Selection: Best Structure

Parent NodeChild Node
good_badchecking
good_badhistory
good_badamount
durationamount
good_badduration


The following statements produce Output 8.1.3, which shows the validation results for each parameter combination. The NAIVE Bayesian network structure has misclassified 85 observations out of 309 validation observations when the value of the maxParents parameter is greater than or equal to 2 and the value of the varSelect parameter is 1. The PC Bayesian network has 86 misclassification errors. The TAN structure has 86 misclassification errors. The MB Bayesian network structure has 105 misclassification errors when the value of the maxParents parameter is greater than or equal to 1 and the value of the varSelect parameter is 3.

 proc print data=mycas.validinfo noobs label;
 run;

Output 8.1.4: Model Selection: Validation Information

Best ModelMisclassification
Rate
Misclassification
Errors
Number of
Observations
for Assessment
Significance
Threshold
PrescreeningVariable
Selection
StructureParenting MethodMaximum
Number
of Parents
Yes0.27508853090.0511NaiveBestSet2
 0.27508853090.0511NaiveBestSet3
 0.27508853090.0511NaiveBestSet4
 0.27508853090.0511NaiveBestSet5
 0.27832863090.0511PCBestSet2
 0.27832863090.0511PCBestSet3
 0.27832863090.0511PCBestSet4
 0.27832863090.0511PCBestSet5
 0.27832863090.0511TANBestSet2
 0.28479883090.0511PCBestSet1
 0.28479883090.0511NaiveBestSet1
 0.339811053090.0513MBBestSet1
 0.339811053090.0513MBBestSet2
 0.339811053090.0513MBBestSet3
 0.339811053090.0513MBBestSet4
 0.339811053090.0513MBBestSet5


German Credit Benchmark Data

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 dmagecr data to the comma-separated-value (CSV) file dmagecr.csv and then use the following code to load the CSV file into CAS:

s:loadtable{casLib="casuser", path="dmagecr.csv"}

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

The following code loads the bayesianNetClassifier action set and then uses the bnet action to train a Bayesian network classifier model on the dmagecr data table:

s:loadactionset{actionset="bayesianNetClassifier"}
out = s:bnet{
   table="dmagecr",
   structure={"NAIVE", "TAN", "PC", "MB"},
   bestmodel=true,
   outnetwork={name="network" replace=1},
   nominals={"checking", "history", "purpose", "savings", "employed",
             "installp", "marital", "coapp", "resident", "property",
             "other", "housing", "existcr", "job",
             "depends", "telephon", "foreign", "good_bad"},
   inputs={"duration", "amount", "age",
           "depends",  "checking", "history", "purpose", "savings",
           "employed", "installp", "marital", "coapp", "resident",
           "property", "other", "job",
           "existcr", "housing", "telephon", "foreign"},
   target="good_bad",
   partbyfrac = {valid = 0.3, test=0, seed=12345},
   outputTables = {names = { FITSTATISTICS = "fit",
                             VALIDINFO = "validinfo",
                             NOBS ="nobs"}}
}

The table parameter names the input data table to be analyzed. The structures parameter lists the network structures to be trained. Set the value of the bestmodel parameter to TRUE when you want the action to select the best network structure. The outnetwork parameter names the table that saves the output network structure and probability table. The nominals parameter lists the nominal input variables and the target variable to use in the training. The inputs parameter lists all the input variables to use in the training. The target parameter specifies the variable to be predicted. The partbyfrac parameter specifies that 30% of the input data is to be used for validation and the rest for training.

The following commands display the tables that are produced by this action call:

print(out.ModelInfo)
print(out.NObs)
print(out.FitStatistics)
print(out.ValidInfo)
print(out.VarLevel)
print(out.VarInfo)

For details about the results of this analysis, see the CASL version of this example.

German Credit Benchmark Data

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 dmagecr data to the comma-separated-value (CSV) file dmagecr.csv and then use the following code to load the CSV file into CAS:

s.upload_file('dmagecr.csv')

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

The following code loads the bayesianNetClassifier action set and then uses the bnet action to train a Bayesian network classifier model on the dmagecr data table:

s.loadactionset{actionset="bayesianNetClassifier"}
out = s.bnet(
   table="dmagecr",
   structure={"NAIVE", "TAN", "PC", "MB"},
   bestmodel=True,
   outnetwork={"name" : "network", "replace" : 1},
   nominals={"checking", "history", "purpose", "savings", "employed",
             "installp", "marital", "coapp", "resident", "property",
             "other", "housing", "existcr", "job",
             "depends", "telephon", "foreign", "good_bad"},
   inputs={"duration", "amount", "age",
           "depends",  "checking", "history", "purpose", "savings",
           "employed", "installp", "marital", "coapp", "resident",
           "property", "other", "job",
           "existcr", "housing", "telephon", "foreign"},
   target="good_bad",
   partbyfrac = {"valid" : 0.3, "test" : 0, "seed" : 12345},
   outputTables = {"names" : { "FITSTATISTICS" : "fit",
                               "VALIDINFO" : "validinfo",
                               "NOBS" : "nobs"}}
)

The table parameter names the input data table to be analyzed. The structure parameter lists the network structures to be trained. The bestmodel=true parameter specifies that you want the action to select the best network structure. The outnetwork parameter names the table that saves the output network structure and probability table. The nominals parameter lists the nominal input variables and the target variable to use in the training. The inputs parameter lists all the input variables to use in the training. The target parameter specifies the variable to be predicted. The partbyfrac parameter specifies that 30% of the input data is to be used for validation and the rest for training.

The following commands display the tables that are produced by this action call:

print(out.ModelInfo)
print(out.NObs)
print(out.FitStatistics)
print(out.ValidInfo)
print(out.VarLevel)
print(out.VarInfo)

For details about the results of this analysis, see the CASL version of this example.

German Credit Benchmark Data

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 dmagecr data to the comma-separated-value (CSV) file dmagecr.csv and then use the following code to load the CSV file into CAS:

m <- cas.read.csv(s, "dmagecr.csv", casOut=list(name="dmagecr"))

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

The following code loads the bayesianNetClassifier action set and then uses the bnet action to train a Bayesian network classifier model on the dmagecr data table:

loadActionSet(s,'bayesianNetClassifier')
rs <- cas.bayesianNetClassifier.bnet(s,
   table        = "dmagecr",
   structure    = list("NAIVE", "TAN", "PC", "MB"),
   bestmodel    = TRUE,
   outnetwork   = list(name = "network", replace = TRUE),
   nominals     = list("checking", "history", "purpose", "savings", "employed",
                       "installp", "marital", "coapp", "resident", "property",
                       "other", "housing", "existcr", "job"),
   inputs       = list("duration", "amount", "age",
                       "depends", "checking", "history", "purpose", "savings",
                       "employed", "installp", "marital", "coapp", "resident",
                       "property", "other", "job",
                       "existcr", "housing", "telephon", "foreign"),
   target       = "good_bad",
   partbyfrac   = list(valid = 0.3, test = 0, seed = 12345),
   outputTables = list(names = list(fitstatistics = "fit",
                       validinfo = "validinfo",
                       nobs ="nobs")))

The table parameter names the input data table to be analyzed. The structure parameter lists the network structures to be trained. The bestmodel=true parameter requests that the action select the best network structure. The outnetwork parameter names the table in which to save the output network structure and probability table. The nominals parameter lists the nominal input variables and the target variable to use in the training. The inputs parameter lists all the input variables to use in the training. The target parameter specifies the variable to be predicted. The partbyfrac parameter specifies that 30% of the input data is to be used for validation and the rest for training.

The following commands display the tables that are produced by this action call:

print(rs)

For details about the results of this analysis, see the CASL version of this example.

Last updated: August 04, 2026