Score the Predictive Model with APPLY Table Operator | Scoring with a model file | Open Analytics Framework - Scoring the Predictive Model with APPLY Table Operator - Teradata Vantage

Teradata® VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Vantage
Published
January 2023
ft:locale
en-US
ft:lastEdition
2024-12-11
dita:mapPath
phg1621910019905.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
phg1621910019905
Use the Apply class to create a teradataml Apply class object with the characteristics you want to consider for the call to the APPLY Table Operator.
In this example, specify the following:
  • The data argument, so you can specify the input teradataml DataFrame that points to the test data table.
  • The files_local_path argument with the path_to_files variable defined earlier.
  • The apply_command argument to call the Python 3 interpreter in your user environment and execute your script.
  • The returns argument with the list of output variables and types returned by your script.
  • The env_name argument to specify your user environment handler.
Assume your Python script returns scored rows that contain the following variables:
  • An integer variable Cust_ID with a customer ID.
  • A float variable Prob_0 with the probability that a customer will not open a new credit card account.
  • A float variable Prob_1 with the probability that a customer will open a new credit card account.
  • A float variable Actual with the actual outcome for the specific customer.
  1. Call to the Apply class.
    apply_obj = Apply(data = scoringData,
                      apply_command = 'python3 scoring.py',
                      returns = {'Cust_ID': VARCHAR(20), 'Prob_0': VARCHAR(10), 
                                 'Prob_1': VARCHAR(10), 'Actual': VARCHAR(10)},
                      env_name = demo_env,
                     )
    You can request to view SQL queries submitted by teradataml in the background with the following statement:
    display.print_sqlmr_query = True
  2. Run the Python script inside the user environment with the execute_script method of the Apply class object.

    Observe that after running the Python statement, the system prints for you the corresponding SQL query as requested before producing the results.

    apply_obj.execute_script().head(n=5)

    Out:

    SELECT * FROM Apply(
            ON "VCLUSTER"."scoringData" AS "input"
            PARTITION BY ANY
            returns(Cust_ID VARCHAR(20), Prob_0 VARCHAR(10), Prob_1 VARCHAR(10), Actual VARCHAR(10))
            USING
            APPLY_COMMAND('python3 scoring.py')
            ENVIRONMENT('opaf_my_scoring_env')
            STYLE('csv')
            delimiter(',') 
    ) as sqlmr
    Cust_ID      Prob_0   Prob_1  Actual
    13624840         0.0         1.0         1
    13624870         0.0         1.0         1
    13624850         0.0         1.0         1
    13624810         1.0         0.0         0
    13624800         0.0         1.0         1