Graph-Based Semisupervised Learning Action Set

German Credit Benchmark 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.

This example trains the model by using German credit benchmark data, which are available in the sampsio.dmagecr data set. This data set contains 800 observations without target variables and 100 observations with target variables, 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 Duration, Amount, Installp, 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 second statement in the following DATA step:

cas mysess; libname mycas sasioca sessref=mysess;
 data
mycas.dmagecr;
   set sampsio.dmagecr;
run;
data mycas.unlabeldmagecr;
    set mycas.dmagecr(obs=800);
    id  =_N_;
run;

data mycas.labeldmagecr;
    set mycas.dmagecr(obs=100);
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 run the graph-based semisupervised learning algorithm on the mycas.dmagecr data table by using the labelSpread action:


proc cas;
 loadactionset "tkssl";
 action tkssl.labelSpread /
table={name="unlabeldmagecr"},
labeledTable="labeldmagecr",
 inputs={"duration", "amount", "installp", "resident", "existcr",
               "depends", "age","checking"},

 output=  {casout={name="out",   replace="TRUE"},
 copyvars={"id", "good_bad","amount","installp"}},
         target= "good_bad",
      kernel="KNN", k=8,
         gamma=280000           ;
          run;
quit;

The table parameter names the unlabeled data table to be analyzed. The labeledTable parameter names the labeled data table to be analyzed. The inputs parameter lists all the input variables to be used in the learning. The kernel parameter specifies KNN as the kernel to compute the distances for the similarity measure. The gamma parameter specifies 280,000 as the inverse of the variance for Gaussian kernels when you are computing the pairwise distance between data observations. The k parameter specifies the number of nearest neighbors that are used to compute the KNN kernel value. The output parameter outputs the list of predicted labels for both the unlabeled data and the labeled data to the Out data table. The target parameter specifies the variable to be predicted. Output 18.2.1 shows the number of unlabeled observations, number of labeled observations, number of levels for target variable, gamma value, maximum number of iterations, number of nearest neighbors, and kernel function used.

Output 18.2.1: Model Information

Results from graphSemiSupLearn.labelSpread

Model Information
Labeled Observations Used100
Unlabeled Observations Used800
Maximum Iterations3
Target Number of Levels2
Gamma280000
Nearest Neighbors8
Kernel FunctionKNN


Output 18.2.2 shows the loss that the labelSpread action computed on the basis of the input and the prediction results.

Output 18.2.2: Loss

Loss
Value
35.266603984


The following statements sort the output of the labelSpread action by id and show the observations from 110 to 118:

data out2; set mycas.out; run;
proc sort data=out2; by id; run;
proc print data=out2 (firstobs=110 obs=118);
run;

Figure 2 shows the values of the variables amount and installp, the output of the predicted labels for the nine observations, the target variable, and the indicator for the unlabeled or labeled data.

Figure 2: Predicted Labels for Unlabeled Data

Obsidgood_badamountinstallpI_good_bad_WARN_
11010good4093good0
11111good19133good0
11212good12644good0
11313good61101good0
11414good4584good0
11515good62042good0
11616good13934good0
11717good72281good0
11818good59651good0


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 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 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 graphsemisuplearn action set and then uses the labelSpread action to apply the graph-based semisupervised learning algorithm to the dmagecr data table:

    table = {name="unlabeldmagecr"},
    labeledTable={name="labeldmagecr"},
    inputs = {"duration", "amount", "installp", "resident", "existcr",
               "depends", "age","checking"},
               kernel="KNN",
    gamma=280000
    output={casout={name="out", replace="TRUE"}},
    k=8,
    target = "good_bad"
    nominals={"good_bad"}

The table parameter names the unlabeled data table, which contains the data without target variables. The labeledTable parameter names the labeled data table, which contains the data with target variables. The inputs parameter specifies the input variables to be used. The gamma parameter specifies 280,000 as the inverse of the variance for Gaussian kernels when you are computing the pairwise distance between data observations. The kernel parameter specifies KNN as the kernel to compute the distances for similarity measure. The k parameter specifies the number of nearest neighbors that are used to compute the KNN kernel value. The output parameter outputs the list of predicted labels for both the unlabeled data and the labeled data to the Out data table. The target parameter specifies the target variable name for the labeled data.

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 accessible to the CAS server. One way to do this is to convert the hmeq data to the comma-separated-value (CSV) file hmeq.csv and then use the following code to load the CSV file into CAS:

s.upload_file('hmeq.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 graphsemisuplearn action set and then uses the labelSpread action to apply the graph-based semisupervised learning algorithm to the dmagecr data table:

 s.loadactionset{actionset="tkssl"}
 s.labelSpread(output={"casOut":{"name":"out", "replace":True}},
           inputs={"duration", "amount", "installp", "resident", "existcr",
               "depends", "age","checking"},
           target="good_bad",
           gamma=280000,
           kernel="KNN",
           k=8,
           labeledTable={ "name":"labeldmagecr"},
           table={ "name":"unlabeldmagecr"}
           nominals={"good_bad"}
           )

 out=s.CASTable('out')
 pprint(out.fetch(to=20))

The table parameter names the unlabeled data table, which contains the data without target variables. The labeledTable parameter names the labeled data table, which contains the data with target variables. The inputs parameter specifies the input variables to be used. The gamma parameter specifies 280,000 as the inverse of the variance for Gaussian kernels when you are computing the pairwise distance between data observations. The kernel parameter specifies KNN as the kernel to compute the distances for similarity measure. The k parameter specifies the number of nearest neighbors that are used to compute the KNN kernel value. The output parameter outputs the list of predicted labels for both the unlabeled data and the labeled data to the Out data table. The target parameter specifies the target variable name for the labeled data.

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 accessible to the CAS server. One way to do this is to convert the hmeq data to the comma-separated-value (CSV) file hmeq.csv and then use the following code to load the CSV file into CAS:

m <- cas.read.csv(s, "hmeq.csv", casOut=list(name="hmeq"))

For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.

This example shows how to use the labelSpread action to predict the target variables for the unlabeled data by combining the information from observations in a labeled data table and an unlabeled data table. In this case, the data are from the dmagecr data set, which contains observations for mortgage applicants. This example selects 800 observations for the unlabeled data table and 100 observations for the labeled data table. The labelSpread action returns the predicted labels for observations in the unlabeled data table by combining the information from the unlabeled data and labeled data. The analysis uses nine variables: good_bad, duration, amount, installp, resident, existcr, depends, age and checking. The remaining variables in the data table are not used.

The following statements use the labelSpread action to predict the labels for the unlabeled data table from the dmagecr data. In this example, the value of the gamma parameter is 280,000.


 data <- read.csv("dmagecr.csv", header = T)
 ds1 <- as.casTable(s,
                    data[1:800,],
                    casOut =list(name = "unlabel",replace = TRUE))
 loadActionSet(s,'datastep')
 cas.dataStep.runCode(s,
                      code   = "data unlabel;
                                  set unlabel;

                                run;",
                      single = 'YES')

 ds2 <- as.casTable(s,
                    data[801:900,],
                    casOut =list(name = "label",replace = TRUE))
 cas.dataStep.runCode(s,
                      code   = "data label;
                                  set label;

                                run;",
                      single = 'YES')

 loadActionSet(s,'labelSpread')
 rs <- cas.tkssl.labelSpread(s,
 table      = list(name = "unlabeldmagecr"),
 labeledTable      = list(name = "labeldmagecr"),
 inputs     = list("duration", "amount", "installp", "resident", "existcr",
               "depends", "age","checking"),
 gamma         = 280000,
 kernel="KNN",
           k=8,
 output     = list(casout = list(name = "out",
 replace = TRUE)))

The table parameter names the unlabeled data table, which contains the data without target variables. The labeledTable parameter names the labeled data table, which contains the data with target variables. The inputs parameter specifies the input variables to be used. The gamma parameter specifies 1,000 as the inverse of the variance for Gaussian kernels when you are computing the pairwise distance between data observations. The output parameter outputs the list of predicted labels for both the unlabeled data and the labeled data to the Out data table. The target parameter specifies the target variable name for the labeled data.

Last updated: September 10, 2021