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

VectorDistance

Description

The td_vector_distance_sqle() function accepts a tbl_teradata of target vectors and a tbl_teradata of reference vectors and returns output containing the distance between target-reference pairs.

The function computes the distance between the target pair and the reference pair from the same input if you provide only one input.

You must have the same column order in the "target.feature.columns" argument and the "ref.feature.columns" argument. The function ignores the feature values during distance computation if the value is either NULL, NAN, or INF.

The function returns N*N output if you use the "topk" value as -1 because the function includes all reference vectors in the output.

Notes:

  • The algorithm used in this function is of the order of N*N (where N is the number of rows). Hence, expect the function to run significantly longer as the number of rows increases in either the "target.data" or the "reference.data".

  • Because the reference data is copied to the spool for each AMP before running the query, the user spool limits the size and scalability of the input.

  • This function requires the UTF8 client character set for UNICODE data.

  • This function does not support Pass Through Characters (PTCs). For information about PTCs, see Teradata Vantage™ - Analytics Database International Character Set Support.

  • This function does not support KanjiSJIS or Graphic data types.

Usage

  td_vector_distance_sqle (
      target.data = NULL,
      reference.data = NULL,
      target.id.column = NULL,
      target.feature.columns = NULL,
      ref.id.column = NULL,
      ref.feature.columns = NULL,
      distance.measure = "COSINE",
      topk = 10,
      ...
  )

Arguments

target.data

Required Argument.
Specifies the tbl_teradata containing target data vectors.
Types: tbl_teradata

reference.data

Optional Argument.
Specifies the tbl_teradata containing reference data vectors.
Types: tbl_teradata

target.id.column

Required Argument.
Specifies the name of the "target.data" column that contains identifiers of the target data vectors.
Types: character

target.feature.columns

Required Argument.
Specifies the names of the "target.data" columns that contain features of the target data vectors.
Note:
You can specify up to 2018 feature columns.
Types: character OR vector of Strings (character)

ref.id.column

Optional Argument.
Specifies the name of the "reference.data" column that contains identifiers of the reference data vectors.
Types: character

ref.feature.columns

Optional Argument.
Specifies the names of the "reference.data" columns that contain features of the reference data vectors.
Note:
You can specify up to 2018 feature columns.
Types: character OR vector of Strings (character)

distance.measure

Optional Argument.
Specifies the distance type to compute between the target and the reference vector.
Default Value: "COSINE"
Permitted Values:

  • Cosine: Cosine distance between the target vector and the reference vector.

  • Euclidean: Euclidean distance between the target vector and the reference vector.

  • Manhattan: Manhattan distance between the target vector and the reference vector.

Types: character OR vector of Strings (character)

topk

Optional Argument.
Specifies the maximum number of closest reference vectors to include in the output table for each target vector. The value k is an integer between 1 and 100.
Default Value: 10
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 SQLE Engine function supports, else an exception is raised.

Value

Function returns an object of class "td_vector_distance_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", "target_mobile_data_dense",
                    "ref_mobile_data_dense")
    
    # Create tbl_teradata object.
    target_mobile_data_dense <- tbl(con, "target_mobile_data_dense")
    ref_mobile_data_dense <- tbl(con, "ref_mobile_data_dense")
    
    # Check the list of available analytic functions.
    display_analytic_functions()
    
    # Example 1 : Compute the cosine, euclidean, manhattan distance 
    # between the target and reference vectors.
    VectorDistance_out <- td_vector_distance_sqle(
                            target.id.column="userid",
                            target.feature.columns=c('CallDuration',
                                                     'DataCounter',
                                                     'SMS'),
                            ref.id.column="userid",
                            ref.feature.columns=c('CallDuration',
                                                  'DataCounter',
                                                  'SMS'),
                            distance.measure=c('Cosine', 'Euclidean',
                                               'Manhattan'),
                            topk=2,
                            target.data=target_mobile_data_dense,
                            reference.data=ref_mobile_data_dense)
    
    # Print the result.
    print(VectorDistance_out$result)