Teradata Package for R Function Reference | 17.20 - DecisionForestPredict - 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
ft:locale
en-US
ft:lastEdition
2024-05-03
dita:id
TeradataR_FxRef_Enterprise_1720
lifecycle
latest
Product Category
Teradata Vantage

TDDecisionForestPredict

Description

td_decision_forest_predict_sqle() function uses the model output by td_decision_forest_sqle() function to analyze the input data and make predictions. This function outputs the probability that each observation is in the predicted class.

Processing times are controlled by the number of trees in the model.
When the number of trees is more than what can fit in memory, then the trees are cached in a local spool space.

Usage

  td_decision_forest_predict_sqle (
      newdata = NULL,
      object = NULL,
      id.column = NULL,
      detailed = FALSE,
      output.prob = FALSE,
      output.responses = NULL,
      accumulate = NULL,
      ...
  )

Arguments

newdata

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

object

Required Argument.
Specifies the tbl_teradata which contains the model data generated by the td_decision_forest_sqle() function or instance of td_decision_forest_sqle.
Types: tbl_teradata or td_decision_forest_sqle

id.column

Required Argument.
Specifies a column containing a unique identifier for each test point in the test set.
Types: character

detailed

Optional Argument.
Specifies whether to output detailed information about the decision tree and the specific tree information, including task index and tree index for each tree.
Default Value: FALSE
Types: logical

output.prob

Optional Argument.
Specifies whether to output probability for each response.
Notes:

  • If "output.prob" is false the function outputs only the probability of the predicted class.

  • The "output.prob" must be true when using "output.responses".

  • The "output.prob" only works with a classification model.

Default Value: FALSE
Types: logical

output.responses

Optional Argument.
Specifies the classes to output the probabilities. By default it output only the probability of the predicted class.
Note:

  • The "output.responses" only works with a classification model.

Types: character OR vector of str(s)

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)

...

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_decision_forest_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("pmmlpredict_example", "boston")
    loadExampleData("tdplyr_example", "iris_input")
    
    # Create tbl_teradata object.
    boston <- tbl(con, "boston")
    iris_input <- tbl(con, "iris_input")
    
    # Check the list of available analytic functions.
    display_analytic_functions()
    
    # Example 1 : This example takes boston data as input, and generates the Regression
    #             model using td_decision_forest_sqle(). 
    #             Using td_decision_forest_predict_sqle()
    #             function to predict the medv with the Regression model
    #             generated by td_decision_forest_sqle().
    
    # Create 2 samples of input data - sample 1 will have 80% of total rows and
    # sample 2 will have 20% of total rows.
    boston_sample <- td_sample(df = boston, 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.
    boston_train = boston_sample 
    
    # Create test dataset from sample 2 by filtering on "sampleid" and
    # drop "sampleid" column as it is not required for scoring.
    boston_test = boston_sample 

    # Training the model.
    DecisionForest_out <- td_decision_forest_sqle(
                                data = boston_train,
                                input.columns = c('crim', 'zn', 'indus', 'chas',
                                                  'nox', 'rm', 'age', 'dis',
                                                  'rad', 'tax','ptratio',
                                                  'black', 'lstat'),
                                response.column = 'medv',
                                max.depth = 12,
                                num.trees = 4,
                                min.node.size = 1,
                                mtry = 3,
                                mtry.seed = 1,
                                seed = 1,
                                tree.type = 'REGRESSION')
    
    # td_decision_forest_predict_sqle() predicts the result using generated Regression model by
    # td_decision_forest_sqle() and "newdata".
    TDDecisionForestPredict_out <- td_decision_forest_predict_sqle(
                                        newdata=boston_test,
                                        object=DecisionForest_out,
                                        id.column="id")
    
    # Print the result.
    print(TDDecisionForestPredict_out$result)
    
    # Example 2 : This example takes iris_input data, and generates the Classification
    #             model using td_decision_forest_sqle(). 
    #             Using td_decision_forest_predict_sqle() function
    #             to predict the species with the Classification model 
    #             generated by  td_decision_forest_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.
    DecisionForest_out_2 <- td_decision_forest_sqle(
                                    data=iris_train,
                                    input.columns=c('sepal_length',
                                                    'sepal_width',
                                                    'petal_length',
                                                    'petal_width'),
                                    response.column="species",
                                    tree.type="CLASSIFICATION")
    
    # td_decision_forest_predict_sqle() predicts the result using generated
    # Regression model by td_decision_forest_sqle() and "newdata".
    TDDecisionForestPredict_out_2 <- td_decision_forest_predict_sqle(
                                            newdata=iris_test,
                                            object=DecisionForest_out_2,
                                            id.column="id",
                                            output.prob=TRUE,
                                            output.responses=c('1', '2', '3'))
    
    # Print the result.
    print(TDDecisionForestPredict_out_2$result)
    
    # Alternatively use S3 predict function to run predict on the output of
    # td_decision_forest_sqle() function.
    TDDecisionForestPredict_out_2 <- predict(DecisionForest_out_2,
                                             newdata=iris_test,
                                             id.column="id",
                                             output.prob=TRUE,
                                             output.responses=c('1', '2', '3'))
    
    # Print the result.
    print(TDDecisionForestPredict_out_2$result)