Teradata Package for R Function Reference | 17.20 - SVM - 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

SVM

Description

The td_svm_sqle() function is a linear support vector machine that performs classification and regression analysis on data sets.

This function supports these models:

  • Regression (loss: epsilon_insensitive).

  • Classification (loss: hinge). Only binary classification is supported. The only response values are 0 or 1.

td_svm_sqle() is implemented using Minibatch Stochastic Gradient Descent (SGD) algorithm, which is highly scalable for large datasets.

Due to gradient-based learning, the function is highly sensitive to feature scaling.
Before using the features in the function, you must standardize the Input features using td_scale_fit_sqle() and td_scale_transform_sqle() functions. The function only accepts numeric features. Therefore, before training, you must convert the categorical features to numeric values. The function skips the rows with missing (null) values during training.

The function output is a trained td_svm_sqle model, which can be input to the td_svm_predict_sqle() for prediction. The model also contains model statistics of mean squared error (MSE), Loglikelihood, Akaike information criterion (AIC), and Bayesian information criterion (BIC).

Further model evaluation can be done as a post-processing step using functions such as td_regression_evaluator_sqle(), td_classification_evaluator_sqle(), and td_roc_sqle().

Usage

  td_svm_sqle (
      formula = NULL,
      data = NULL,
      input.columns = NULL,
      response.column = NULL,
      model.type = "Classification",
      iter.max = 300,
      epsilon = 0.1,
      batch.size = 10,
      lambda1 = 0.02,
      alpha = 0.15,
      iter.num.no.change = 50,
      tolerance = 0.001,
      intercept = TRUE,
      class.weights = "0:1.0, 1:1.0",
      learning.rate = NULL,
      initial.eta = 0.05,
      decay.rate = 0.25,
      decay.steps = 5,
      momentum = 0.0,
      nesterov = FALSE,
      local.sgd.iterations = 0,
      ...
  )

Arguments

formula

Required Argument when "input.columns" and "response.column" are not provided, optional otherwise.
Specifies a string consisting of "formula" which is the model to be fitted.
Only basic formula of the "col1 ~ col2 + col3 +..." form are supported and all variables must be from the same tbl_teradata object.
Notes:

  • The function only accepts numeric features. User must convert the categorical features to numeric values, before passing to the formula.

  • In case categorical features are passed to formula, those are ignored, and only numeric features are considered.

  • Provide either "formula" argument or "input.columns" and "response.column" arguments.

Types: character

data

Required Argument.
Specifies the input tbl_teradata.
Types: tbl_teradata

input.columns

Required Argument when "formula" is not provided, optional otherwise.
Specifies the name(s) of the column(s) in "data" to be used for training the model (predictors, features or independent variables).
Note:

  • Provide either "formula" argument or "input.columns" and "response.column" arguments.

Types: character OR vector of Strings (character)

response.column

Required Argument when "formula" is not provided, optional otherwise.
Specifies the name of the column that contains the class label for classification or target value (dependent variable) for regression.
Note:

  • Provide either "formula" argument or "input.columns" and "response.column" arguments.

Types: character

model.type

Optional Argument.
Specifies the type of the analysis.
Permitted Values: 'Regression', 'Classification'
Default Value: 'Classification'
Types: character

iter.max

Optional Argument.
Specifies the maximum number of iterations (mini-batches) over the training data batches.
Note:

  • It must be a positive value less than 10,000,000.

Default Value: 300
Types: integer

epsilon

Optional Argument.
Specifies the epsilon threshold for 'Regression' (the value of epsilon for epsilon_insensitive loss). Any difference between the current prediction and the correct label is ignored within this threshold.
Default Value: 0.1
Types: float OR integer

batch.size

Optional Argument.
Specifies the number of observations (training samples) processed in a single mini-batch per AMP. The value '0' indicates no mini-batches, the entire dataset is processed in each iteration, and the algorithm becomes Gradient Descent. A value higher than the number of rows on any AMP will also default to Gradient Descent.
Note:

  • It must be a non-negative integer value.

Default Value: 10
Types: integer

lambda1

Optional Argument.
Specifies the amount of regularization to be added. The higher the value, the stronger the regularization. It is also used to compute the learning rate when the "learning.rate" is set to 'OPTIMAL'.
A value of '0' means no regularization.
Note:

  • It must be a non-negative float value.

Default Value: 0.02
Types: float OR integer

alpha

Optional Argument.
Specifies the Elasticnet parameter for penalty computation. It only becomes effective when "lambda1" greater than 0. The value represents the contribution ratio of L1 in the penalty. A value '1.0' indicates L1 (LASSO) only, a value '0' indicates L2 (Ridge) only, and a value in between is a combination of L1 and L2.
Note:

  • It must be a float value between 0 and 1.

Default Value: 0.15(15 Types: float OR integer

iter.num.no.change

Optional Argument.
Specifies the number of iterations (mini-batches) with no improvement in loss including the tolerance to stop training. A value of '0' indicates no early stopping and the algorithm continues until "iter.max" iterations are reached.
Note:

  • It must be a non-negative integer value.

Default Value: 50
Types: integer

tolerance

Optional Argument.
Specifies the stopping criteria in terms of loss function improvement.
Notes:

  • Applicable when "iter.num.no.change" is greater than '0'.

  • It must be a positive value.

Default Value: 0.001
Types: float OR integer

intercept

Optional Argument.
Specifies whether intercept should be estimated or not based on
whether "data" is already centered or not.
Default Value: TRUE
Types: logical

class.weights

Optional Argument.
Specifies the weights associated with classes. If the weight of a class is omitted, it is assumed to be 1.0.
Note:

  • Only applicable when "model.type" is set to 'Classification' . The format is '0:weight,1:weight'. For example, '0:1.0,1:0.5' will give twice the weight to each observation in class 0.

Default Value: "0:1.0, 1:1.0"
Types: character

learning.rate

Optional Argument.
Specifies the learning rate algorithm for SGD iterations.
Permitted Values: CONSTANT, OPTIMAL, INVTIME, ADAPTIVE
Default Value:

  • 'INVTIME' when "model.type" is set to 'Regression'

  • 'OPTIMAL' when "model.type" is set to 'Classification'

Types: character

initial.eta

Optional Argument.
Specifies the initial value of eta for the learning rate. When the "learning.rate" is 'CONSTANT', this value is applicable for all iterations.
Default Value: 0.05
Types: float OR integer

decay.rate

Optional Argument.
Specifies the decay rate for the learning rate.
Note:

  • Only applicable for 'INVTIME' and 'ADAPTIVE' learning rates.

Default Value: 0.25
Types: float OR integer

decay.steps

Optional Argument.
Specifies the decay steps (number of iterations) for the 'ADAPTIVE'
learning rate. The learning rate changes by decay rate after the specified number of iterations are completed.
Default Value: 5
Types: integer

momentum

Optional Argument.
Specifies the value to use for the momentum learning rate optimizer.
A larger value indicates a higher momentum contribution.
A value of 0 means the momentum optimizer is disabled. For a good momentum contribution, a value between 0.6-0.95 is recommended.
Note:

  • It must be a non-negative float value between 0 and 1.

Default Value: 0.0
Types: float OR integer

nesterov

Optional Argument.
Specifies whether Nesterov optimization should be applied to the momentum optimizer or not.
Note:

  • Applicable when "momentum" is greater than 0.

Default Value: FALSE
Types: logical

local.sgd.iterations

Optional Argument.
Specifies the number of local iterations to be used for Local SGD algorithm. A value of 0 implies that Local SGD is disabled. A value higher than 0 enables Local SGD and that many local iterations are performed before updating the weights for the global model. With Local SGD algorithm, recommended values for this argument are as follows:

  • local.sgd.iterations: 10

  • iter_max:100

  • batch.size: 50

  • iter.num.no.change: 5

Note:

  • It must be a positive integer value.

Default Value: 0
Types: integer

...

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 SQL Engine function supports, else an exception is raised.

Value

Function returns an object of class "td_svm_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):

  1. result

  2. output.data

Examples

  
    
    # Get the current context/connection.
    con <- td_get_context()$connection
    
    # Load the example data.
    loadExampleData("tdplyr_example", "cal_housing_ex_raw")
    
    # Create tbl_teradata object.
    data_input <- tbl(con, "cal_housing_ex_raw")
    
    # Check the list of available analytic functions.
    display_analytic_functions()
    
    # Scale "target_columns" with respect to 'STD' value of the column.
    fit_obj <- td_scale_fit_sqle(
                data=data_input,
                target.columns=c('MedInc', 'HouseAge', 'AveRooms',
                                 'AveBedrms', 'Population', 'AveOccup',
                                 'Latitude', 'Longitude'),
                scale.method="STD")
    
    # Transform the data.
    transform_obj <- td_scale_transform_sqle(data=data_input,
                                             object=fit_obj$output,
                                             accumulate=c("id", "MedHouseVal"))
    
    
    # Example 1 : Train the transformed data using td_svm_sqle when
    #             "model.type" is 'Regression' and default values provided.
    obj1 <- td_svm_sqle(
              data=transform_obj$result,
              input.columns=c('MedInc', 'HouseAge', 'AveRooms',
                             'AveBedrms', 'Population', 'AveOccup',
                             'Latitude', 'Longitude'),
              response.column="MedHouseVal",
              model.type="Regression"
              )
    
    # Print the result.
    print(obj1$result)
    print(obj1$output.data)
    
    # Example 2 : Train the transformed data using td_svm_sqle() 
    #             when "model.type" is 'Classification' 
    #             and "learning.rate" is 'INV_TIME'.
    obj2 <- td_svm_sqle(
              data=transform_obj$result,
              input.columns=c('MedInc', 'HouseAge', 'AveRooms',
                             'AveBedrms', 'Population', 'AveOccup',
                             'Latitude', 'Longitude'),
              response.column="MedHouseVal",
              model.type="Classification",
              batch.size=12,
              iter.max=301,
              lambda1=0.1,
              alpha=0.5,
              iter.num.no.change=60,
              tolerance=0.01,
              intercept=FALSE,
              class.weights="0:1.0,1:0.5",
              learning.rate="INVTIME",
              initial.data=0.5,
              decay.rate=0.5,
              momentum=0.6,
              nesterov=TRUE,
              local.sgd.iterations=1
              )
    
    # Print the result.
    print(obj2$result)
    print(obj2$output.data)
    
    # Example 3 : Generate linear support vector machine(SVM) when 
    #             "learning.rate" is 'ADAPTIVE' and "class_weight" is
    #             '0:1.0,1:0.5'.
    obj3 <- td_svm_sqle(
              data=transform_obj$result,
              input.columns=c('MedInc', 'HouseAge', 'AveRooms',
                             'AveBedrms', 'Population', 'AveOccup',
                             'Latitude', 'Longitude'),
              response.column="MedHouseVal",
              model.type="Classification",
              batch.size=1,
              iter.max=1,
              lambda1=0.0,
              iter.num.no.change=60,
              tolerance=0.01,
              intercept=FALSE,
              class.weights="0:1.0,1:0.5",
              learning.rate="ADAPTIVE",
              initial.data=0.1,
              decay.rate=0.5,
              momentum=0.7,
              nesterov=TRUE,
              local.sgd.iterations=1
              )
    
    # Print the result.
    print(obj3$result)
    print(obj3$output.data)
    
    # Example 4 : Generate linear support vector machine(SVM) when
    #             "decay.rate" is 0.5 and "model.type" is 'regression'.
    obj4 <- td_svm_sqle(
              data=transform_obj$result,
              input.columns=c('MedInc', 'HouseAge', 'AveRooms',
                             'AveBedrms', 'Population'),
              response.column="MedHouseVal",
              model.type="Regression",
              decay.rate=0.5,
              momentum=0.7,
              nesterov=TRUE,
              local.sgd.iterations=1
              )
    
    # Print the result.
    print(obj4$result)
    print(obj4$output.data)
    
    # Example 5 : Generate linear support vector machine(SVM) using
    #             input tbl_teradata, provided formula and "model.type" is 'regression'.
    formula <- MedHouseVal~MedInc + HouseAge + AveRooms + 
               AveBedrms + Population + AveOccup + Latitude + Longitude
    obj5 <- td_svm_sqle(
              data=transform_obj$result,
              formula=formula,
              model.type="Regression"
              )
    
    # Print the result.
    print(obj5$result)
    print(obj5$output.data)