Teradata R Package Function Reference - KNNRecommenderPredict - Teradata R Package - Look here for syntax, methods and examples for the functions included in the Teradata R Package.

Teradata® R Package Function Reference

Product
Teradata R Package
Release Number
16.20
Published
February 2020
Language
English (United States)
Last Update
2020-02-28
dita:id
B700-4007
lifecycle
previous
Product Category
Teradata Vantage

Description

The KNNRecommender Predict function applies the model output by the KNNRecommender Train function to predict the ratings or preferences that users would assign to entities like books, songs, movies and other products.

Usage

  td_knn_recommender_predict_mle (
      object = NULL,
      ratings.data = NULL,
      weights.data = NULL,
      bias.data = NULL,
      userid.column = NULL,
      itemid.column = NULL,
      rating.column = NULL,
      topk = 3,
      ratings.data.sequence.column = NULL,
      weights.data.sequence.column = NULL,
      bias.data.sequence.column = NULL,
      ratings.data.partition.column = NULL
  )
  
  ## S3 method for class 'td_knn_recommender_mle'
predict(
      object = NULL,
      ratings.data = NULL,
      weights.data = NULL,
      bias.data = NULL,
      userid.column = NULL,
      itemid.column = NULL,
      rating.column = NULL,
      topk = 3,
      ratings.data.sequence.column = NULL,
      weights.data.sequence.column = NULL,
      bias.data.sequence.column = NULL,
      ratings.data.partition.column = NULL
  )

Arguments

object

Specifies the name of the object returned by the td_knn_recommender_mle function. For td_knn_recommender_predict_mle function, this is an optional argument, if both the arguments "weights.data" and "bias.data" are specified. For S3 predict function, this is a required argument. Whenever the argument "object" is specified, the arguments "weights.data" and "bias.data" will be overwritten by their equivalent tibbles within "object" argument.

ratings.data

Required Argument.
Specifies the user rating table on which the predictions are to be made.

ratings.data.partition.column

Required Argument.
Specifies the Partition By columns for ratings.data. Values to this argument can be provided as vector, if multiple columns are used for ordering.

weights.data

Optional Argument.
Specifies the tbl_teradata (produced by td_knn_recommender_mle) containing the interpolation weights. For S3 predict function, this argument is not required. For td_knn_recommender_predict_mle function, this is an optional argument. For td_knn_recommender_predict_mle function, if this argument is not specified, argument "object" must be specified. Whenever the argument "object" is specified, arguments "weights.data" and "bias.data" will be overwritten by their equivalent tibbles within "object" argument.

bias.data

Optional Argument.
Specifies the tbl_teradata (produced by td_knn_recommender_mle) containing the global, user, and item bias statistics. For S3 predict function, this argument is not required. For td_knn_recommender_predict_mle function, this is an optional argument. For td_knn_recommender_predict_mle function, if this argument is not specified, argument "object" must be specified. Whenever the argument "object" is specified, the arguments "weights.data" and "bias.data" will be overwritten by their equivalent tibbles within "object" argument.

userid.column

Optional Argument.
Specifies the user id column in the rating table. The default value is the first column in the rating table.

itemid.column

Optional Argument.
Specifies the item id column in the rating table. The default value is the second column in the rating table.

rating.column

Optional Argument.
Specifies the rating column in the rating table. The default value is the third column in the rating table.

topk

Optional Argument.
Specifies the number of items to recommend for each user. The "topk" highest-rated items are recommended. Default Value: 3

ratings.data.sequence.column

Optional Argument.
Specifies the vector of column(s) that uniquely identifies each row of the input argument "ratings.data". The argument is used to ensure deterministic results for functions which produce results that vary from run to run.

weights.data.sequence.column

Optional Argument.
Specifies the vector of column(s) that uniquely identifies each row of the input argument "weights.data" or its equivalent tibble within the input argument "object". The argument is used to ensure deterministic results for functions which produce results that vary from run to run.

bias.data.sequence.column

Optional Argument.
Specifies the vector of column(s) that uniquely identifies each row of the input argument "bias.data" or its equivalent tibble within the input argument "object". The argument is used to ensure deterministic results for functions which produce results that vary from run to run.

Value

Function returns an object of class "td_knn_recommender_predict_mle" which is a named list containing Teradata tbl object.
Named list member can be referenced directly with the "$" operator using name: result.

Examples

    # Get the current context/connection
    con <- td_get_context()$connection
    
    # Load example data.
    loadExampleData("knnrecommender_example", "ml_ratings")
    loadExampleData("knnrecommenderpredict_example", "ml_ratings_10")
    
    # The ml_ratings table has movie ratings from 50 users.
    ml_ratings <- tbl(con, "ml_ratings")
    
    #  Build/generate the KNN Recommender model 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"
                                                )
                                          
    # ml_ratings_10 table has movie ratings from a subset of users from the ml_ratings 
    # table. The ml_bias and ml_weights table has the weights and bias values 
    # from the trained KNN Recommender model.
    ml_ratings_10 <- tbl(con, "ml_ratings_10")
    ml_weights <- td_knn_recommender_out$weight.model.table
    ml_bias <- td_knn_recommender_out$bias.model.table
                                               
    # Here "bias.data" and "weights.data" have been made optional with the argument "object"
    # having the td_knn_recommender_mle output object
    td_knn_recommender_predict_out <- td_knn_recommender_predict_mle(
                                                object = td_knn_recommender_out,
                                                ratings.data = ml_ratings_10,
                                                ratings.data.partition.column = c("userid"),
                                                topk = 5
                                               )
    
    # Alternatively use the S3 predict function to make user predictions. 
    td_knn_recommender_predict_out1 <- predict(object = td_knn_recommender_out,
                                                ratings.data = ml_ratings_10,
                                                ratings.data.partition.column = c("userid"),
                                                topk = 5
                                               )
                                               
    # Use the generated model to make user rating predictions. Here the argument "object"
    # has been made optional with the specification of both the arguments "bias.data" and
    # "weights.data"
    td_knn_recommender_predict_out2 <- td_knn_recommender_predict_mle(
                                                ratings.data = ml_ratings_10,
                                                ratings.data.partition.column = c("userid"),
                                                weights.data = ml_weights,
                                                bias.data = ml_bias,
                                                topk = 5
                                               )