Score the Predictive Bear Marketing Model with APPLY Table Operator | Teradata Open Analytics Framework - Score the Prediction Model - Teradata VantageCloud Lake

Lake - Analyze Your Data with ClearScape Analytics™

Deployment
VantageCloud
Edition
Lake
Product
Teradata VantageCloud Lake
Release Number
Published
February 2025
ft:locale
en-US
ft:lastEdition
2026-02-20
dita:mapPath
tcl1683670667798.ditamap
dita:ditavalPath
pny1626732985837.ditaval
dita:id
tcl1683670667798
  1. Do the prediction using the model deployed in Vantage.
    #!/usr/bin/env python3
    import sys
    import pandas as pd
    import pickle
    import xgboost as xgb
    def load_data():
        try:
            return pd.read_csv(sys.stdin, delimiter=",", header=None)
        except Exception as e:
            print(e, file=sys.stderr)
            sys.exit(0)
    def main():
        df = load_data()
        print(f"Number of columns in df: {len(df.columns)}\n", file=sys.stderr)
        print(f"Number of rows in df: {len(df)}\n", file=sys.stderr)
        if df.empty:
            sys.exit(0)
        category_columns_indices = [1,2,3,4,6,7,8,10,15]
        for col in category_columns_indices:
            df[col] = df[col].astype('category')
        test = df
        x_test = test.iloc[:, 0:15]
        y_test = test.iloc[:, 16]
        y_test = y_test.map({'yes': 1, 'no': 0})
        dtest_reg = xgb.DMatrix(x_test, y_test, enable_categorical=True)
        model_file_name = "xgb_model_"
        model_xgb = xgb.Booster()
        model_xgb.load_model(model_file_name)
        predict = model_xgb.predict(
            dtest_reg,
        )
        preds_df = pd.DataFrame(predict, columns=['Predictions'])
        x_test['Predictions'] = preds_df['Predictions'].values
        x_test['Predictions_str'] = x_test['Predictions'].apply(lambda x: 'Yes' if x >= 0.5 else 'No')
        csv_string = x_test.to_csv(index=False, header=False)
        # Remove the last line from the CSV string, it is empty.
        csv_string = '\n'.join(csv_string.split('\n')[:-1])
        print(csv_string, file=sys.stdout)
    if __name__ == "__main__":
        main()
  2. Start the scoring.
    from teradataml import Apply
    display.print_sqlmr_query = True
    apply_obj = Apply(data=bank_df_test,
                     data_partition_column="partition_column_1",
                     apply_command='python3 bank-marketing-predict.py',
                     returns={"age": INTEGER(), "job": VARCHAR(), "marital": VARCHAR(), "education": VARCHAR(), "default_value": VARCHAR(), "balance": INTEGER(), "housing": VARCHAR(), "loan": VARCHAR(), "contact": VARCHAR(), "day_of_month": INTEGER(), "month_of_year": VARCHAR(), "duration": INTEGER(), "campaign": INTEGER(), "pdays": INTEGER(), "previous": INTEGER(), "prediction" : FLOAT(), "Predictions_str" : VARCHAR()}
                    )
    prediction = apply_obj.execute_script()

    Out:

    SELECT * FROM Apply(
    	ON "ALICE"."ml__select__1714117364845315" AS "input"
    	PARTITION BY "partition_column_1"
    	returns(age INTEGER, job LONG VARCHAR, marital LONG VARCHAR, education LONG VARCHAR, default_value LONG VARCHAR, balance INTEGER, housing LONG VARCHAR, loan LONG VARCHAR, contact LONG VARCHAR, day_of_month INTEGER, month_of_year LONG VARCHAR, duration INTEGER, campaign INTEGER, pdays INTEGER, previous INTEGER, prediction FLOAT, Predictions_str LONG VARCHAR)
    	USING
    	APPLY_COMMAND('python3 bank-marketing-predict.py')
    	ENVIRONMENT('bank-marketing-env')
    	STYLE('csv')
    	delimiter(',') 
    ) as sqlmr
  3. View and compare the generated prediction (prediction and Predictions_str columns).
    prediction

    Out:

    age 	job 	marital 	education 	default_value 	balance 	housing 	loan 	contact 	day_of_month 	month_of_year 	duration 	campaign 	pdays 	previous 	prediction 	Predictions_str
    30 	management 	married 	tertiary 	no 	1567 	yes 	no 	cellular 	12 	aug 	1133 	4 	-1 	0 	1.0 	Yes
    30 	services 	divorced 	secondary 	no 	1336 	yes 	no 	cellular 	18 	may 	13 	3 	355 	5 	0.0 	No
    30 	unemployed 	divorced 	secondary 	no 	7105 	no 	no 	cellular 	22 	dec 	321 	2 	-1 	0 	1.0 	Yes
    30 	blue-collar 	married 	primary 	no 	5 	no 	no 	cellular 	23 	dec 	282 	1 	203 	1 	1.0 	Yes
    30 	management 	married 	tertiary 	no 	102 	yes 	no 	cellular 	5 	apr 	470 	7 	426 	3 	1.0 	Yes
    30 	technician 	single 	tertiary 	no 	3086 	yes 	no 	cellular 	7 	may 	84 	3 	168 	1 	0.0 	No
    30 	technician 	married 	secondary 	no 	2278 	no 	yes 	telephone 	19 	nov 	47 	1 	-1 	0 	0.0 	No
    30 	services 	married 	tertiary 	no 	81 	no 	no 	cellular 	22 	apr 	145 	1 	-1 	0 	1.0 	Yes
    30 	technician 	single 	tertiary 	no 	411 	no 	no 	cellular 	22 	oct 	127 	3 	149 	2 	1.0 	Yes
    30 	management 	single 	tertiary 	no 	168 	no 	no 	cellular 	27 	aug 	35 	13 	-1 	0 	0.0 	No