Teradata R Package Function Reference | 17.00 - 17.00 - KNNRecommender - Teradata R Package

Teradata® R Package Function Reference

Teradata R Package
September 2020
Programming Reference


The KNNRecommender function trains a interpolation weight model based on weighted collaborative filtering approach. It uses the input user ratings data to create three model tables: the weights model table, the bias model table and the optional nearest items or neighbors table. These tables are then used to predict the ratings or preferences that users assign to entities like books, songs, movies, and other products.


  td_knn_recommender_mle (
      rating.table = NULL,
      userid.column = NULL,
      itemid.column = NULL,
      rating.column = NULL,
      k = 20,
      learning.rate = 0.001,
      max.iternum = 10,
      threshold = 2.0E-4,
      item.similarity = "Pearson",
      rating.table.sequence.column = NULL



Required Argument.
Specifies the tbl_teradata containing the user ratings.


Optional Argument.
Specifies the user id column in the argument "rating.table". The default is the first column in the "rating.table".
Types: character


Optional Argument.
Specifies the item id column in the argument "rating.table". The default is the second column in the "rating.table".
Types: character


Optional Argument.
Specifies the rating column in the argument "rating.table". The default is the third column in the rating table.
Types: character


Optional Argument.
Specifies the number of nearest neighbors used in the calculation of the interpolation weights.
Default Value: 20
Types: integer


Optional Argument.
Specifies the initial learning rate. The learning rate adjusts automatically during training based on changes in the RMSE.
Default Value: 0.001
Types: numeric


Optional Argument.
Specifies the maximum number of iterations.
Default Value: 10
Types: integer


Optional Argument.
Specifies the function stops when the RMSE drops below this level.
Default Value: 0.0002.
Types: numeric


Optional Argument.
Specifies the method used to calculate item similarity.
Default Value: "Pearson"
Permitted Values: AdjustedCosine, Pearson
Types: character


Optional Argument.
Specifies the vector of column(s) that uniquely identifies each row of the input argument "rating.table". The argument is used to ensure deterministic results for functions which produce results that vary from run to run.
Types: character OR vector of Strings (character)


Function returns an object of class "td_knn_recommender_mle" which is a named list containing objects of class "tbl_teradata".
Named list members can be referenced directly with the "$" operator using the following names:

  1. weight.model.table

  2. bias.model.table

  3. nearest.items

  4. output


    # Get the current context/connection
    con <- td_get_context()$connection
    # Load example data.
    loadExampleData("knnrecommender_example", "ml_ratings")
    # The ml_ratings table has movie ratings from 50 users on 
    # approximately 2900 movies, with an average of about 150 ratings 
    # for each user. The 10 possible ratings range from 0.5 to 5
    # in steps of 0.5. A higher number indicates a better rating.
    ml_ratings <- tbl(con, "ml_ratings")
    # Example 1 - Train the KNN Recommender system on the user ratings data
    td_knn_recommender_out <- td_knn_recommender_mle(rating.table = ml_ratings,
                                                 userid.column = "userid",
                                                 itemid.column = "itemid",
                                                 rating.column = "rating"