Bayesian Net Classifier Action Set
Example 5.1 German Credit Benchmark Data
German Credit Benchmark Data
This section contains PROC CAS code.
Note: Input data must be in a CAS table that is accessible in your CAS session. This 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 3: Shared Concepts in SAS Visual Data Mining and Machine Learning 8.3: Procedures Guide. For more information about PROC CAS and programming in CASL, see Getting Started with CASL, SAS Cloud Analytic Services: CAS Procedure Programming Guide and Reference, and SAS Viya: System Programming Guide.
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 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 outputTables parameter specifies ODS tables to be saved in the CAS library.
The following statements produce Output 5.1.1, which shows the number of observations in the input data, including the fact that 711 observations are used for training and 289 are used for validation.
proc print data=mycas.nobs noobs label; run;
Output 5.1.1: Model Selection: Number of Obs
The following statements produce Output 5.1.2, which shows the fit statistics, including the fact that there are 4 nodes, 3 links between the nodes, and the number of parameters is 23 in the resulting network.
proc print data=mycas.fit noobs label; run;
Output 5.1.2: Model Selection: Fit Statistics
| RowId | Description | Value |
|---|---|---|
| NUMNODES | Number of Nodes | 5.00 |
| NUMLINKS | Number of Links | 5.00 |
| AVGDEGREE | Average Degree | 2.00 |
| MAXNUMPARENTS | Maximum Number of Parents in Network | 2.00 |
| NUMPARMS | Number of Parameters | 63.00 |
| SCORE | Score | -3446.84 |
| VALIDERRORSRATE | Validation Misclassification Rate | 0.28 |
| TESTERRORSRATE | Test Misclassification Rate | . |
The following statements produce Output 5.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 5.1.3: Model Selection: Best Structure
The following statements produce Output 5.1.3, which shows the validation results for each parameter combination. The PC Bayesian network structure has misclassified 55 observations out of 289 validation observations when MAXPARENTS is greater than or equal to 2 and VARSELECT=1. The naive Bayesian network has 115 misclassification errors. The TAN structure has 116 misclassification errors. The MB Bayesian network structure has 153 misclassification errors when MAXPARENTS is greater than or equal to 1 and VARSELECT=3.
proc print data=mycas.validinfo noobs label; run;
Output 5.1.4: Model Selection: Validation Information
| Best Model | Misclassification Rate | Misclassification Errors | Number of Observations for Assessment | Significance Threshold | Prescreening | Variable Selection | Structure | Parenting Method | Maximum Number of Parents |
|---|---|---|---|---|---|---|---|---|---|
| Yes | 0.27508 | 85 | 309 | 0.05 | 1 | 1 | Naive | BestSet | 2 |
| 0.27508 | 85 | 309 | 0.05 | 1 | 1 | Naive | BestSet | 3 | |
| 0.27508 | 85 | 309 | 0.05 | 1 | 1 | Naive | BestSet | 4 | |
| 0.27508 | 85 | 309 | 0.05 | 1 | 1 | Naive | BestSet | 5 | |
| 0.27832 | 86 | 309 | 0.05 | 1 | 1 | PC | BestSet | 2 | |
| 0.27832 | 86 | 309 | 0.05 | 1 | 1 | PC | BestSet | 3 | |
| 0.27832 | 86 | 309 | 0.05 | 1 | 1 | PC | BestSet | 4 | |
| 0.27832 | 86 | 309 | 0.05 | 1 | 1 | PC | BestSet | 5 | |
| 0.27832 | 86 | 309 | 0.05 | 1 | 1 | TAN | BestSet | 2 | |
| 0.28479 | 88 | 309 | 0.05 | 1 | 1 | PC | BestSet | 1 | |
| 0.28479 | 88 | 309 | 0.05 | 1 | 1 | Naive | BestSet | 1 | |
| 0.33981 | 105 | 309 | 0.05 | 1 | 3 | MB | BestSet | 1 | |
| 0.33981 | 105 | 309 | 0.05 | 1 | 3 | MB | BestSet | 2 | |
| 0.33981 | 105 | 309 | 0.05 | 1 | 3 | MB | BestSet | 3 | |
| 0.33981 | 105 | 309 | 0.05 | 1 | 3 | MB | BestSet | 4 | |
| 0.33981 | 105 | 309 | 0.05 | 1 | 3 | MB | BestSet | 5 |
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 loaded into CAS. 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}
}
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 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 loaded into CAS. 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("bayesianNetClassifier")
out=s.bnet( table = {"name":"dmagecr"},
structure={"NAIVE", "TAN", "PC", "MB"}, outnetwork={"name":"network", "replace":True},
bestmodel=True,
output={"casOut":{"name":"out", "replace":True}, "copyVars":{"good_bad"}},
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}
)
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 loaded into CAS. 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:
data <- cas.read.csv(s,
"dmagecr.csv",
header = TRUE,
casOut = list(name = "dmagecr", replace = TRUE))
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.