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

Teradata® Package for R Function Reference

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Teradata Package for R
Release Number
17.20
Published
March 2024
Language
English (United States)
Last Update
2024-05-03
dita:id
TeradataR_FxRef_Enterprise_1720
Product Category
Teradata Vantage

XGBoostPredict

Description

The td_xgboost_predict_sqle() function runs the predictive algorithm based on the model generated by td_xgboost_sqle(). The td_xgboost_sqle() function, also known as eXtreme Gradient Boosting, performs classification or regression analysis on datasets.

td_xgboost_predict_sqle() performs prediction for test input data using multiple simple trees in the trained model. The test input data should have the same attributes as used during the training phase, which can be up to 2046. These attributes are used to score based on the trees in the model.

The output contains prediction for each data point in the test data based on regression or classification. The prediction probability is computed based on the majority vote from participating trees. A higher probability implies a more confident prediction by the model. Majority of the trees result in the same prediction.

Notes:

  • The processing time is controlled by (proportional to): * The number of boosted trees used for prediction from the model (controlled by "num_boosted_trees"). * The number of iterations (sub-trees) used for prediction from the model in each boosted tree (controlled by "iter.num").

A careful choice of these parameters can be used to control the processing time.
When the boosted trees size grows more than what can fit in memory, the trees are cached in a local spool space, which may impact the performance of the function compared to the case when all trees fit in memory.

Usage

  td_xgboost_predict_sqle (
      newdata = NULL,
      object = NULL,
      id.column = NULL,
      num.boosted.tree = 1000,
      iter.num = 3,
      accumulate = NULL,
      output.prob = FALSE,
      model.type = "REGRESSION",
      output.responses = NULL,
      ...
  )

Arguments

newdata

Required Argument.
Specifies the tbl_teradata containing the input data.
Types: tbl_teradata

object

Required Argument.
Specifies the tbl_teradata containing the model data generated by td_xgboost_sqle()
function or the instance of td_xgboost_sqle.
Types: tbl_teradata or td_xgboost_sqle

id.column

Required Argument.
Specifies the input data column name that contains a unique
identifier for each test point in the test set.
Note:

  • Input column names with double quotation marks are not allowed for this function.

Types: character

num.boosted.tree

Optional Argument.
Specifies how many boosted trees to use to make predictions.
A combination of both 'task_Index' and 'tree_num' in the model table determines the AMPID and number of trees generated by that AMP. As we order the model table with these two arguments, the number of boosted trees that are loaded are based on this order.
For example, if there are two AMPs on the system and AMP 1 ('task_index': 0) generates three boosted trees ('tree_num':1,2,3), while AMP 2 ('task_index': 1) generate two boosted trees ('tree_num': 1,2). Then, "num_boosted_tree"(4) loads three boosted trees from AMP1 ('task_index': 0) and one boosted tree from AMP2 ('task_index': 1).
As one boosted tree is skipped altogether from loading it in memory and making predictions, this results in a faster elapsed time for queries compared to loading all trees in memory.
However, this can also lead to loss in prediction accuracy. In addition, any unique tree is determined by 'task_index', 'tree_id' and 'iter.num' in the model table.
Default Value: 1000
Types: integer

iter.num

Optional Argument.
Specifies how many iterations to load for each boosted tree to make predictions.
For example, AMP1 ('task_index':0) generates three boosted trees ('tree_num': 1,2,3) with each tree having four iterations ('iter':1,2,3,4). There are 12 trees in total.
"iter_num"(2) only loads two iterations per boosted tree, that is, only six trees are loaded for this example.
As some trees are skipped from being loaded in memory and predictions are made without them, this results in a faster elapsed time for queries compared to loading all trees in memory, but also lead to loss in prediction accuracy.
Default Value: 3
Types: integer

accumulate

Optional Argument.
Specifies the name(s) of input tbl_teradata column(s) to copy to the output. By default, the function copies no input tbl_teradata columns to the output.
Types: character OR vector of Strings (character)

output.prob

Optional Argument.
Specifies whether the function should output the probability for each
"output.responses".
Notes:

  • If "output.prob" is true and "output.responses" are not provided, output the probability of the predicted class.

  • The "output.prob" argument works only when "model.type" is 'Classification'.

Default Value: FALSE
Types: logical

model.type

Optional Argument.
Specifies whether the analysis is a regression (continuous response variable) or a multiple-class classification (predicting result from the number of classes).
For 'Classification', output the prediction column as integers. These integral values represent different categories, and so are better observed as an integer column. To make the output schema for prediction column as an integer, set "model.type" as 'Classification'.
Permitted Values:

  • Regression

  • Classification

Default Value: Regression
Types: character

output.responses

Optional Argument.
Specifies the classes for which to output probabilities.
Notes:

  • If "output.prob" is true and "output.responses" are not provided, output the probability of the predicted class.

  • The "output.responses" argument works only when "model.type" is 'Classification'.

Types: character OR vector of str(s)

...

Specifies the generic keyword arguments SQLE functions accept. Below
are the generic keyword arguments:

persist:
Optional Argument.
Specifies whether to persist the results of the
function in a table or not. When set to TRUE, results are persisted in a table; otherwise, results are garbage collected at the end of the session.
Default Value: FALSE
Types: logical

volatile:
Optional Argument.
Specifies whether to put the results of the
function in a volatile table or not. When set to TRUE, results are stored in a volatile table, otherwise not.
Default Value: FALSE
Types: logical

Function allows the user to partition, hash, order or local order the input data. These generic arguments are available for each argument that accepts tbl_teradata as input and can be accessed as:

  • "<input.data.arg.name>.partition.column" accepts character or vector of character (Strings)

  • "<input.data.arg.name>.hash.column" accepts character or vector of character (Strings)

  • "<input.data.arg.name>.order.column" accepts character or vector of character (Strings)

  • "local.order.<input.data.arg.name>" accepts logical

Note:
These generic arguments are supported by tdplyr if the underlying SQLE Engine function supports, else an exception is raised.

Value

Function returns an object of class "td_xgboost_predict_sqle" which is a named list containing object of class "tbl_teradata".
Named list member(s) can be referenced directly with the "$" operator using the name(s):result

Examples

  
    
    # Get the current context/connection.
    con <- td_get_context()$connection
    
    # Load the example data.
    loadExampleData("tdplyr_example", "titanic", "iris_input")
    
    # Create tbl_teradata object.
    titanic <- tbl(con, "titanic")
    iris_input <- tbl(con, "iris_input")
    
    # Check the list of available analytic functions.
    display_analytic_functions()
    
    # Example 1: This example takes titanic data as input, and generates the Regression
    #            model using td_xgboost_sqle() function.
    #            Using td_xgboost_predict_sqle() function to predict the
    #            fare with the Regression model generated by td_xgboost_sqle().
    
    # Create 2 samples of input data - sample 1 will have 80% of total rows and
    # sample 2 will have 20% of total rows.
    titanic_sample = td_sample(df = titanic, n = c(0.8,0.2))
    
    # Create train dataset from sample 1 by filtering on "sampleid" and drop
    # "sampleid" column as it is not required for training model.
    titanic_train = titanic_sample 
    
    # Create test dataset from sample 2 by filtering on "sampleid" and
    # drop "sampleid" column as it is not required for scoring.
    titanic_test = titanic_sample 
    
    XGBoost_out_1 <- td_xgboost_sqle(
                      data=titanic_train,
                      input.columns=c("age", "survived", "pclass"),
                      response.column = 'fare',
                      max.depth=3,
                      lambda1 = 1000.0,
                      model.type='Regression',
                      seed=-1,
                      shrinkage.factor=0.1,
                      iter.num=2)

    # td_xgboost_predict_sqle() function predicts the result using 
    # generated Regression model by td_xgboost_sqle() and "newdata".
    XGBoostPredict_out_1 <- td_xgboost_predict_sqle(
                              newdata=titanic_test,
                              object=XGBoost_out_1$result,
                              id.column='passenger',
                              object.order.column=c('task_index', 'tree_num',
                                                    'iter', 'tree_order'))
    
    # Print the result.
    print(XGBoostPredict_out_1$result)
    
    # Example 2: This example takes titanic data, and generates the classification
    #            model using td_xgboost_sqle(). Using td_xgboost_predict_sqle() 
    #            function to predict the fare with the classification model
    #            generated by td_xgboost_sqle(). Provides the classes for
    #            which to output the probabilities.
    
    # Create 2 samples of input data - sample 1 will have 80% of total rows and
    # sample 2 will have 20% of total rows.
    iris_sample = td_sample(df = iris_input, n = c(0.8,0.2))
    
    # Create train dataset from sample 1 by filtering on "sampleid" and drop
    # "sampleid" column as it is not required for training model.
    iris_train = iris_sample 
    
    # Create test dataset from sample 2 by filtering on "sampleid" and
    # drop "sampleid" column as it is not required for scoring.
    iris_test = iris_sample 
    
    # Training the model.
    XGBoost_out_2 <- td_xgboost_sqle(
                      data=iris_train,
                      input.columns=c('sepal_length', 'sepal_width',
                                      'petal_length', 'petal_width'),
                      response.column = 'species',
                      max.depth=3,
                      lambda1 = 10000.0,
                      model.type='Classification',
                      seed=-1,
                      shrinkage.factor=0.1,
                      iter.num=2)
    
    # td_xgboost_predict_sqle() predicts the result using generated
    #  Classification model by td_xgboost_sqle() and XGBoost object as input.
    XGBoostPredict_out_2 <- td_xgboost_predict_sqle(
                              newdata=iris_test,
                              object=XGBoost_out_2,
                              id.column='id',
                              model.type='Classification',
                              num.boosted.trees=3,
                              iter.num=2,
                              output.prob=TRUE,
                              output.responses=c('1', '2', '3'),
                              object.order.column=c('task_index',
                                                    'tree_num', 'iter',
                                                    'class_num', 'tree_order'))
    
    # Print the result.
    print(XGBoostPredict_out_2$result)
    
    # Alternatively use S3 predict function to run predict on the output of
    # td_xgboost_sqle() function.
    XGBoostPredict_out_2 <- predict(
                               XGBoost_out_2,
                               newdata=iris_test,
                               id.column='id',
                               model.type='Classification',
                               num.boosted.trees=3,
                               iter.num=2,
                               output.prob=TRUE,
                               output.responses=c('1', '2', '3'),
                               object.order.column=c('task_index',
                                                     'tree_num', 'iter',
                                                     'class_num', 'tree_order'))
                                                    
                                                    
    # Print the result.
    print(XGBoostPredict_out_2$result)