TD_KNN Classification Example | kNN | Teradata Vantage - Example: Using TD_KNN with Classification - Teradata VantageCloud Lake

Lake - Analyze Your Data with ClearScape Analytics™

Deployment
VantageCloud
Edition
Lake
Product
Teradata VantageCloud Lake
Release Number
Published
February 2025
ft:locale
en-US
ft:lastEdition
2026-02-20
dita:mapPath
tcl1683670667798.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
tcl1683670667798

TD_KNN Input Table for Classification Example

The following example predicts whether the person has diabetes:

id pregnancies glucose bloodpressure skinthickness insulin bmi diabetespedigreefunction age outcome
19 7.0 105.0 0.0 0.0 0.0 0.0 0.305 24.0 0
0 15.0 136.0 70.0 32.0 110.0 37.1 0.153 43.0 1
7 6.0 147.0 80.0 0.0 0.0 29.5 0.17800000000000000 50.0 1
17 3.0 78.0 70.0 0.0 0.0 32.5 0.27 39.0 0
26 2.0 122.0 76.0 27.0 200.0 35.9 0.483 26.0 0
15 6.0 115.0 60.0 39.0 0.0 33.7 0.245 40.0 1
5 1.0 143.0 84.0 23.0 310.0 42.4 1.0760000000000000 22.0 0
13 3.0 125.0 58.0 0.0 0.0 31.6 0.151 24.0 0
24 13.0 106.0 70.0 0.0 0.0 34.2 0.251 52.0 0
11 1.0 100.0 72.0 12.0 70.0 25.3 0.6580000000000000 28.0 0
3 2.0 106.0 64.0 35.0 119.0 30.5 1.4 34.0 0
9 3.0 130.0 78.0 23.0 79.0 28.4 0.32300000000000000 34.0 1
22 12.0 84.0 72.0 31.0 0.0 29.7 0.297 46.0 1
28 5.0 144.0 82.0 26.0 285.0 32.0 0.452 58.0 1
1 0.0 97.0 64.0 36.0 100.0 36.8 0.6 25.0 0
16 6.0 111.0 64.0 39.0 0.0 34.2 0.26 24.0 0
20 3.0 158.0 76.0 36.0 245.0 31.6 0.851 28.0 1
14 1.0 79.0 60.0 42.0 48.0 43.5 0.6780000000000000 23.0 0
18 2.0 89.0 90.0 30.0 0.0 33.5 0.292 42.0 0
12 8.0 120.0 86.0 0.0 0.0 28.4 0.259 22.0 1
8 6.0 129.0 90.0 7.0 326.0 19.6 0.5820000000000000 60.0 0
10 10.0 92.0 62.0 0.0 0.0 25.9 0.16700000000000000 31.0 0
6 9.0 124.0 70.0 33.0 402.0 35.4 0.282 34.0 0
29 6.0 144.0 72.0 27.0 228.0 33.9 0.255 40.0 0
25 4.0 112.0 78.0 40.0 0.0 39.4 0.23600000000000000 38.0 0
27 5.0 124.0 74.0 0.0 0.0 34.0 0.22 38.0 1
4 0.0 123.0 88.0 37.0 0.0 35.2 0.19700000000000000 29.0 0
23 1.0 108.0 60.0 46.0 178.0 35.5 0.415 24.0 0
2 1.0 116.0 70.0 28.0 0.0 27.4 0.204 21.0 0
21 6.0 96.0 0.0 0.0 0.0 23.7 0.19 28.0 0

TD_KNN SQL Call for Classification

SELECT * FROM TD_KNN (
    ON diabetes AS TestTable PARTITION BY ANY
    ON diabetes AS TrainingTable DIMENSION
    USING
        K(3)
        ResponseColumn('outcome')
        InputColumns('[1:8]')
        IDColumn('id')
        ModelType('Classification')
        OutputProb('true')
        EmitNeighbors('true')
        Responses('0', '1')
) AS dt;

TD_KNN Output for Classification

id prediction prob_0 prob_1 neighbor_id1 neighbor_id2 neighbor_id3
0 1 0.3333333333333330 0.6666666666666670 0 3 9
1 0 1.0 0.0 1 3 11
2 0 1.0 0.0 2 16 4
3 0 0.6666666666666670 0.3333333333333330 3 1 0
4 0 1.0 0.0 4 25 2
5 0 0.6666666666666670 0.3333333333333330 5 28 8
6 0 1.0 0.0 6 8 5
7 1 0.0 1.0 7 27 12
8 0 0.6666666666666670 0.3333333333333330 8 28 5
9 1 0.3333333333333330 0.6666666666666670 9 11 0
10 0 1.0 0.0 10 17 24
11 0 0.6666666666666670 0.3333333333333330 11 9 1
12 1 0.3333333333333330 0.6666666666666670 12 27 13
13 1 0.3333333333333330 0.6666666666666670 13 27 12
14 0 1.0 0.0 14 11 1
15 0 0.6666666666666670 0.3333333333333330 15 16 25
16 0 0.6666666666666670 0.3333333333333330 16 2 15
17 0 1.0 0.0 17 10 24
18 0 0.6666666666666670 0.3333333333333330 18 22 25
19 0 1.0 0.0 19 21 10
20 1 0.3333333333333330 0.6666666666666670 20 29 28
21 0 1.0 0.0 21 19 10
22 0 0.6666666666666670 0.3333333333333330 22 18 25
23 0 1.0 0.0 23 26 3
24 0 0.6666666666666670 0.3333333333333330 24 27 10
25 0 0.6666666666666670 0.3333333333333330 25 4 15
26 0 1.0 0.0 26 23 29
27 1 0.3333333333333330 0.6666666666666670 27 12 13
28 0 0.6666666666666670 0.3333333333333330 28 5 8
29 0 0.6666666666666670 0.3333333333333330 29 20 26