Teradata R Package Function Reference - 16.20 - NaiveBayesTextClassifierPredict - Teradata R Package

Teradata® R Package Function Reference

prodname
Teradata R Package
vrm_release
16.20
created_date
February 2020
category
Programming Reference
featnum
B700-4007-098K

Description

The NaiveBayesTextClassifierPredict (td_naivebayes_textclassifier_predict_mle) function uses the model output by the NaiveBayesTextClassifierTrainer (td_naivebayes_textclassifier_mle) function to predict outcomes for test data.
Note: This function is only available when tdplyr is connected to Vantage 1.1 or later versions.

Usage

  td_naivebayes_textclassifier_predict_mle (
      object = NULL,
      newdata = NULL,
      input.token.column = NULL,
      doc.id.columns = NULL,
      model.type = "MULTINOMIAL",
      top.k = NULL,
      model.token.column = NULL,
      model.category.column = NULL,
      model.prob.column = NULL,
      terms = NULL,
      output.prob = FALSE,
      output.responses = NULL,
      newdata.sequence.column = NULL,
      object.sequence.column = NULL,
      newdata.partition.column = NULL,
      newdata.order.column = NULL,
      object.order.column = NULL
  )

Arguments

object

Required Argument.
Specifies the tbl_teradata which contains the model generated by the td_naivebayes_textclassifier_mle function.
This argument can accept either a tbl_teradata object or an object of td_naivebayes_textclassifier_mle class.

object.order.column

Optional Argument.
Specifies Order By columns for object.
Values to this argument can be provided as a vector, if multiple columns are used for ordering.
Types: character OR vector of Strings (character)

newdata

Required Argument.
Specifies the tbl_teradata object defining the tokens for prediction.

newdata.partition.column

Required Argument.
Specifies Partition By columns for newdata.
Values to this argument can be provided as a vector, if multiple columns are used for partition.
Types: character OR vector of Strings (character)

newdata.order.column

Optional Argument.
Specifies Order By columns for newdata.
Values to this argument can be provided as a vector, if multiple columns are used for ordering.
Types: character OR vector of Strings (character)

input.token.column

Required Argument.
Specifies the name of the column in the input argument "newdata" that contains the tokens.
Types: character

doc.id.columns

Required Argument.
Specifies the names of the columns in the input argument "newdata" that contain the document identifier.
Types: character OR vector of Strings (character)

model.type

Optional Argument.
Specifies the model type of the text classifier.
Default Value: "MULTINOMIAL"
Permitted Values: MULTINOMIAL, BERNOULLI
Types: character

top.k

Optional Argument.
Specifies the number of most likely prediction categories to output with their log-likelihood values (for example, the top 10 most likely prediction categories). The default is all prediction categories.
Note: "top.k" cannot be specified along with "output.responses".
Types: numeric

model.token.column

Optional Argument.
Specifies the name of the column in the input argument "object" that contains the tokens. The default value is the first column of the model.
Note: This argument must be specified along with "model.category.column" and "model.prob.column".
Types: character

model.category.column

Optional Argument.
Specifies the name of the column in the input argument "object" that contains the prediction categories. The default value is the second column of the model.
Note: This argument must be specified along with "model.token.column" and "model.prob.column".
Types: character

model.prob.column

Optional Argument.
Specifies the name of the column in the input argument "object" that contains the token counts. The default value is the third column of the model.
Note: This argument must be specified along with "model.token.column" and "model.category.column".
Types: character

terms

Optional Argument.
Specifies the names of the input tbl_teradata columns to copy to the output tbl_teradata.
Types: character OR vector of Strings (character)

output.prob

Optional Argument.
Specifies whether to output probabilities.
Default Value: FALSE
Types: logical

output.responses

Optional Argument.
Specifies a list of responses to output.
Note:

  1. "output.responses" argument support is only available when tdplyr is connected to Vantage 1.1.1 or later versions.

  2. "output.responses" cannot be specified along with "top.k".

Types: character OR vector of characters

newdata.sequence.column

Optional Argument.
Specifies the vector of column(s) that uniquely identifies each row of the input argument "newdata". 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)

object.sequence.column

Optional Argument.
Specifies the vector of column(s) that uniquely identifies each row of the input argument "object". 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)

Value

Function returns an object of class "td_naivebayes_textclassifier_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("naivebayes_textclassifier_predict_example", "token_table","complaints_tokens_test")
    
    # Create remote tibble objects.
    token_table <- tbl(con, "token_table")
    complaints_tokens_test <- tbl(con,"complaints_tokens_test")
    
    # Example -
    # Run NaiveBayesTextClassifier on the train data (token_table).
    nbt_out <- td_naivebayes_textclassifier_mle(data = token_table,
                                                data.partition.column = c("category"),
                                                token.column = "token",
                                                doc.id.columns = c("doc_id"),
                                                doc.category.column = "category",
                                                model.type = "Bernoulli"
                                               )
    
    # Use the generated model to predict the 'tokens' on the test data complaints_tokens_test
    # by using nbt_out model.
    nbt_predict_out <- td_naivebayes_textclassifier_predict_mle(newdata = complaints_tokens_test,
                                                                object = nbt_out,
                                                                newdata.partition.column = "doc_id",
                                                                input.token.column = "token",
                                                                doc.id.columns = c("doc_id"),
                                                                model.type = "Bernoulli",
                                                                top.k = 1
                                                               )
   
   # Use the argument 'output.responses' to predict the 'tokens' on the test data complaints_tokens_test
   # by using nbt_out model generated by td_naivebayes_textclassifier_mle.
   nbt_predict_out1 <- td_naivebayes_textclassifier_predict_mle(newdata=complaints_tokens_test,
                                                                newdata.partition.column='doc_id',
                                                                object=nbt_out,
                                                                input.token.column='token',
                                                                doc.id.columns='doc_id',
                                                                model.token.column='token',
                                                                model.category.column='category',
                                                                model.prob.column='prob',
                                                                model.type='Bernoulli',
                                                                terms = 'token',
                                                                output.prob = TRUE,
                                                                output.responses = c('crash','no_crash'),
                                                                newdata.sequence.column = 'token',
                                                                object.sequence.column = 'token',
                                                                newdata.order.column = 'doc_id',
                                                                object.order.column = 'category'
                                                               )