TD_KNN Classification Example | kNN | Teradata Vantage - Example: Using TD_KNN with Classification - Analytics Database

Database Analytic Functions

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Analytics Database
Release Number
17.20
Published
June 2022
Language
English (United States)
Last Update
2024-04-06
dita:mapPath
gjn1627595495337.ditamap
dita:ditavalPath
ayr1485454803741.ditaval
dita:id
jmh1512506877710
Product Category
Teradata Vantageā„¢

TD_KNN Input Table for Classification

encoded ROW_I attribute_1 attribute_2 attribute_3 ... attribute_49 sample_id
0 99 -0.0664 -0.0999 -0.0949 ... -0.0942 2
0 101 -0.603 -0.0938 -0.0900 ... -0.0935 2
1 114 0.0000 0.0001 0.0001 ... 0.0001 2
1 115 0.0001 0.0001 0.0001 ... 0.0001 2
... ... ... ... ... ... ... ...

Example: TD_KNN SQL Call for Classification

CREATE VOLATILE TABLE KNN AS (
SELECT * FROM TD_KNN (
    ON test_dataset AS TestTable PARTITION BY ANY
    ON train_dataset AS TrainingTable DIMENSION
    USING
        K(3)
        ResponseColumn('encoded')
        InputColumns('[2:7]')
        IDColumn('Row_I')
        Accumulate ('encoded')
        ModelType('Classification')
        OutputProb('true')
        EmitNeighbors('true')
        Responses('0', '1')
) AS dt
)WITH DATA
ON COMMIT PRESERVE ROWS;

TD_KNN Output Table for Classification

SELECT * FROM KNN;
ROW_I prediction prob_0 prob_1 neighbor_id1 neighbor_id2 neighbor_id3 encoded
43 1 0.3333 0.6666 146 42 145 0
101 0 1.0 0.0 100 102 103 0
150 0 0.6666 0.3333 48 47 128 1
192 1 0.0 1.0 191 193 190 1