Explain Model Action Set
Compute Shapley Values Using the HyperSHAP Method
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 runs the shapleyExplainer action to explain a prediction made by a forest model by using the HyperSHAP method with depth 1. The following DATA step creates the reference data table mycas.dmagecr in your CAS session, keeping only the variables age, amount, coapp, duration, foreign, job, and good_bad. These statements assume that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
data mycas.dmagecr;
set sampsio.dmagecr;
keep age amount coapp duration foreign job good_bad;
run;
The following DATA step creates the query data table mycas.query in your CAS session:
data mycas.query;
set sampsio.dmagecr;
keep age amount coapp duration foreign job good_bad;
if _N_ = 20;
run;
The following statements run the forestTrain action in the decisionTree action set to build a forest model in order to predict whether the credit rating of each individual in the dmagecr data table is good or bad. The action also outputs an analytic store named forest_astore.
proc cas;
loadactionset "decisionTree";
decisionTree.forestTrain result = r
/ table = 'DMAGECR'
seed = 1234
saveState = {name = 'FOREST_ASTORE',
replace = True}
target = 'good_bad'
maxLevel = 5
inputs = {{name = "age"},
{name = "amount"},
{name = "coapp"},
{name = "duration"},
{name = "foreign"},
{name = "job"}
}
nominals = {{name = "coapp"},
{name = "foreign"},
{name = "job"}
}
;
run;
quit;
The following statements run the shapleyExplainer action and output the results to ODS tables:
proc cas;
loadactionset "explainModel";
explainModel.shapleyExplainer / table = "DMAGECR"
query = "QUERY"
modelTable = "FOREST_ASTORE"
modelTableType = "ASTORE"
predictedTarget = "P_good_badbad"
inputs = {{name = "age"},
{name = "amount"},
{name = "coapp"},
{name = "duration"},
{name = "foreign"},
{name = "job"}
}
nominals = {{name = "coapp"},
{name = "foreign"},
{name = "job"}
}
depth = 1
;
run;
quit;
The table parameter names the input reference data table. The query parameter names the input query data table. The modelTable parameter names the model table. The modelTableType parameter specifies that the model table is an analytic store model table. The predictedTarget parameter specifies that the variable P_good_badbad be used as the predicted target variable. The inputs parameter specifies that the variables age, amount, coapp, duration, foreign, and job be used as inputs. The nominals parameter specifies that the variables coapp, foreign, and job be used as nominal variables. The depth parameter specifies the level of the HyperSHAP approximation to calculate.
The "Shapley Values" table that these statements produce is shown in Output 13.10.1.
Output 13.10.1: Shapley Values
| Shapley Values | |
|---|---|
| Variable | ShapleyValue |
| Intercept | 0.07182 |
| age | -0.00988 |
| amount | -0.02656 |
| duration | -0.02114 |
| coapp | -0.00627 |
| foreign | 0.00011 |
| job | -0.00808 |
Compute Shapley Values Using the HyperSHAP Method
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, convert the query data to the CSV file query.csv, and then use the following code to load the CSV files into CAS:
s:loadtable{casLib="casuser", path="dmagecr.csv"}
s:loadtable{casLib="casuser", path="query.csv"}
For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.
This example runs the shapleyExplainer action to explain a prediction made by a forest model by using the HyperSHAP method. The following code runs the forestTrain action in the decisionTree action set to build a forest model in order to predict whether the credit rating of each individual in the dmagecr data table is good or bad. The action also outputs an analytic store named forest_astore.
table = {name ="DMAGECR"},
seed = 1234,
saveState = {name = 'FOREST_ASTORE',
replace = True},
target = 'good_bad',
maxLevel = 5,
inputs = {{name = "age"},
{name = "amount"},
{name = "coapp"},
{name = "duration"},
{name = "foreign"},
{name = "job"}
},
nominals = {{name = "coapp"},
{name = "foreign"},
{name = "job"}
}
The following code runs the shapleyExplainer action to explain the trained forest model that is saved in the forest_astore analytic store:
table = {name="DMAGECR"},
query = {name="QUERY"},
modelTable = {name="FOREST_ASTORE"},
modelTableType = "ASTORE",
predictedTarget = "P_good_badbad",
inputs = {{name = "age"},
{name = "amount"},
{name = "coapp"},
{name = "duration"},
{name = "foreign"},
{name = "job"}
},
nominals = {{name = "coapp"},
{name = "foreign"},
{name = "job"}
},
depth = 1
The table parameter names the input reference data table. The query parameter names the input query data table. The modelTable parameter names the model table. The modelTableType parameter specifies that the model table is an analytic store model table. The predictedTarget parameter specifies that the variable P_good_badbad be used as the predicted target variable. The inputs parameter specifies that the variables age, amount, coapp, duration, foreign, and job be used as inputs. The nominals parameter specifies that the variables coapp, foreign, and job be used as nominal variables. The depth parameter specifies the level of the HyperSHAP approximation to calculate.
For example output from the action, see the SAS code examples.
Compute Shapley Values Using the HyperSHAP Method
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, convert the query data to the CSV file query.csv, and then use the following code to load the CSV files into CAS:
m <- cas.read.csv(s, "dmagecr.csv", casOut=list(name="dmagecr"))
m <- cas.read.csv(s, "query.csv", casOut=list(name="query"))
For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.
This example runs the shapleyExplainer action to explain a prediction made by a forest model by using the HyperSHAP method. The following code runs the forestTrain action in the decisionTree action set to build a forest model in order to predict whether the credit rating of each individual in the dmagecr data table is good or bad. The action also outputs an analytic store named forest_astore.
s,
table = list(name ="DMAGECR"),
seed = 1234,
saveState = list(name = 'FOREST_ASTORE',
replace = True),
target = 'good_bad',
maxLevel = 5,
inputs = list("age",
"amount",
"coapp",
"duration",
"foreign",
"job"
),
nominals = list("coapp",
"foreign",
"job"
)
The following code runs the shapleyExplainer action to explain the trained forest model that is saved in the forest_astore analytic store:
s,
table = list(name="DMAGECR"),
query = list(name="QUERY"),
modelTable = list(name="FOREST_ASTORE"),
modelTableType = "ASTORE",
predictedTarget = "P_good_badbad",
inputs = list("age",
"amount",
"coapp",
"duration",
"foreign",
"job"
),
nominals = list("coapp",
"foreign",
"job"
),
depth = 1
The table parameter names the input reference data table. The query parameter names the input query data table. The modelTable parameter names the model table. The modelTableType parameter specifies that the model table is an analytic store model table. The predictedTarget parameter specifies that the variable P_good_badbad be used as the predicted target variable. The inputs parameter specifies that the variables age, amount, coapp, duration, foreign, and job be used as inputs. The nominals parameter specifies that the variables coapp, foreign, and job be used as nominal variables. The depth parameter specifies the level of the HyperSHAP approximation to calculate.
For example output from the action, see the SAS code examples.
Compute Shapley Values Using the HyperSHAP Method
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, convert the query data to the CSV file query.csv, and then use the following code to load the CSV files into CAS:
s.upload_file('dmagecr.csv')
s.upload_file('query.csv')
For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
This example runs the shapleyExplainer action to explain a prediction made by a forest model by using the HyperSHAP method. The following code runs the forestTrain action in the decisionTree action set to build a forest model in order to predict whether the credit rating of each individual in the dmagecr data table is good or bad. The action also outputs an analytic store named forest_astore.
s.loadactionset(actionset="decisionTree")
s.forestTrain(
table = {"name" : "DMAGECR"},
seed = 123,
inputs = ["age", "amount", "coapp", "duration",
"foreign", "job"],
target = "good_bad",
nominals = ["coapp", "foreign", "job"],
maxLevel = 5,
saveState = {"name" : "FOREST_ASTORE", "replace" : True}
)
The following code runs the shapleyExplainer action to explain the trained forest model that is saved in the forest_astore analytic store:
s.loadactionset(actionset="explainModel")
s.shapleyExplainer(
table = {"name" : "DMAGECR"},
query = {"name" : "QUERY"},
modelTable = {"name" : "FOREST_ASTORE"},
modelTableType = "ASTORE",
inputs = ["age", "amount", "coapp", "duration",
"foreign", "job"],
nominals = ["coapp", "foreign", "job"],
depth = 1
)
The table parameter names the input reference data table. The query parameter names the input query data table. The modelTable parameter names the model table. The modelTableType parameter specifies that the model table is an analytic store model table. The predictedTarget parameter specifies that the variable P_good_badbad be used as the predicted target variable. The inputs parameter specifies that the variables age, amount, coapp, duration, foreign, and job be used as inputs. The nominals parameter specifies that the variables coapp, foreign, and job be used as nominal variables. The depth parameter specifies the level of the HyperSHAP approximation to calculate.
For example output from the action, see the SAS code examples.