Data Science Pilot Action Set
Detecting Interacting Variables in Binary Classification 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 Visual Data Mining and 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.
The following statements run the detectInteractions action to find interactions between predictor variables in a data table for a binary classification task.
These statements assume that the CAS engine libref is named mycas, but you can substitute any appropriately named CAS engine libref.
The action produces a data table that contains a list of interacting variable pairs together with their probability score for interaction. The output table name is intdet1_out. The following DATA step creates the input data table, mycas.syntheticdata, in your CAS session. This data table contains 10 variables: Y is the response variable, and X1 through X9 are the predictor variables.
data mycas.syntheticdata;
input Y X1 X2 X3 X4 X5 X6 X7 X8 X9;
cards;
1 1 1 -1 1 -1 -1 -1 -1 -1
1 1 1 1 -1 -1 1 1 -1 1
1 1 1 1 -1 -1 1 -1 1 1
1 1 1 1 -1 1 -1 1 -1 1
1 1 1 -1 1 1 1 -1 -1 -1
1 1 1 1 -1 -1 1 1 -1 1
1 1 1 1 -1 -1 1 -1 1 -1
1 1 1 1 -1 1 -1 -1 1 1
1 1 1 -1 1 -1 -1 1 1 1
1 1 1 -1 1 -1 -1 -1 -1 1
-1 1 -1 -1 1 -1 -1 -1 -1 -1
-1 1 -1 1 -1 -1 1 1 -1 1
-1 1 -1 1 -1 -1 1 -1 1 1
-1 1 -1 1 -1 1 -1 1 -1 1
-1 1 -1 -1 1 1 1 -1 -1 -1
-1 1 -1 1 -1 -1 1 1 -1 1
-1 1 -1 1 -1 -1 1 -1 1 -1
-1 1 -1 1 -1 1 -1 -1 1 1
-1 1 -1 -1 1 -1 -1 1 1 1
-1 1 -1 -1 1 -1 -1 -1 -1 1
;
run;
The following PROC CAS statements run the detectInteractions action and display the results in two ODS tables:
proc cas;
loadactionset "dataSciencePilot";
action detectInteractions /
table = {name="syntheticdata"},
target = "Y",
event = "1",
seed = 123,
sparseInputs = false,
inputs = {"X1", "X2", "X3", "X4", "X5", "X6", "X7", "X8", "X9"},
numPairs = 0,
numProjections = 100,
maxNumInteractions = 1000,
maxDistance = 1e-8,
minInteractionProbability = 0.01,
casOut = {name="intdet1_out", replace=True};
run;
fetch / table = "intdet1_out";
run;
quit;
The table parameter names the input data table. The target parameter specifies the name of the variable from the input data table to treat as the response variable. The event parameter specifies the label for observations to treat as the positive class (). The seed parameter initializes the pseudorandom number generator for the action to use. The sparseInputs parameter specifies that the input data are not in sparse format. The inputs parameter specifies which set of numeric (interval) variables from the input data table the action is to consider for interaction analysis. The numPairs parameter specifies the number of background pairs (or the number of pairs of columns) to be used by the XYZ algorithm to estimate the average interaction strength between all input variables under consideration. The numProjections parameter specifies the number of random projections that the XYZ algorithm must use in order to obtain the final list of promising candidates. The maxNumInteractions parameter specifies the maximum number of potential candidates to consider per random projection. The maxDistance parameter specifies the threshold of the maximum distance between two variables for them to be considered as interacting or similar. The minInteractionProbability parameter specifies the threshold of the minimum interaction strength, or interaction probability, that is used to create the final list of interacting variables. The casOut parameter specifies the name of the table to be used to display the list of interacting variables detected by the detectInteractions action.
The CAS output table is shown in Figure 2.
Figure 2: Interaction Variables
| Selected Rows from Table INTDET1_OUT | |||||
|---|---|---|---|---|---|
| _Index_ | FirstVarID | FirstVarName | SecondVarID | SecondVarName | Gamma |
| 1 | 1 | X1 | 2 | X2 | 1 |
| 2 | 2 | X2 | 5 | X5 | 0.4 |
| 3 | 2 | X2 | 9 | X9 | 0.4 |
| 4 | 2 | X2 | 3 | X3 | 0.2 |
| 5 | 2 | X2 | 4 | X4 | 0.2 |
| 6 | 2 | X2 | 7 | X7 | 0.2 |
| 7 | 2 | X2 | 8 | X8 | 0.2 |
Detecting Interacting Variables in Regression 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 Visual Data Mining and 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.
Like the preceding example, this example shows how to use the detectInteractions action to find interactions between predictor variables in a data table for a regression task. In order for this action to handle data for regression tasks, the event parameter should not be specified.
These statements assume that the CAS engine libref is named mycas, but you can substitute any appropriately named CAS engine libref.
The following DATA step creates the input data table, mycas.syntheticdata, in your CAS session. This data table contains 11 variables: Y is the response variable, and X1 through X10 are the predictor variables.
Remark In practice, and for some regression problems, it might be helpful to standardize the response to have zero mean and unit variance before going through the interaction analysis.
data mycas.syntheticdata;
input Y X1 X2 X3 X4 X5 X6 X7 X8 X9 X10;
cards;
0.92656 -1 1 1 1 -1 -1 -1 1 1 -1
7.06509 1 1 1 -1 1 -1 -1 -1 1 1
3.96648 -1 -1 1 -1 -1 -1 -1 -1 1 1
1.28011 1 -1 -1 1 1 -1 1 1 1 -1
0.37873 -1 1 1 1 1 -1 1 1 1 1
4.91928 1 1 -1 1 1 -1 -1 -1 -1 -1
5.61293 -1 -1 1 -1 -1 -1 1 -1 -1 -1
3.66434 1 1 1 1 1 1 1 1 -1 1
4.71154 1 1 1 -1 1 -1 1 -1 -1 1
4.01789 -1 -1 1 1 1 -1 -1 1 -1 -1
1.62021 1 -1 1 -1 -1 1 1 -1 -1 1
6.01789 1 1 -1 1 -1 -1 -1 -1 1 1
4.91928 1 1 1 1 -1 1 -1 1 1 -1
3.66434 1 1 -1 1 -1 -1 1 1 -1 -1
-0.7199 -1 1 -1 1 1 -1 1 1 1 -1
1.62021 1 -1 -1 -1 1 -1 1 -1 -1 1
5.27284 -1 -1 1 1 -1 1 1 -1 1 1
2.02517 -1 1 1 1 1 -1 -1 -1 1 1
7.06509 1 1 -1 -1 -1 1 -1 -1 1 -1
5.66434 -1 -1 1 1 1 1 1 -1 -1 1
0.32731 -1 1 1 -1 -1 1 1 -1 1 1
1.62021 1 -1 1 -1 -1 1 1 -1 -1 -1
3.66434 1 1 -1 1 -1 -1 1 -1 -1 -1
5.27284 -1 -1 -1 1 1 -1 1 -1 1 -1
1.97376 -1 1 -1 -1 1 1 -1 1 1 -1
6.01789 1 1 -1 1 -1 -1 -1 1 -1 -1
2.02517 -1 1 1 1 1 1 -1 -1 -1 1
6.01789 1 1 -1 1 -1 1 -1 1 1 -1
4.37145 1 1 -1 1 -1 -1 1 1 1 -1
0.92656 -1 1 -1 1 1 1 -1 -1 -1 1
;
run;
The following statements run the detectInteractions action with default parameters and display the results in two ODS tables. Note that the event parameter is not specified, so the action treat this as a regression task.
proc cas;
loadactionset "dataSciencePilot";
action detectInteractions /
table = {name="syntheticdata"},
target = "Y",
numProjections = 100,
maxNumInteractions = 1000,
casOut = {name="intdet2_out", replace=true};
run;
fetch / table = "intdet2_out";
run;
quit;
The CAS output table is shown in Figure 3.
Figure 3: Interaction Variables
| Selected Rows from Table INTDET2_OUT | |||||
|---|---|---|---|---|---|
| _Index_ | FirstVarID | FirstVarName | SecondVarID | SecondVarName | Gamma |
| 1 | 1 | X1 | 2 | X2 | 2.5967213333 |
| 2 | 3 | X3 | 10 | X10 | 1.8195593333 |
| 3 | 1 | X1 | 3 | X3 | 1.460066 |
| 4 | 8 | X8 | 10 | X10 | 1.3838106667 |
| 5 | 5 | X5 | 10 | X10 | 1.2670806667 |
| 6 | 7 | X7 | 9 | X9 | 1.25765 |
| 7 | 4 | X4 | 8 | X8 | 1.0128486667 |
| 8 | 1 | X1 | 5 | X5 | 0.905882 |
| 9 | 3 | X3 | 5 | X5 | 0.894416 |
| 10 | 3 | X3 | 4 | X4 | 0.893436 |
| 11 | 1 | X1 | 10 | X10 | 0.7447706667 |
| 12 | 1 | X1 | 7 | X7 | 0.7251053333 |
| 13 | 4 | X4 | 10 | X10 | 0.7138593333 |
| 14 | 2 | X2 | 4 | X4 | 0.6869406667 |
| 15 | 3 | X3 | 8 | X8 | 0.6374606667 |
| 16 | 2 | X2 | 8 | X8 | 0.5939653333 |
| 17 | 3 | X3 | 7 | X7 | 0.5903926667 |
| 18 | 6 | X6 | 8 | X8 | 0.566408 |
| 19 | 3 | X3 | 6 | X6 | 0.5511486667 |
| 20 | 1 | X1 | 8 | X8 | 0.538268 |
Detecting Interacting Variables in Classification Data with Nominal Inputs in Sparse Format
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 Visual Data Mining and 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.
The following statements run the detectInteractions action to find interactions between predictor variables in sparse format in a data table for a classification task.
These statements assume that the CAS engine libref is named mycas, but you can substitute any appropriately named CAS engine libref.
The following DATA step loads the home equity data table (hmeq), which has 5,906 observations, 12 input variables, and 1 binary target variable (column) named bad.
data mycas.hmeq;
set sampsio.hmeq;
run;
The next steps prepare the sparse input table for the detectInteractions action, determine the number of input levels for each nominal variable, and exclude observations that have missing values.
The following code calls the detectInteractions action by using the sparse parameter. Note that the inputs and inputLevels parameters must be specified when the sparse parameter is set to True.
proc cas;
session mysess;
transform /
table = 'hmeq'
pipelines = {
{
inputs = ${loan, mortdue}
discretize = {method = 'quantile', args={nbins=5}}
},
{
inputs = ${job, reason}
cattrans = {method = 'label'}
}
}
copyvars = ${bad}
outVarsNameGlobalPrefix = ''
casout = {name='transformed_hmeq' replace=True};
run;
dataSciencePilot.detectInteractions /
table = 'transformed_hmeq'
target = 'bad'
event = '1'
sparse = True
inputs = ${loan, mortdue, job, reason}
inputLevels = {5, 5, 6, 2}
casout = {name = 'intdet3_out', replace=True};
run;
fetch / table = "intdet3_out";
run;
quit;
The CAS output table is shown in Figure 4.
Figure 4: Interacting Variables
| Selected Rows from Table INTDET3_OUT | |||||
|---|---|---|---|---|---|
| _Index_ | FirstVarID | FirstVarName | SecondVarID | SecondVarName | Gamma |
| 1 | 1 | LOAN_1 | 15 | JOB_5 | 0.457276629 |
| 2 | 1 | LOAN_1 | 16 | JOB_6 | 0.4346468982 |
| 3 | 2 | LOAN_2 | 15 | JOB_5 | 0.339055794 |
| 4 | 3 | LOAN_3 | 15 | JOB_5 | 0.3347639485 |
| 5 | 1 | LOAN_1 | 11 | JOB_1 | 0.3312524385 |
| 6 | 1 | LOAN_1 | 7 | MORTDUE_2 | 0.321888412 |
| 7 | 4 | LOAN_4 | 15 | JOB_5 | 0.3211080765 |
| 8 | 5 | LOAN_5 | 16 | JOB_6 | 0.3195474054 |
| 9 | 4 | LOAN_4 | 16 | JOB_6 | 0.3179867343 |
| 10 | 2 | LOAN_2 | 16 | JOB_6 | 0.3164260632 |
| 11 | 3 | LOAN_3 | 16 | JOB_6 | 0.3144752243 |
| 12 | 5 | LOAN_5 | 15 | JOB_5 | 0.3094030433 |
| 13 | 1 | LOAN_1 | 8 | MORTDUE_3 | 0.3019898556 |
| 14 | 5 | LOAN_5 | 10 | MORTDUE_5 | 0.2961373391 |
| 15 | 1 | LOAN_1 | 12 | JOB_2 | 0.2902848225 |
| 16 | 1 | LOAN_1 | 9 | MORTDUE_4 | 0.2750682794 |
| 17 | 4 | LOAN_4 | 11 | JOB_1 | 0.2707764339 |
| 18 | 5 | LOAN_5 | 6 | MORTDUE_1 | 0.2684354272 |
| 19 | 1 | LOAN_1 | 17 | REASON_1 | 0.2567303941 |
| 20 | 1 | LOAN_1 | 18 | REASON_2 | 0.2567303941 |
Detecting Interacting Variables in Binary Classification 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 syntheticdata data to the comma-separated-value (CSV) file syntheticdata.csv and then use the following code to load the CSV file into CAS:
s.upload_file('syntheticdata.csv')
For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
s.loadactionset('dataSciencePilot')
s.detectInteractions(
table = casDataTableName,
target = 'Y',
event = '1',
sparseInputs = False,
numPairs = 0,
maxNumInteractions = 1000,
numProjections = 100,
maxDistance = 1.0e-8,
minInteractionProbability = 0.01,
casOut = {"name" : "detint1_out", "replace" : True}
)
s.fetch(table = {"name": "detint1_out"})
For example output from the action, see the CASL code examples.
Detecting Interacting Variables in Regression Data
This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.
For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
s.loadactionset('dataSciencePilot')
s.detectInteractions(
table = casDataTableName,
target = 'Y',
sparseInputs = False,
numPairs = 0,
maxNumInteractions = 1000,
numProjections = 100,
maxDistance = 1.0e-8,
minInteractionProbability = 0.01,
casOut = {"name" : "detint2_out", "replace" : True}
)
s.fetch(table = {"name": "detint2_out"})
For example output from the action, see the CASL code examples.
Detecting Interacting Variables in Classification Data with Nominal Inputs in Sparse Format
This example is not available for the Python programming language.
Detecting Interacting Variables in Binary Classification Data
This example is not available for the Lua programming language.
Detecting Interacting Variables in Regression Data
This example is not available for the Lua programming language.
Detecting Interacting Variables in Classification Data with Nominal Inputs in Sparse Format
This example is not available for the Lua programming language.
Detecting Interacting Variables in Binary Classification Data
This example is not available for the R programming language.
Detecting Interacting Variables in Regression Data
This example is not available for the R programming language.
Detecting Interacting Variables in Classification Data with Nominal Inputs in Sparse Format
This example is not available for the R programming language.