Factorization Machine Action Set
Example 8.1 MovieLens Data
MovieLens Data
This section contains PROC CAS code.
Note: Input data must be in a CAS table that is accessible in your CAS session. This 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 3: Shared Concepts in SAS Visual Data Mining and Machine Learning 8.3: Procedures Guide. For more information about PROC CAS and programming in CASL, see Getting Started with CASL, SAS Cloud Analytic Services: CAS Procedure Programming Guide and Reference, and SAS Viya: System Programming Guide.
This example shows how to use the factmac action to train a factorization machine model from observations in a data table. In this case, the data are derived from companies that provide movies for online viewing. A company wants to offer its customers recommendations of movies that they might like. These recommendations are based on ratings that are provided by users. The MovieLens data set was developed by the GroupLens project at the University of Minnesota and is available at http://grouplens.org/datasets/movielens (. This example uses the MovieLens 100K version.
There are four columns in the MovieLens 100K data set: user ID, item ID (each item is a movie), timestamp, and rating. This example predicts the rating for a specified user ID and an item ID. The data set is very sparse because most combinations of users and movies are not rated.
You can download the compressed archive file from the website at http://files.grouplens.org/datasets/movielens/ml-100k.zip and use any third-party unzip tool to extract all the files in the archive to the destination directory of your choice.[5] The file that contains the ratings is u.data. The following DATA step loads the data table from the directory into your CAS session:
proc casutil;
load file="u.data" /*or other user-defined location*/
casout="movlens"
importoptions=(filetype="CSV" delimiter="TAB" getnames="FALSE"
vars=("userid" "itemid" "rating" "timestamp"));
run;
The following statements train a factorization machine model on the MovieLens data by using the factmac action.
The table parameter names the input data table to be analyzed. The outModel parameter outputs the fitted parameter estimates to the factors_out data table. The inputs parameter specifies the input variables to be used. The nominals parameter specifies that the variables userid and itemid are nominal. The target parameter specifies the target variable to be used. The maxIter parameter specifies the number of iterations for the optimization. The nFactors parameter specifies the number of factors to be estimated for the factorization machine model. The learnStep parameter specifies the learning step size for the optimization.
proc cas;
loadactionset "tkfactmac";
action factmac result=R / table={name="movlens"},
outModel={name="factors_out", replace=true},
inputs={"userid", "itemid"},
nominals={"userid", "itemid"},
target="rating",
maxIter=20, nFactors=10, learnStep=0.15,
output={casout={name="score_out", replace="TRUE"},
copyvars={"userid","itemid","rating"}};
run;
MovieLens 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 loaded into CAS. One way to do this is to convert the movlens data to the comma-separated-value (CSV) file movlens.csv and then use the following code to load the CSV file into CAS:
s:loadtable{casLib="casuser", path="movlens.csv"}
For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.
The following statements train a factorization machine model on the MovieLens data by using the factmac action.
The table parameter names the input data table to be analyzed. The outModel parameter outputs the fitted parameter estimates to the factors_out data table. The inputs parameter specifies the input variables to be used. The nominals parameter specifies that the variables userid and itemid are nominal. The target parameter specifies the target variable to be used. The maxIter parameter specifies the number of iterations for the optimization. The nFactors parameter specifies the number of factors to be estimated for the factorization machine model. The learnStep parameter specifies the learning step size for the optimization.
s:loadactionset{actionset="factmac"}
s:factmac{table={name="data.movlens"},
inputs={"userid", "itemid"},
nominals={"userid", "itemid"},
target="rating",
maxIter=20, nFactors=10, learnStep=0.15,
output={casout={name="score_out", replace="TRUE"},
copyvars={"userid","itemid","rating"}},
outModel={name="factors_out"}}
s:fetch{table={name="score_out"}}
MovieLens 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 loaded into CAS. One way to do this is to convert the movlens data to the comma-separated-value (CSV) file movlens.csv and then use the following code to load the CSV file into CAS:
s.upload_file('movlens.csv')
For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
The following statements train a factorization machine model on the MovieLens data by using the factmac action.
The table parameter names the input data table to be analyzed. The outModel parameter outputs the fitted parameter estimates to the factors_out data table. The inputs parameter specifies the input variables to be used. The nominals parameter specifies that the variables userid and itemid are nominal. The target parameter specifies the target variable to be used. The maxIter parameter specifies the number of iterations for the optimization. The nFactors parameter specifies the number of factors to be estimated for the factorization machine model. The learnStep parameter specifies the learning step size for the optimization.
s.loadactionset{actionset="factmac"}
s.factmac(display={"names":{"DescStatsInt","FinalLoss","ModelInfo",
"NObs","OptIterHistory "}},
output={"casOut":{"name":"out", "replace":True},
"copyVars":{"userid","itemid","rating"}},
target="rating",
inputs={"userid","itemid"},
nominals={"userid","itemid"},
outModel={"name":"factors_out", "replace":True},
learnStep=0.15, maxIter=20, nFactors=10,
table={"caslib":"hps", "name":"movlens"})
factmac_out=s.CASTable('out')
pprint(factmac_out.fetch(to=20))
MovieLens 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 loaded into CAS. One way to do this is to convert the movlens data to the comma-separated-value (CSV) file movlens.csv and then use the following code to load the CSV file into CAS:
m <- cas.read.csv(s, "movlens.csv", casOut=list(name="movlens"))
For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.
The following statements train a factorization machine model on the MovieLens data by using the factmac action.
The table parameter names the input data table to be analyzed. The outModel parameter outputs the fitted parameter estimates to the factors_out data table. The inputs parameter specifies the input variables to be used. The nominals parameter specifies that the variables userid and itemid are nominal. The target parameter specifies the target variable to be used. The maxIter parameter specifies the number of iterations for the optimization. The nFactors parameter specifies the number of factors to be estimated for the factorization machine model. The learnStep parameter specifies the learning step size for the optimization.
ds <- cas.read.table(s,
"u.data",
header = FALSE,
col.names = c("userid", "itemid", "rating", "timestamp"),
casOut = list(name = "movlens",replace = TRUE))
loadActionSet(s,'factmac')
rs <- cas.factmac.factmac(s,
table = list(name = "movlens"),
outModel = list(name = "factors_out", replace = TRUE),
inputs = list("userid", "itemid"),
nominals = list("userid", "itemid"),
target = "rating",
maxIter = 20,
nFactors = 10,
learnStep = 0.15,
output = list(casout = list(name = "score_out",
replace = TRUE),
copyvars = list("userid", "itemid",
"rating")))
[5] Disclaimer: SAS may reference other websites or content or resources for use at Customer’s sole discretion. SAS has no control over any websites or resources that are provided by companies or persons other than SAS. Customer acknowledges and agrees that SAS is not responsible for the availability or use of any such external sites or resources, and does not endorse any advertising, products, or other materials on or available from such websites or resources. Customer acknowledges and agrees that SAS is not liable for any loss or damage that may be incurred by Customer or its end users as a result of the availability or use of those external sites or resources, or as a result of any reliance placed by Customer or its end users on the completeness, accuracy, or existence of any advertising, products, or other materials on, or available from, such websites or resources.