Example: Deploy and Load Distributed Model | teradataml OpenSourceML - Example: Deploy and Load Distributed Model - Teradata Package for Python

Teradata® Package for Python User Guide

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Teradata Package for Python
Release Number
20.00
Published
December 2024
ft:locale
en-US
ft:lastEdition
2025-01-23
dita:mapPath
nvi1706202040305.ditamap
dita:ditavalPath
plt1683835213376.ditaval
dita:id
rkb1531260709148
lifecycle
latest
Product Category
Teradata Vantage
  1. Train the model.
    1. Import the module.
      >>> from teradataml import td_sklearn as osml
    2. Create an object.
      >>> kmeans = osml.KMeans(n_clusters=4, algorithm="elkan", init="random")
      >>> kmeans.set_params(n_clusters=6, tol=0.1)
      >>> kmeans.fit(X=df_x_clasif, partition_columns=part_cols)
         partition_column_1  partition_column_2                                                            model
      0                   1                  11  KMeans(algorithm='elkan', init='random', n_clusters=6, tol=0.1)
      1                   0                  11  KMeans(algorithm='elkan', init='random', n_clusters=6, tol=0.1)
      2                   1                  10  KMeans(algorithm='elkan', init='random', n_clusters=6, tol=0.1)
      3                   0                  10  KMeans(algorithm='elkan', init='random', n_clusters=6, tol=0.1)
  2. Deploy the model.
    >>> kmeans.deploy(model_name="test_kmeans", replace_if_exists=True)
    Model is deleted.
    Model is saved.
       partition_column_1  partition_column_2                                                            model
    0                   1                  11  KMeans(algorithm='elkan', init='random', n_clusters=6, tol=0.1)
    1                   0                  11  KMeans(algorithm='elkan', init='random', n_clusters=6, tol=0.1)
    2                   1                  10  KMeans(algorithm='elkan', init='random', n_clusters=6, tol=0.1)
    3                   0                  10  KMeans(algorithm='elkan', init='random', n_clusters=6, tol=0.1)
    
    This step replace the model if a model with the same name already exists.
    >>> try:
    >>>    kmeans.deploy(model_name="test_kmeans")
    >>> except Exception as ex:
    >>>    print(str(ex))
    [Teradata][teradataml](TDML_2200) Model with name 'test_kmeans' already exists.
  3. Load the model back in Python session to be used further for prediction.
    >>> loaded = osml.load(model_name="test_kmeans")
    >>> loaded
       partition_column_1  partition_column_2                                                            model
    0                   1                  11  KMeans(algorithm='elkan', init='random', n_clusters=6, tol=0.1)
    1                   0                  11  KMeans(algorithm='elkan', init='random', n_clusters=6, tol=0.1)
    2                   1                  10  KMeans(algorithm='elkan', init='random', n_clusters=6, tol=0.1)
    3                   0                  10  KMeans(algorithm='elkan', init='random', n_clusters=6, tol=0.1)
    
  4. Predict on loaded model.
    Predict on loaded model with partition_columns argument.
    >>> loaded.predict(df_x_clasif, partition_columns=part_cols)
    partition_column_1    partition_column_2                  col1                 col2                 col3                 col4     kmeans_predict_1
                     1                    10      3.03238599297047     3.03481436440056    -2.82355004664948     1.92120455311016                    3
                     1                    10      1.01738017000636     1.33916140701086    -1.82497543774135    0.807901883197634                    3
                     1                    10      1.31804370433225    0.178246937722197     1.89230973992192    0.254523189858355                    1
                     1                    10    -0.651461127752867    -0.63774973705703    0.567676564082086    -0.40549830544753                    0
                     1                    10     -1.17935408605987    -0.06353714170578    -1.95557314394007    -0.17891371408631                    4
                     1                    10    -0.428384248092839    -0.23480968295742    -0.13137227859559    -0.17273019020336                    0
                     1                    10    -0.695735879594567    -1.07084898019651     1.67201893676997    -0.63139004513942                    5
                     1                    10     -1.23995029412835    -1.07952124763549    0.713159403625714    -0.70344346319249                    0
                     1                    10      2.48384809580349     2.37973754066001    -2.02266697958209     1.51968143653911                    3
                     1                    10      1.55458501178875     1.85365285731526    -2.26189922281813      1.1364775082343                    3
    Predict on loaded model without partition_columns.

    df_x_clasif contains both feature columns, and partition columns data picked from fitted model.

    >>> loaded.predict(df_x_clasif)
    partition_column_1    partition_column_2                  col1                 col2                 col3                 col4     kmeans_predict_1
                     1                    10      3.03238599297047     3.03481436440056    -2.82355004664948     1.92120455311016                    3
                     1                    10      1.01738017000636     1.33916140701086    -1.82497543774135    0.807901883197634                    3
                     1                    10      1.31804370433225    0.178246937722197     1.89230973992192    0.254523189858355                    1
                     1                    10    -0.651461127752867    -0.63774973705703    0.567676564082086    -0.40549830544753                    0
                     1                    10     -1.17935408605987    -0.06353714170578    -1.95557314394007    -0.17891371408631                    4
                     1                    10    -0.428384248092839    -0.23480968295742    -0.13137227859559    -0.17273019020336                    0
                     1                    10    -0.695735879594567    -1.07084898019651     1.67201893676997    -0.63139004513942                    5
                     1                    10     -1.23995029412835    -1.07952124763549    0.713159403625714    -0.70344346319249                    0
                     1                    10      2.48384809580349     2.37973754066001    -2.02266697958209     1.51968143653911                    3
                     1                    10      1.55458501178875     1.85365285731526    -2.26189922281813      1.1364775082343                    3