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

pd_res: Results from explainModel.partialDependence

Partial Dependence
Bin (Type)Bin2 (Cylinders)TypeCylindersMean Prediction (P_MPG_City)Standard Error (P_MPG_City)
11Sedan421.7147885660.1533080621
12Sedan620.1501008820.1579377292
13Sedan819.2278053980.1690330113
21SUV419.3594882730.1413896733
22SUV617.6496537170.1495123219
23SUV815.82038920.1728460277
31Sports421.9830625770.184249415
32Sports620.7435155630.1757092082
33Sports819.6157477990.1895215161
41Wagon421.1991575580.1660932214
42Wagon619.7938517130.1712770007
43Wagon818.4705336090.1913730813
51Truck421.4524891520.1757862849
52Truck617.5567036090.1871462721
53Truck816.3710212650.2005731682
61Hybrid425.871239720.2811427168
62Hybrid622.0776808150.2782339377
63Hybrid821.1312984420.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.

Last updated: August 04, 2026