Teradata Package for Python Function Reference | 20.00 - GBM - Teradata Package for Python - Look here for syntax, methods and examples for the functions included in the Teradata Package for Python.

Teradata® Package for Python Function Reference - 20.00

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Teradata Package for Python
Release Number
20.00
Published
March 2024
Language
English (United States)
Last Update
2024-04-10
dita:id
TeradataPython_FxRef_Enterprise_2000
Product Category
Teradata Vantage

H2OPredict() using GBM model.

Setup

In [1]:
import tempfile
import getpass
from teradataml import create_context, DataFrame, save_byom, retrieve_byom, \
delete_byom, list_byom, remove_context, load_example_data, db_drop_table
from teradataml.options.configure import configure
from teradataml.analytics.byom.H2OPredict import H2OPredict
import h2o
In [2]:
# Create the connection.
host = getpass.getpass("Host: ")
username = getpass.getpass("Username: ")
password = getpass.getpass("Password: ")

con = create_context(host=host, username=username, password=password)

Load example data and use sample() for splitting input data into testing and training dataset.

In [3]:
load_example_data("byom", "iris_input")
iris_input = DataFrame("iris_input")

# Create 2 samples of input data - sample 1 will have 80% of total rows and sample 2 will have 20% of total rows. 
iris_sample = iris_input.sample(frac=[0.8, 0.2])
In [4]:
# Create train dataset from sample 1 by filtering on "sampleid" and drop "sampleid" column as it is not required for training model.
iris_train = iris_sample[iris_sample.sampleid == "1"].drop("sampleid", axis = 1)
iris_train
Out[4]:
id sepal_length sepal_width petal_length petal_width species
120 6.0 2.2 5.0 1.5 3
59 6.6 2.9 4.6 1.3 2
99 5.1 2.5 3.0 1.1 2
61 5.0 2.0 3.5 1.0 2
78 6.7 3.0 5.0 1.7 2
101 6.3 3.3 6.0 2.5 3
141 6.7 3.1 5.6 2.4 3
17 5.4 3.9 1.3 0.4 1
38 4.9 3.6 1.4 0.1 1
19 5.7 3.8 1.7 0.3 1
In [5]:
# Create test dataset from sample 2 by filtering on "sampleid" and drop "sampleid" column as it is not required for scoring.
iris_test = iris_sample[iris_sample.sampleid == "2"].drop("sampleid", axis = 1)
iris_test
Out[5]:
id sepal_length sepal_width petal_length petal_width species
7 4.6 3.4 1.4 0.3 1
70 5.6 2.5 3.9 1.1 2
47 5.1 3.8 1.6 0.2 1
108 7.3 2.9 6.3 1.8 3
18 5.1 3.5 1.4 0.3 1
51 7.0 3.2 4.7 1.4 2
68 5.8 2.7 4.1 1.0 2
87 6.7 3.1 4.7 1.5 2
3 4.7 3.2 1.3 0.2 1
74 6.1 2.8 4.7 1.2 2

Prepare dataset for a creating Gradient Boosting Machine model.

In [6]:
h2o.init()
# Since H2OFrame accepts pandas DataFrame, converting teradataml DataFrame to pandas DataFrame.
iris_train_pd = iris_train.to_pandas()
h2o_df = h2o.H2OFrame(iris_train_pd)
h2o_df
Checking whether there is an H2O instance running at http://localhost:54321 . connected.
H2O_cluster_uptime: 3 mins 26 secs
H2O_cluster_timezone: America/Los_Angeles
H2O_data_parsing_timezone: UTC
H2O_cluster_version: 3.32.1.6
H2O_cluster_version_age: 1 month and 21 days
H2O_cluster_name: H2O_from_python_gp186005_ip5q0u
H2O_cluster_total_nodes: 1
H2O_cluster_free_memory: 4.000 Gb
H2O_cluster_total_cores: 12
H2O_cluster_allowed_cores: 12
H2O_cluster_status: locked, healthy
H2O_connection_url: http://localhost:54321
H2O_connection_proxy: {"http": null, "https": null}
H2O_internal_security: False
H2O_API_Extensions: Amazon S3, XGBoost, Algos, AutoML, Core V3, TargetEncoder, Core V4
Python_version: 3.7.3 final
Parse progress: |█████████████████████████████████████████████████████████| 100%
sepal_length sepal_width petal_length petal_width species
5 2 3.5 1 2
6.3 3.3 6 2.5 3
5.1 3.4 1.5 0.2 1
5.6 2.8 4.9 2 3
4.9 3.6 1.4 0.1 1
6.7 3.1 5.6 2.4 3
6 2.2 5 1.5 3
5.7 3.8 1.7 0.3 1
6.7 3 5 1.7 2
5.4 3.9 1.3 0.4 1
Out[6]:

Train Gradient Boosting Machine Model.

In [7]:
# Import required libraries.
from h2o.estimators import H2OGradientBoostingEstimator
In [8]:
# Add the code for training model. 
h2o_df["species"] = h2o_df["species"].asfactor()
predictors = h2o_df.columns
response = "species"
In [9]:
gbm_model = H2OGradientBoostingEstimator(nfolds=5, seed=1111, keep_cross_validation_predictions = True)
In [10]:
gbm_model.train(x=predictors, y=response, training_frame=h2o_df)
gbm Model Build progress: |███████████████████████████████████████████████| 100%

Save the model in MOJO format.

In [11]:
# Saving H2O Model to a file.
temp_dir = tempfile.TemporaryDirectory()
model_file_path = gbm_model.save_mojo(path=f"{temp_dir.name}", force=True)

Save the model in Vantage.

In [12]:
# Save the H2O Model in Vantage.
save_byom(model_id="h2o_gbm_iris", model_file=model_file_path, table_name="byom_models")
Created the model table 'byom_models' as it does not exist.
Model is saved.

List the models from Vantage.

In [13]:
# List the models from "byom_models".
list_byom("byom_models")
                                 model
model_id                              
h2o_gbm_iris  b'504B03041400080808...'

Retrieve the model from Vantage.

In [14]:
# Retrieve the model from vantage using the model name 'h2o_gbm_iris'.
model=retrieve_byom("h2o_gbm_iris", "byom_models")

Set "configure.byom_install_location" to the database where BYOM functions are installed.

In [15]:
configure.byom_install_location = getpass.getpass("byom_install_location: ")

Score the model.

In [16]:
# Score the model on 'iris_test' data.
result = H2OPredict(newdata=iris_test,
                    newdata_partition_column='id',
                    newdata_order_column='id',
                    modeldata=model,
                    modeldata_order_column='model_id',
                    model_output_fields=['label', 'classProbabilities'],
                    accumulate=['id', 'sepal_length', 'petal_length'],
                    overwrite_cached_models='*',
                    enable_options='stageProbabilities',
                    model_type='OpenSource'
                   )
In [17]:
# Print the query.
print(result.show_query())
SELECT * FROM "mldb".H2OPredict(
	ON "MLDB"."ml__select__16344085400480" AS InputTable
	PARTITION BY "id"
	ORDER BY "id" 
	ON (select model_id,model from "MLDB"."ml__filter__16344957882721") AS ModelTable
	DIMENSION
	ORDER BY "model_id"
	USING
	Accumulate('id','sepal_length','petal_length')
	ModelOutputFields('label','classProbabilities')
	OverwriteCachedModel('*')
	EnableOptions('stageProbabilities')
) as sqlmr
In [18]:
# Print the result.
result.result
Out[18]:
id sepal_length petal_length prediction label classprobabilities
39 4.4 1.3 1 1 {"1": 0.9984670598659316,"2": 8.48035477847887E-4,"3": 6.849046562205058E-4}
21 5.4 1.7 1 1 {"1": 0.9986887048769342,"2": 8.077411856592843E-4,"3": 5.035539374064341E-4}
45 5.1 1.9 1 1 {"1": 0.9984626318895735,"2": 0.0010341969303511627,"3": 5.031711800753143E-4}
1 5.1 1.4 1 1 {"1": 0.9989079708314166,"2": 5.881927758866791E-4,"3": 5.03836392696891E-4}
25 4.8 1.9 1 1 {"1": 0.9985815074040594,"2": 7.316813309016052E-4,"3": 6.868112650390427E-4}
13 4.8 1.4 1 1 {"1": 0.9984670598659316,"2": 8.48035477847887E-4,"3": 6.849046562205058E-4}
32 5.4 1.5 1 1 {"1": 0.998077533821194,"2": 0.0014197556779644385,"3": 5.027105008415158E-4}
81 5.5 3.8 2 2 {"1": 0.0014301867800769756,"2": 0.9913702502168862,"3": 0.007199563003036926}
8 5.0 1.5 1 1 {"1": 0.9986866811669394,"2": 8.095375733412047E-4,"3": 5.037812597193045E-4}
6 5.4 1.7 1 1 {"1": 0.99846517157086,"2": 0.0010319025506690632,"3": 5.029258784710217E-4}

Cleanup.

In [19]:
# Delete the saved Model.
delete_byom("h2o_gbm_iris", table_name="byom_models")
Model is deleted.
In [20]:
# Drop model table.
db_drop_table("byom_models")
Out[20]:
True
In [21]:
# Drop input data table.
db_drop_table("iris_input")
Out[21]:
True
In [22]:
# One must run remove_context() to close the connection and garbage collect internally generated objects.
remove_context()
Out[22]:
True
In [ ]: