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

KNN

Description

The td_knn_sqle() function classifies data objects based on proximity to other data objects with known categories.

Usage

  td_knn_sqle (
      test.data = NULL,
      train.data = NULL,
      id.column = NULL,
      input.columns = NULL,
      model.type = "classification",
      k = 5,
      accumulate = NULL,
      response.column = NULL,
      voting.weight = 0,
      tolerance = 1.0,
      output.prob = FALSE,
      output.responses = NULL,
      emit.neighbors = NULL,
      emit.distances = FALSE,
      ...
  )

Arguments

test.data

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

train.data

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

id.column

Required Argument.
Specifies the name of the column that uniquely identifies a data object both in train and test data.
Types: character

input.columns

Required Argument.
Specifies the name(s) of the column(s) in train data that the function uses to compute the distance between a test object and the train objects. The test data must also have these columns.
Types: character OR vector of Strings (character)

model.type

Optional Argument.
Specifies the model type for td_knn_sqle function.
Default Value: "classification"
Permitted Values: regression, classification, neighbors
Types: character

k

Optional Argument.
Specifies the number of nearest neighbors to use in the algorithm.
Any positive integer value > 0 and <= 1000 can be chosen.
Default Value: 5
Types: integer

accumulate

Optional Argument.
Specifies the name(s) of the column(s) in test data
to be copied to output.
Types: character OR vector of Strings (character)

response.column

Optional Argument. Required when model type is regression or classification.
Specifies the name of the train data column that contains the
numeric response variable values to be used for prediction in td_knn_sqle based regression or classification.
Types: character

voting.weight

Optional Argument.
Specifies the voting weight of the train object for determining the class of the test object as a function of the distance between the train and test objects. The voting weight is calculated as w, where w=1/POWER(distance, voting.weight) and distance is the distance between the test object and the train object. Must be a non-negative real number.
Default Value: 0
Types: float OR integer

tolerance

Optional Argument.
Specifies the user to define the smallest distance to be considered.
When a non-zero voting weight is used, the case of zero distance causes the weight (w=1/POWER(distance, voting.weight)) to be undefined.
For any distance under the given tolerance, the weight is calculated as w=1/POWER(tolerance, voting.weight).
Default Value: 1.0E-7
Types: float OR integer

output.prob

Optional Argument.
Specifies whether the function should output the probability for each response specified in "response.column". If "response.column" is not given, outputs the probability of the predicted response.
Default Value: FALSE
Types: logical

output.responses

Optional Argument.
Specifies the class labels for which to output probabilities.
Types: character OR vector of Strings (character)

emit.neighbors

Optional Argument.
Specifies whether the neighbors are to be emitted in the output.
Default Value: FALSE
Types: logical

emit.distances

Optional Argument.
Specifies whether the neighbor distances are to be emitted in the output.
Default Value: FALSE
Types: logical

...

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_knn_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("knn_example", "computers_train1_clustered", "computers_test1")
    
    # Create tbl_teradata object.
    computers_test1 <- tbl(con, "computers_test1")
    computers_train1_clustered <- tbl(con, "computers_train1_clustered")
    
    # Check the list of available analytic functions.
    display_analytic_functions()
    
    # Generate fit object for column "computer_category".
    fit_obj <- td_one_hot_encoding_fit_sqle(
                data=computers_train1_clustered,
                is.input.dense=TRUE,
                target.column="computer_category",
                categorical.values=c("ultra", "special"),
                other.column="other")
    
    
    # Encode "ultra" and "special" values of column "computer_category".
    computers_train1_encoded <- td_one_hot_encoding_transform_sqle(
                                  data=computers_train1_clustered,
                                  object=fit_obj$result,
                                  is.input.dense=TRUE)
    
    # Example 1: Map the test computer data to "special" category.
    KNN_out <- td_knn_sqle(
                train.data = computers_train1_encoded$result,
                test.data = computers_test1,
                k = 50,
                response.column = "computer_category_special",
                id.column="id",
                output.prob=FALSE,
                input.columns = c("price", "speed", "hd", "ram", "screen"),
                voting.weight = 1.0,
                emit.distances=FALSE)
    
    # Print the result.
    print(KNN_out$result)
    
    # Example 2: Get the distance of 10 nearest neighbours based on "price", "speed" and "hd".
    KNN_out <- td_knn_sqle(train.data = computers_train1_encoded$result,
                test.data = computers_test1,
                k=10,
                model.type="neighbors",
                id.column="id",
                input.columns = c("price", "speed", "hd"),
                emit.distances=TRUE,
                emit.neighbors=TRUE)
    
    # Print the result.
    print(KNN_out$result)