Explain Model Action Set
Compute a Two-Way Partial Dependence Function
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 shows how to use the partialDependence action to estimate the partial dependence (PD) function for two analysis variables.
The following DATA step creates the input data table mycas.cars in your CAS session. These statements assume that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
data mycas.cars;
set sashelp.cars;
run;
The following statements run the gbtreeTrain action in the decisionTree action set to build a gradient boosting model to predict the city mileage of each automobile in the cars data table. The action also outputs an analytic store named gbStore_cars.
proc cas;
action decisionTree.gbtreeTrain /
table = {name ="cars"},
inputs = {{name = "Origin"},
{name = "Type"},
{name = "Cylinders"},
{name = "Horsepower"}},
target = "MPG_City",
savestate = {name="gbStore_cars", replace=true},
seed = 1234
;
run;
quit;
The following statements run the partialDependence action and output the PD function to an ODS table:
proc cas;
action explainModel.partialDependence result = pd_res /
table = "cars",
modelTable = "gbStore_cars",
inputs = {{name = "Origin"},
{name = "Type"},
{name = "Cylinders"},
{name = "Horsepower"}},
predictedTarget = "P_MPG_City",
analysisVariable = "Type",
analysisVariable2 = {name = "Cylinders", min = 4, max = 8, nBins = 3},
outputTables = {includeAll=true, replace=true},
seed = 1234
;
print pd_res;
run;
quit;
Here, Type and Cylinders are specified as the analysis variables, and P_MPG_City is the predicted target variable. The partialDependence action reads the cars data table and the analytic store that is created by the gbtreeTrain action and outputs a partial dependence table. Note that the input variables are the same as those specified in the gbtreeTrain action call. The output table displays the average predicted mileage for each combination of values of the two analysis variables. Because Cylinders is an interval variable, its values are binned. The nBins parameter requests three bins, and the min and max parameters specify the range of values to bin.
Output 13.9.1: Partial Dependence Table
| Partial Dependence | |||||
|---|---|---|---|---|---|
| Bin (Type) | Bin2 (Cylinders) | Type | Cylinders | Mean Prediction (P_MPG_City) | Standard Error (P_MPG_City) |
| 1 | 1 | Sedan | 4 | 21.714788566 | 0.1533080621 |
| 1 | 2 | Sedan | 6 | 20.150100882 | 0.1579377292 |
| 1 | 3 | Sedan | 8 | 19.227805398 | 0.1690330113 |
| 2 | 1 | SUV | 4 | 19.359488273 | 0.1413896733 |
| 2 | 2 | SUV | 6 | 17.649653717 | 0.1495123219 |
| 2 | 3 | SUV | 8 | 15.8203892 | 0.1728460277 |
| 3 | 1 | Sports | 4 | 21.983062577 | 0.184249415 |
| 3 | 2 | Sports | 6 | 20.743515563 | 0.1757092082 |
| 3 | 3 | Sports | 8 | 19.615747799 | 0.1895215161 |
| 4 | 1 | Wagon | 4 | 21.199157558 | 0.1660932214 |
| 4 | 2 | Wagon | 6 | 19.793851713 | 0.1712770007 |
| 4 | 3 | Wagon | 8 | 18.470533609 | 0.1913730813 |
| 5 | 1 | Truck | 4 | 21.452489152 | 0.1757862849 |
| 5 | 2 | Truck | 6 | 17.556703609 | 0.1871462721 |
| 5 | 3 | Truck | 8 | 16.371021265 | 0.2005731682 |
| 6 | 1 | Hybrid | 4 | 25.87123972 | 0.2811427168 |
| 6 | 2 | Hybrid | 6 | 22.077680815 | 0.2782339377 |
| 6 | 3 | Hybrid | 8 | 21.131298442 | 0.2894111087 |
Compute a Two-Way Partial Dependence Function
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 cars data to the comma-separated-value (CSV) file cars.csv and then use the following code to load the CSV file into CAS:
s:loadtable{casLib="casuser", path="cars.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 calls the gbtreeTrain and partialDependence actions.
The gbtreeTrain action call builds a gradient boosting model to predict the city mileage of each automobile in the cars data table and outputs an analytic store named gbStore_cars. The partialDependence action reads the analytic store and outputs the partial dependence (PD) function to an ODS table.
s:loadactionset{actionset="decisionTree"}
s:loadactionset{actionset="explainModel"}
s:decisionTree_gbtreeTrain{
inputs = {{name = "Origin"},
{name = "Type"},
{name = "Cylinders"},
{name = "Horsepower"}},
savestate = {name = "gbStore_cars", replace = true},
table = {name = "cars"},
target = "MPG_City",
seed = 1234
}
s:explainModel_partialDependence{
analysisVariable = "Type",
analysisVariable2 = {name = "Cylinders", min = 4, max = 8, nBins = 3},
inputs = {{name = "Origin"},
{name = "Type"},
{name = "Cylinders"},
{name = "Horsepower"}},
modelTable = "gbStore_cars",
outputTables = {includeAll = true, replace = true},
predictedTarget = "P_MPG_City",
table = "cars",
seed = 1234
In the partialDependence action call, Type and Cylinders are specified as the analysis variables, and P_MPG_City is the predicted target variable. The partialDependence action reads the cars data table and the analytic store that is created by the gbtreeTrain action and outputs a partial dependence table. Note that the input variables are the same as those specified in the gbtreeTrain action call. The output table displays the average predicted mileage for each combination of values of the two analysis variables. Because Cylinders is an interval variable, its values are binned. The nBins parameter requests three bins, and the min and max parameters specify the range of values to bin.
Compute a Two-Way Partial Dependence Function
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 cars data to the comma-separated-value (CSV) file cars.csv and then use the following code to load the CSV file into CAS:
s.upload_file('cars.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 calls the gbtreeTrain and partialDependence actions.
The gbtreeTrain action call builds a gradient boosting model to predict the city mileage of each automobile in the cars data table and outputs an analytic store named gbStore_cars. The partialDependence action reads the analytic store and outputs the partial dependence (PD) function to an ODS table.
s.loadactionset("decisionTree")
s.decisionTree.gbtreeTrain (
table = {"name":"cars"},
inputs = ["Origin", "Type", "Cylinders",
"Horsepower"],
target = "MPG_City",
savestate = {"name":"gbStore_cars", "replace":True},
seed = 1234
)
s.loadactionset("explainModel")
s.explainModel.partialDependence (
table = "cars",
modelTable = {"name": "gbStore_cars"},
inputs = ["Origin", "Type", "Cylinders",
"Horsepower"],
predictedTarget = "P_MPG_City",
analysisVariable = "Type",
analysisVariable2 = {"name": "Cylinders", "min":4, "max":8, "nBins":3},
outputTables = {'includeall':True,'replace':True},
seed = 1234
)
In the partialDependence action call, Type and Cylinders are specified as the analysis variables, and P_MPG_City is the predicted target variable. The partialDependence action reads the cars data table and the analytic store that is created by the gbtreeTrain action and outputs a partial dependence table. Note that the input variables are the same as those specified in the gbtreeTrain action call. The output table displays the average predicted mileage for each combination of values of the two analysis variables. Because Cylinders is an interval variable, its values are binned. The nBins parameter requests three bins, and the min and max parameters specify the range of values to bin.
Compute a Two-Way Partial Dependence Function
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 cars data to the comma-separated-value (CSV) file cars.csv and then use the following code to load the CSV file into CAS:
m <- cas.read.csv(s, "cars.csv", casOut=list(name="cars"))
For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.
The following code calls the gbtreeTrain and partialDependence actions.
The gbtreeTrain action call builds a gradient boosting model to predict the city mileage of each automobile in the cars data table and outputs an analytic store named gbStore_cars. The partialDependence action reads the analytic store and outputs the partial dependence (PD) function to an ODS table.
loadActionSet(s,'decisionTree')
loadActionSet(s,'explainModel')
rs <- cas.decisionTree.gbtreeTrain(
s,
inputs = list('Origin',
'Type',
'Cylinders',
'Horsepower'),
savestate = list(name='gbStore_cars',replace=true),
table = list(name='cars'),
target = 'MPG_City',
seed = 1234
)
rs <- cas.explainModel.partialDependence(
s,
analysisVariable = 'Type',
analysisVariable2 = list(name='Cylinders', min=4, max=8, nBins=3),
inputs = list('Origin',
'Type',
'Cylinders',
'Horsepower'),
modelTable = 'gbStore_cars',
outputTables = list(includeAll=true,replace=true),
predictedTarget = 'P_MPG_City',
table = 'cars',
seed = 1234
)
In the partialDependence action call, Type and Cylinders are specified as the analysis variables, and P_MPG_City is the predicted target variable. The partialDependence action reads the cars data table and the analytic store that is created by the gbtreeTrain action and outputs a partial dependence table. Note that the input variables are the same as those specified in the gbtreeTrain action call. The output table displays the average predicted mileage for each combination of values of the two analysis variables. Because Cylinders is an interval variable, its values are binned. The nBins parameter requests three bins, and the min and max parameters specify the range of values to bin.