RECENGINE Procedure

Example 30.5 Tuning Hyperparameters of a k-Nearest Neighbors Model

This example shows how to use the AUTOTUNE statement in the RECENGINE procedure to tune a k-nearest neighbors model on the usersCommunity data set (which is available here: https://support.sas.com/documentation/onlinedoc/viya/examples.htm ). The data set consists of click events over a period of time on a peer-to-peer-support community website. Each web page (viewed as an item by PROC RECENGINE) on the site belongs to a message board that includes a number of topics of specific interest. The same user might have viewed the same web page multiple times. Also, in this data set, it is indicated whether or not the questions within the topic have been resolved. For each click event, the user ID and item ID are recorded. The data set includes 40,000 click events (user feedback).

The following code includes the DATA step that generates the data table usersCommunity. These statements assume that your libref is named mylib, but you can substitute any appropriately named libref.

   data mylib.usersCommunity;
      input itemid boardid userid isSolvedTopic;
      datalines;
      38 4817 6313 0
      15 7589 1355 1
      46 9560 2926 1
      16 11635 3985 1
      38 13023 4163 0

   ... more lines ...   

The following statements use PROC RECENGINE with the AUTOTUNE statement to tune a k-nearest neighbors model on the usersCommunity data:

proc recengine
   data=mylib.userscommunity
   method=knn
   outmodel=mylib.knnmodel;
   input userid itemid / level=nominal;
   input issolvedtopic / level=interval;
   userid userid;
   itemid itemid;
   autotune
      /* Tuning Parameters
      You do not need to specify any tuning parameters for the default
      tuning process. If you want to make adjustments to the default
      tuning process, uncomment the following block of code and change
      any of the tuning parameters' attributes.

      tuningparameters=(
         K ( init=5  lb=1 ub=30 )
         DISTANCEMETRIC
      )
      */
   ;
   /* Remove this line to see all results */
   ods select BestConfiguration EvaluationHistoryPlot IterationHistoryPlot;
run;

The preceding statements produce the table and the plots shown in Output 30.5.1 through Output 30.5.3. The table in Output 30.5.1 displays the evaluation number, the values of the tuning parameters, and the error metric value for the best k-nearest neighbors model that the tuner found. Note that the ODS SELECT statement limits the displayed results to a single table and two plots. You can remove this statement to display all tables and plots. For the full list of ODS tables that PROC RECENGINE produces, see Table 3.

Output 30.5.1: Best Configuration Table

Best Configuration
Evaluation29
Nearest Neighbors (K)24
Distance MetricDICE
Mean Average Precision1.0542803093


Output 30.5.2 displays a scatter plot of all configurations that the tuner tried. The objective values are shown on the Y axis, and the evaluation numbers are shown on the X axis.

Output 30.5.2: Evaluation History Plot

Evaluation History Plot


The plot in Output 30.5.3 displays how the best found objective value and the elapsed time changed with each iteration of the tuner.

Output 30.5.3: Iteration History Plot

Iteration History Plot


Last updated: August 06, 2026