TD_NaiveBayes Function Example | Teradata Vantage - Example: How to Use TD_NaiveBayes - 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

Every complete example in this document is available in a zip file that you can download. The zip file includes a SQL script file that creates the input tables for the examples. If you are reading this document on https://docs.teradata.com/, you can download the zip file from the attachment in the left sidebar.

This section shows the input table, SQL query, and output tables of an example using TD_NaiveBayes.

The following table contains a subset of housing dataset.
Only part of the dataset is shown in this example.
DROP TABLE housing_train;
CREATE TABLE housing_train (sn integer, price real, lotsize real, bedrooms  integer, bathrms integer, stories integer, driveway varchar(10), recroom varchar(10), fullbase varchar(10), gashw varchar(10), airco varchar(10), garagepl integer, prefarea varchar(10), homestyle varchar(10)) PRIMARY INDEX(sn);
Insert into housing_train values(1,42000,5850,3,1,2,'yes','no','yes','no','no',1,'no','Classic');
Insert into housing_train values(2,38500,4000,2,1,1,'yes','no','no','no','no',0,'no','Classic');
Insert into housing_train values(3,49500,3060,3,1,1,'yes','no','no','no','no',0,'no','Classic');
Insert into housing_train values(4,60500,6650,3,1,2,'yes','yes','no','no','no',0,'no','Eclectic');
Insert into housing_train values(5,61000,6360,2,1,1,'yes','no','no','no','no',0,'no','Eclectic');
...
SELECT * FROM housing_train ORDER BY 1;

TD_NaiveBayes SQL Call

SELECT * FROM TD_NaiveBayes(
ON housing_train AS InputTable
USING
ResponseColumn('homestyle')
NumericInputs('price','lotsize','bedrooms','bathrms','stories','garagepl')
CategoricalInputs('driveway','recroom','fullbase','gashw','airco','prefarea')
) AS dt ORDER BY 1,2;
TD_NaiveBayes Output Table
class variable type category cnt sum sumsq totalcnt smoothingfactor
bungalow airco CATEGORICAL no 14 ? ? 392 1.72413793103448E-002
bungalow airco CATEGORICAL yes 42 ? ? 392 1.72413793103448E-002
bungalow bathrms NUMERIC ? 56 1.05000000000000E 002 2.19000000000000E 002 392 ?
... ... ... ... ... ... ... ... ...
Classic recroom CATEGORICAL no 133 ? ? 980 7.04225352112676E-003
Classic recroom CATEGORICAL yes 7 ? ? 980 7.04225352112676E-003
Classic stories NUMERIC ? 140 2.07000000000000E 002 3.49000000000000E 002 980 ?
... ... ... ... ... ... ... ... ...
Eclectic recroom CATEGORICAL no 233 ? ? 2072 3.35570469798658E-003
Eclectic recroom CATEGORICAL yes 63 ? ? 2072 3.35570469798658E-003
Eclectic stories NUMERIC ? 296 5.31000000000000E 002 1.15300000000000E 003 2072 ?

As the data is in dense format, you need to use TD_Unpivoting function to change the data to sparse format.

DROP TABLE housing_train_sparse;
CREATE MULTISET TABLE housing_train_sparse AS( 
SELECT * FROM TD_UNPIVOTING(
ON housing_train AS InputTable
USING
IDColumn('sn')
TargetColumns('price','lotsize','bedrooms','bathrms','stories','garagepl','driveway','recroom','fullbase','gashw','airco','prefarea')
AttributeColName('AttributeName')
ValueColName('AttributeValue')
Accumulate('homestyle')
) AS dt) WITH data;

TD_NaiveBayes SQL Call

SELECT * FROM TD_NaiveBayes(
ON housing_train_sparse AS InputTable
USING
ResponseColumn('homestyle') 
AttributeNameColumn('AttributeName') 
AttributeValueColumn('AttributeValue')
NumericAttributes('price','lotsize','bedrooms','bathrms','stories','garagepl')
CategoricalAttributes('driveway','recroom','fullbase','gashw','airco','prefarea')
) AS dt Order by 1,2, 3, 4;
TD_NaiveBayes Output Table
class variable type category cnt sum sumsq totalcnt smoothingfactor
bungalow airco CATEGORICAL no 14 ? ? 392 1.72413793103448E-002
bungalow airco CATEGORICAL yes 42 ? ? 392 1.72413793103448E-002
bungalow bathrms NUMERIC ? 56 1.05000000000000E 002 2.19000000000000E 002 392 ?
... ... ... ... ... ... ... ... ...
Classic recroom CATEGORICAL no 133 ? ? 980 7.04225352112676E-003
Classic recroom CATEGORICAL yes 7 ? ? 980 7.04225352112676E-003
Classic stories NUMERIC ? 140 2.07000000000000E 002 3.49000000000000E 002 980 ?
... ... ... ... ... ... ... ... ...
Eclectic recroom CATEGORICAL no 233 ? ? 2072 3.35570469798658E-003
Eclectic recroom CATEGORICAL yes 63 ? ? 2072 3.35570469798658E-003
Eclectic stories NUMERIC ? 296 5.31000000000000E 002 1.15300000000000E 003 2072 ?