TD_KNN Neighbors Example | kNN | Teradata Vantage - Example: Using TD_KNN with Neighbors - Teradata Vantage

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
Language
English (United States)
Last Update
2024-04-03
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905

TD_KNN Input training table and test table

The following example displays the nearest neighbors for each data point:

sn price lotsize bedrooms bathrms stories driveway recroom fullbase gashw airco garagepl prefarea homestyle partition_id
1 42000 5850 3 1 2 1 0 1 0 0 1 0 0 31
2 38500 4000 2 1 1 1 0 0 0 0 0 0 0 31
3 49500 3060 3 1 1 1 0 0 0 0 0 0 0 31
4 60500 6650 3 1 2 1 1 0 0 0 0 0 1 31
5 61000 6360 2 1 1 1 0 0 0 0 0 0 1 31
…...         ..       ..          
…....         ..       ..          
130 127000 4600 3 2 2 1 1 0 0 1 2 0 0 31
131 35000 4840 2 1 2 1 0 0 0 0 0 0 0 31
132 40500 3300 3 1 2 0 0 0 0 0 1 0 0 31
133 49900 4900 3 1 2 0 0 0 0 0 0 0 0 31
134 50500 3850 3 1 1 1 0 0 0 0 2 0 1 31

Example: TD_KNN SQL Call for Neighbors

SELECT * FROM TD_KNN(
ON housing_train AS TestTable PARTITION BY ANY
ON housing_train AS TrainingTable DIMENSION
USING     
k(5)
InputColumns('[1:5]') 
IDColumn('sn')     
ModelType('Neighbors')  
) AS dt;   

TD_KNN Output Table for Neighbors

sn neighbor_id1 neighbor_id2 neighbor_id3 neighbor_id4 neighbor_id5
1 106 28 27 18 1
2 23 55 2 24 15
3 3 83 134 81 33
4 4 5 66 35 87
5 35 4 5 87 66
…...   ...   ..  
…...   ...   ..  
130 129 103 130 94 95
131 99 131 59 82 75
132 17 13 16 25 38
133 134 133 81 83 33
134 81 134 83 72 34