Machine Learning for Sparse Data Action Set
Sparse Data Set Example
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 trains the model by using a simulated sparse data set, smldata. This data set contains one string variable named vars and 15 observations; vars is a character variable. It contains the data information, which includes the target, a sequence of combined column indexes and column values. The target value is either 1 or –1. The column index starts from 1.
The following code generates the smldata data set:
data smldata;
length vars varchar(*);
input vars $ 1-20;
datalines;
1 1:-1 2:3
1
1 1:1 2:1
1 1:2 2:2
1 1:3 2:3
1 1:4 2:4
1 1:5 2:5
-1 2:2
-1 1:1 2:3
-1 1:2 2:4
-1 1:3 2:5
1 1:0 2:-5
1 1:5
-1 1:0 2:5
-1 1:2 2:8
;
run;
You can load the smldata data set into your CAS session by specifying your CAS engine libref in the second statement in the following DATA step:
data mycas.smldata;
set smldata;
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 sparse machine learning algorithm on the mycas.smldata data table by loading the sparseML action set and then using the sparseSvmTrain action:
proc cas;
action builtins.loadactionset / actionSet='sparseML';
action sparseSvmTrain /
table="smldata"
input="vars"
savestate="mystate"
;
run;
quit;
The table parameter names the input data table to analyze. The input parameter specifies the input variable to use in the training. The savestate parameter specifies the name of the analytic store model to save.
The "Data Information" table in Output 26.1.1 shows that the number of observations is 15, the number of features is 2, and the number of sparse elements is 26.
Output 26.1.1: Sparse Data Information
| Data Information | |
|---|---|
| Number of Rows | 15 |
| Number of Features | 2 |
| Number of Sparse Elements | 26 |
The "Misclassification Matrix" table in Output 26.1.2 shows that among the total of fifteen observations, nine observations are classified as 1 and six observations are classified as –1. The number of correctly predicted 1 observations is eight, and the number of correctly predicted –1 observations is six. Thus the accuracy is 93.33%, as indicated in the "Fit Statistics" table in Output 26.1.3.
Output 26.1.2: Misclassification Matrix
| Misclassification Matrix | |||
|---|---|---|---|
| Observed | Training Prediction | ||
| 1 | -1 | Total | |
| 1 | 8 | 1 | 9 |
| -1 | 0 | 6 | 6 |
| Total | 8 | 7 | 15 |
Output 26.1.3: Fit Statistics
| Fit Statistics | |
|---|---|
| Statistic | Training |
| Accuracy | 0.9333 |
| Error | 0.0667 |
| Sensitivity | 0.8889 |
| Specificity | 1.0000 |
The generated model mycas.mystate can be used by PROC ASTORE to score data. For example, you can score mycas.smldata as follows:
proc astore;
score data=mycas.smldata
out=mycas.smlscore
rstore=mycas.mystate
copyvars=(vars)
;
run;
quit;
The output data are saved in the mycas.smlscore data table.
Sparse Data Set Example
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 smldata data to the comma-separated-value (CSV) file smldata.csv and then use the following code to load the CSV file into CAS:
s:loadtable{casLib="casuser", path="smldata.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 sparseML action set and then uses the sparseSvmTrain action to train a sparse data set model on the smldata data table:
s:loadactionset{actionset="sparseML"}
out = s:sparseSvmTrain{
table = "smldata",
input = "vars",
savestate = "mystate"
}
The table parameter names the input data table to analyze. The input parameter specifies the input variable to use in the training. The savestate parameter specifies the name of the analytic store model to save.
The following commands display the tables that are produced by this action call:
print(out.ModelInfo)
print(out.DataInfo)
print(out.Misclassification)
print(out.FitStats)
The generated model mystate can be used with the astore action set to score data. For example, you can score the smldata data table as follows:
s:loadactionset{actionset="astore"}
s:score{
table="smldata",
rstore="mystate",
out="smlscore",
copyvars=("vars")
}
The output data are saved in the smlscore data table.
For details about the results of this analysis, see the CASL version of this example.
Sparse Data Set Example
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 smldata data to the comma-separated-value (CSV) file smldata.csv and then use the following code to load the CSV file into CAS:
s.upload_file('smldata.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 sparseML action set and then uses the sparseSvmTrain action to train a sparse data set model on the smldata data table:
s.loadactionset("sparseML")
out=s.sparseSvmTrain(
table="smldata",
input="vars",
savestate="mystate"
)
The table parameter names the input data table to analyze. The input parameter specifies the input variable to use in the training. The savestate parameter specifies the name of the analytic store model to save.
The following commands display the tables that are produced by this action call:
print(out.DataInfo)
print(out.Misclassification)
print(out.FitStats)
The generated model mystate can be used with the astore action set to score data. For example, you can score the smldata data table as follows:
s.loadactionset("astore")
s.score(
table="smldata",
rstore="mystate",
out="smlscore",
copyvars=("vars")
)
The output data are saved in the smlscore data table.
For details about the results of this analysis, see the CASL version of this example.
Sparse Data Set Example
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 smldata data to the comma-separated-value (CSV) file smldata.csv and then use the following code to load the CSV file into CAS:
m <- cas.read.csv(s, "smldata.csv", casOut=list(name="smldata"))
For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.
The following code loads the sparseML action set and then uses the sparseSvmTrain action to train a sparse data set model on the smldata data table:
cas.read.csv(s,
"smldata.csv",
header = TRUE,
casOut = list(name = "smldata", replace = TRUE))
loadActionSet(s, 'sparseML')
result<-cas.sparseML.sparseSvmTrain(s,
table = "smldata",
input = "vars",
savestate = "mystate"
)
The table parameter names the input data table to analyze. The input parameter specifies the input variable to use in the training. The savestate parameter specifies the name of the analytic store model to save.
The following commands display the tables that are produced by this action call:
print(result)
For details about the results of this analysis, see the CASL version of this example.