Hyperparameter Tuning operations on Non-Model Trainer Function | GridSearch - Example 3: Hyperparameter Tuning Operations on Non-Model Trainer Function - Teradata Package for Python

Teradata® Package for Python User Guide

Teradata Package for Python
Release Number
March 2024
English (United States)
Last Update
Product Category
Teradata Vantage

teradataml offers hyper-parameterization of parameters for non-model trainer functions using GridSearch algorithm. This example makes use of hyper-parameterization feature to perform Antiselect function on titanic data.

In this example, teradataml example titanic data is used to perform Antiselect function.

  1. Example setup.
    1. Load the example dataset.
      >>> load_example_data("teradataml", "titanic")
    2. Create teradataml DataFrame.
      >>> titanic = DataFrame.from_table("titanic")
    3. Slice input data for Hyper-parameterization of data.
      >>> train_df1 = titanic.iloc[:200]
      >>> train_df2 = titanic.iloc[200:]
  2. Define hyperparameter-tuning for Antiselect (non-model trainer function).
    1. Define the non-model trainer function parameter space. Include input data in parameter space for non-model trainer function.
      >>> params = {"data":(train_df1, train_df2), "exclude":(
                    ['survived', 'name', 'age'],
                    ['survived', 'age'],
                    ["ticket", "parch", "age"],
                    ["ticket", "parch", "sex", "age"])}
      Any argument in 'params' can be hyper-parameterized.
    2. Import non-model trainer function and optimizer.
      >>> from teradataml import Antiselect, GridSearch
    3. Initialize the GridSearch optimizer with non-model trainer function and parameter space required for non-model training.
      >>> gs_obj = GridSearch(func=Antiselect, params=params)
  3. Perform execution of Antiselect function in sequential mode, and enable progress bar by setting verbose level to “1”.
    >>> gs_obj.fit(run_parallel=True, verbose=1)
    completed: |████████████████████████████████████████████████████████████| 100% - 8/8
    Best model selection is not supported by hyperparameter tuning for non-model trainer function.
  4. View the non-model trainer function execution metadata. Retrieve the model metadata of "gs_obj" instance.
    >>> gs_obj.models
           MODEL_ID                                         PARAMETERS STATUS
    0  ANTISELECT_0  {'data': '"ALICE"."ml__select__169839646305843      PASS
    1  ANTISELECT_3  {'data': '"ALICE"."ml__select__169839646305843      PASS
    2  ANTISELECT_2  {'data': '"ALICE"."ml__select__169839646305843      PASS
    3  ANTISELECT_1  {'data': '"ALICE"."ml__select__169839646305843      PASS
    4  ANTISELECT_4  {'data': '"ALICE"."ml__select__169840161255008      PASS
    5  ANTISELECT_5  {'data': '"ALICE"."ml__select__169840161255008      PASS
    6  ANTISELECT_6  {'data': '"ALICE"."ml__select__169840161255008      PASS
    7  ANTISELECT_7  {'data': '"ALICE"."ml__select__169840161255008      PASS
    All model training has been passed. In case of failure, use get_error_log method to retrieve corresponding error logs.
  5. Retrieve the parameter grid for non-model trainer function. Retrieve "gs_obj" object's parameter grid.
    >>> pprint.pprint(gs_obj.get_parameter_grid())
    [{'data': '"ALICE"."ml__select__169839646305843"',
      'exclude': ['survived', 'name', 'age']},
     {'data': '"ALICE"."ml__select__169839646305843"',
      'exclude': ['survived', 'age']},
     {'data': '"ALICE"."ml__select__169839646305843"',
      'exclude': ['ticket', 'parch', 'age']},
     {'data': '"ALICE"."ml__select__169839646305843"',
      'exclude': ['ticket', 'parch', 'sex', 'age']},
     {'data': '"ALICE"."ml__select__169840161255008"',
      'exclude': ['survived', 'name', 'age']},
     {'data': '"ALICE"."ml__select__169840161255008"',
      'exclude': ['survived', 'age']},
     {'data': '"ALICE"."ml__select__169840161255008"',
      'exclude': ['ticket', 'parch', 'age']},
     {'data': '"ALICE"."ml__select__169840161255008"',
      'exclude': ['ticket', 'parch', 'sex', 'age']}]
  6. Get the non-model function execution result from GridSearch instance "ANTISELECT_3".
    >>> gs_obj.get_model("ANTISELECT_3")
    ############ result Output ############
       passenger  survived  pclass                                                 name  sibsp     fare cabin embarked
    0          3         1       3                               Heikkinen, Miss. Laina      0   7.9250  None        S
    1          5         0       3                             Allen, Mr. William Henry      0   8.0500  None        S
    2          6         0       3                                     Moran, Mr. James      0   8.4583  None        Q
    3          7         0       1                              McCarthy, Mr. Timothy J      0  51.8625   E46        S
    4          9         1       3    Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg)      0  11.1333  None        S
    5         10         1       2                  Nasser, Mrs. Nicholas (Adele Achem)      1  30.0708  None        C
    6          8         0       3                       Palsson, Master. Gosta Leonard      3  21.0750  None        S
    7          4         1       1         Futrelle, Mrs. Jacques Heath (Lily May Peel)      1  53.1000  C123        S
    8          2         1       1  Cumings, Mrs. John Bradley (Florence Briggs Thayer)      1  71.2833   C85        C
    9          1         0       3                              Braund, Mr. Owen Harris      1   7.2500  None        S