RECENGINE Procedure
Example 28.2 Running the KNN Method of the Recommender Engine Model on Explicit Feedback
This example shows how to use PROC RECENGINE to train a k-nearest-neighbors model on an explicit feedback data set, the MovieLens data set. 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 1M version. You can download the compressed archive file from the website at
http://files.grouplens.org/datasets/movielens/ml-1m.zip
and use any third-party unzip tool to extract all the files in the archive to the destination directory of your choice.[7] The file that contains the ratings is ratings.dat. The data set consists of movie ratings that are provided by a collection of users. For each observation, a user (indicated by the user ID column) has provided a rating from 1 to 5, as indicated by the rating column for a movie (indicated by the item ID column). The data set includes 1,000,209 rating events (user feedback). The following DATA step loads the data table from the directory into your session:
data mylib.movieRatings;
infile 'ratings.dat' DLMSTR = '::';
input userid itemid rating timestamp;
run;
The following statements use the KNN method that is implemented in PROC RECENGINE to train a k-nearest-neighbors model on the mylib.movieRatings data:
proc recengine data=mylib.movieRatings method =KNN outModel=mylib.nearestNeighbors;
input rating / level=interval;
input userid itemid / level=nominal;
userid userid;
itemid itemid;
knn k=5 distanceMetric=cosine outSimilarity=mylib.nearestNeighborValues;
target rating;
savestate rstore=mylib.astoreModel;
run;
When the training is completed, PROC RECENGINE produces three output data tables: nearestNeighbors, nearestNeighborValues, and astoreModel. The nearestNeighbors table contains the nearest neighbors for each item; the nearestNeighborValues table contains the similarity scores for each item’s nearest neighbors; and the astoreModel table contains the nearest neighbors, similarity scores, and training data.
The following statements print the nearest neighbors of the first 10 items in the nearestNeighbors table. The results are shown in Output 28.2.1.
proc print data=mylib.nearestNeighbors(obs=10);
run;
Output 28.2.1: Nearest Neighbors of the First 10 Items
| Obs | Variable | Level | Neighbor1 | Neighbor2 | Neighbor3 | Neighbor4 | Neighbor5 |
|---|---|---|---|---|---|---|---|
| 1 | itemid | 1 | 3114 | 1265 | 588 | 2355 | 1270 |
| 2 | itemid | 10 | 1722 | 1370 | 733 | 648 | 349 |
| 3 | itemid | 100 | 832 | 1589 | 454 | 225 | 1404 |
| 4 | itemid | 1000 | 1470 | 3065 | 2955 | 847 | 1351 |
| 5 | itemid | 1002 | 106 | 821 | 1850 | 3228 | 633 |
| 6 | itemid | 1003 | 1598 | 280 | 22 | 61 | 1518 |
| 7 | itemid | 1004 | 1626 | 204 | 1687 | 1497 | 1382 |
| 8 | itemid | 1005 | 2042 | 2082 | 374 | 1021 | 2953 |
| 9 | itemid | 1006 | 1003 | 1672 | 805 | 280 | 350 |
| 10 | itemid | 1007 | 2016 | 2095 | 1010 | 1011 | 1009 |
The following statements print the nearest-neighbor similarity scores of the first 10 items in the nearestNeighborValues table. The results are shown in Output 28.2.2.
proc print data=mylib.nearestNeighborValues(obs=10);
run;
Output 28.2.2: Nearest-Neighbor Similarity Scores of the First 10 Items
| Obs | Variable | Level | Similarity1 | Similarity2 | Similarity3 | Similarity4 | Similarity5 |
|---|---|---|---|---|---|---|---|
| 1 | itemid | 1 | 0.63310 | 0.61083 | 0.60585 | 0.57938 | 0.57013 |
| 2 | itemid | 10 | 0.67199 | 0.57320 | 0.56666 | 0.56045 | 0.55527 |
| 3 | itemid | 100 | 0.30563 | 0.29816 | 0.29713 | 0.29549 | 0.28901 |
| 4 | itemid | 1000 | 0.27280 | 0.27280 | 0.23264 | 0.22796 | 0.21858 |
| 5 | itemid | 1002 | 0.37227 | 0.30984 | 0.29691 | 0.28868 | 0.27031 |
| 6 | itemid | 1003 | 0.45325 | 0.38697 | 0.36452 | 0.34244 | 0.33584 |
| 7 | itemid | 1004 | 0.55141 | 0.43995 | 0.39007 | 0.38128 | 0.36717 |
| 8 | itemid | 1005 | 0.67295 | 0.44130 | 0.39629 | 0.38543 | 0.38217 |
| 9 | itemid | 1006 | 0.26997 | 0.26703 | 0.25305 | 0.24894 | 0.24633 |
| 10 | itemid | 1007 | 0.47901 | 0.41869 | 0.39998 | 0.37202 | 0.36637 |
The following two code blocks show how to find the top five items for a sample user, whose user ID is 6040, by using the built-in analytic store scoring function. First, you create the scoring data table that includes the user ID of interest:
data mylib.user6040;
input userid;
datalines;
6040
;
run;
Next, you use the ASTORE procedure to score the binary-format output data table astoreModel for this user. In this scoring example, the following parameters are specified in SETOPTION statements:
REC_TOP_Nis set to 5, resulting in five recommendations for user 6040.AGGREGATION_METHODis set to 0, indicating that shared similarity scores are to be summed.REC_ITEM_FILTERis set to 1, indicating that the user’s recommendations cannot include items that the user has interacted with during model training. This is the default behavior during scoring.
proc astore;
setoption REC_TOP_N 5;
setoption AGGREGATION_METHOD 0;
setoption REC_ITEM_FILTER 1;
score data=mylib.user6040 rstore=mylib.astoreModel out=mylib.rankedItemsKnn;
run;
quit;
The following statements print the five most recommended items for user 6040. The results are shown in Output 28.2.3.
proc print data=mylib.rankedItemsKnn;
run;
Output 28.2.3: Top Five Recommended Items for User 6040
| Obs | userid | _RECOMMENDED_itemid | _SCORE | _RANK |
|---|---|---|---|---|
| 1 | 6040 | 2360 | 5 | 1 |
| 2 | 6040 | 942 | 5 | 2 |
| 3 | 6040 | 926 | 5 | 3 |
| 4 | 6040 | 915 | 5 | 4 |
| 5 | 6040 | 911 | 5 | 5 |
[7] 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.