Teradata Package for Python Function Reference on VantageCloud Lake - TDGLMPredict - 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 on VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Package for Python
Release Number
20.00.00.03
Published
December 2024
ft:locale
en-US
ft:lastEdition
2024-12-19
dita:id
TeradataPython_FxRef_Lake_2000
Product Category
Teradata Vantage
 
 
TDGLMPredict

 
Functions
       
TDGLMPredict(object=None, newdata=None, id_column=None, accumulate=None, output_prob=False, output_responses=None, partition_column=None, family='GAUSSIAN', **generic_arguments)
DESCRIPTION:
    The TDGLMPredict() function predicts target values (regression) and class labels
    (classification) for test data using a model generated by the GLM().
    Notes:
        * Before using the features in the function, user must standardize the Input features
            using ScaleFit() and ScaleTransform() functions.
        * The function only accepts numeric features. Therefore, user must convert the categorical
            features to numeric values before prediction.
        * The function skips the rows with missing (null) values during prediction.
        * User can use RegressionEvaluator(), ClassificationEvaluator(), or ROC() function as a
            post-processing step for evaluating prediction results.
        * The TDGLMPredict() function accepts models from GLM() function in SQLE.
 
 
PARAMETERS:
    object:
        Required Argument.
        Specifies the teradataml DataFrame containing the model data generated by GLM()
        function or the instance of GLM.
        Types: teradataml DataFrame or GLM
 
    newdata:
        Required Argument.
        Specifies the teradataml DataFrame containing the input data.
        Types: teradataml DataFrame
 
    id_column:
        Required Argument.
        Specifies the name of the column that uniquely identifies an
        observation in the test data.
        Types: str
 
    accumulate:
        Optional Argument.
        Specifies the name(s) of input teradataml DataFrame column(s) to copy to the
        output. By default, the function copies no input teradataml DataFrame columns
        to the output.
        Types: str OR list of Strings (str)
 
    output_prob:
        Optional Argument.
        Specifies whether the function should output the probability for each
        response.
        Note:
            Only applicable if the "family" is 'Binomial'.
        Default Value: False
        Types: bool
 
    output_responses:
        Optional Argument.
        Specifies the class labels for which to output probabilities.
        A label must be 0 or 1. If not specified, the function outputs the
        probability of the predicted response.
        Note:
            Only applicable if "output_prob" is True.
        Types: str OR list of strs
    
    partition_column:
        Optional Argument.
        Specifies the column names of "data" on which to partition the input.
        Types: str OR list of Strings (str)
 
    family:
        Optional Argument.
        Specifies the distribution exponential family.
        Permitted Values: 'GAUSSIAN', 'BINOMIAL'
        Default Value: 'GAUSSIAN'
        Types: str
 
    **generic_arguments:
        Specifies the generic keyword arguments SQLE functions accept. Below 
        are the generic keyword arguments:
            persist:
                Optional Argument.
                Specifies whether to persist the results of the 
                function in a table or not. When set to True,
                results are persisted in a table; otherwise,
                results are garbage collected at the end of the 
                session.
                Default Value: False
                Types: bool
            
            volatile:
                Optional Argument.
                Specifies whether to put the results of the 
                function in a volatile table or not. When set to
                True, results are stored in a volatile table,
                otherwise not.
                Default Value: False
                Types: bool
                
        Function allows the user to partition, hash, order or local 
        order the input data. These generic arguments are available 
        for each argument that accepts teradataml DataFrame as 
        input and can be accessed as:    
            * "<input_data_arg_name>_partition_column" accepts str or 
                list of str (Strings)
            * "<input_data_arg_name>_hash_column" accepts str or list 
                of str (Strings)
            * "<input_data_arg_name>_order_column" accepts str or list 
                of str (Strings)
            * "local_order_<input_data_arg_name>" accepts boolean
        Note:
            These generic arguments are supported by teradataml if 
            the underlying SQLE Engine function supports, else an 
            exception is raised.
 
RETURNS:
    Instance of TDGLMPredict.
    Output teradataml DataFrames can be accessed using attribute 
    references, such as TDGLMPredictObj.<attribute_name>.
    Output teradataml DataFrame attribute name is:
        result
 
 
RAISES:
    TeradataMlException, TypeError, ValueError
 
 
EXAMPLES:
    # Notes:
    #     1. Get the connection to Vantage to execute the function.
    #     2. One must import the required functions mentioned in
    #        the example from teradataml.
    #     3. Function will raise error if not supported on the Vantage
    #        user is connected to.
    
    # Load the example data.
    load_example_data("teradataml", "cal_housing_ex_raw")
    
    # Create teradataml DataFrame objects.
    df=DataFrame.from_table("cal_housing_ex_raw")
    
    # Check the list of available analytic functions.
    display_analytic_functions()
 
    # Example 1: This example takes raw housing data, and does the following
    #            Uses ScaleFit() to standardize the data.
    #            Uses ScaleTransform() to transform the data.
    #            Uses GLM() to generate a model.
    #            Uses TDGLMPredict() to predict target values.
 
    # Scale "target_columns" with respect to 'STD' value of the column.
    fit_obj = ScaleFit(data=df,
                       target_columns=['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup',
                       'Latitude', 'Longitude'],
                       scale_method="STD")
 
    # Scale values specified in the input data using the fit data generated by the Scale() function above.
    obj = ScaleTransform(object=fit_obj.output,
                         data=df,
                         accumulate=["id","MedHouseVal"])
 
    # Generate regression model using generalized linear model(GLM).
    answer = GLM(input_columns=["MedInc", "HouseAge", "AveRooms", "AveBedrms", "Population", "AveOccup",
                "Latitude", "Longitude"],
                response_column="MedHouseVal",
                data=obj.result,
                nesterov=False)
 
    # TDGLMPredict() predicts 'MedHouseVal' using generated regression model by GLM and newdata.
    # Note that teradataml DataFrame representing the model is passed as input to "object".
    TDGLMPredict_out = TDGLMPredict(newdata=obj.result,
                                    object=answer.result,
                                    accumulate="MedHouseVal",
                                    id_column="id")
 
    # Print the result DataFrame.
    print(TDGLMPredict_out.result)
 
    # Example 2: TDGLMPredict() predicts the 'MedHouseVal' using generated regression model
    #            by GLM and newdata. Note that model is passed as instance of GLM to "object".
    TDGLMPredict_out1 = TDGLMPredict(newdata=obj.result,
                                     object=answer,
                                     accumulate="MedHouseVal",
                                     id_column="id")
 
    # Print the result DataFrame.
    print(TDGLMPredict_out1.result)
 
    # Example 3 : TDGLMPredict() predicts the 'medv' using generated regression model by GLM
    #             using stepwise regression algorithm.   
    #             This example uses the boston dataset and scales the data.
    #             Scaled data is used as input data to generate the GLM model and predict the target values.
 
    # loading the example data
    load_example_data("decisionforest", ["boston"])
    load_example_data('glm', ['housing_train_segment', 'housing_train_parameter', 'housing_train_attribute'])
 
    # Create teradataml DataFrame objects.
    boston_df = DataFrame('boston')
    housing_seg = DataFrame('housing_train_segment')
    housing_parameter = DataFrame('housing_train_parameter')
    housing_attribute = DataFrame('housing_train_attribute')
 
    # scaling the data
    # Scale "target_columns" with respect to 'STD' value of the column.
    fit_obj = ScaleFit(data=boston_df,
                    target_columns=['crim','zn','indus','chas','nox','rm','age','dis','rad','tax','ptratio','black','lstat',],
                    scale_method="STD")
 
    # Scale values specified in the input data using the fit data generated by the ScaleFit() function above.
    obj = ScaleTransform(object=fit_obj.output,
                        data=boston_df,
                        accumulate=["id","medv"])
 
    boston = obj.result
 
    # Generate generalized linear model(GLM) using stepwise regression algorithm.
    glm_1 = GLM(data=boston,
                input_columns=['indus','chas','nox','rm'],
                response_column='medv',
                family='GAUSSIAN',
                lambda1=0.02,
                alpha=0.33,
                batch_size=10,
                learning_rate='optimal',
                iter_max=36,
                iter_num_no_change=100,
                tolerance=0.0001,
                initial_eta=0.02,
                stepwise_direction='backward',
                max_steps_num=10)
    
    # Predict target values using generated regression model by GLM and newdata.
    res = TDGLMPredict(id_column="id",
                        newdata=boston,
                        object=glm_1,
                        accumulate='medv')
    
    # Print the result DataFrame.
    print(res.result)
 
    # Example 4 : TDGLMPredict() predicts the 'medv' using generated regression model by GLM
    #             stepwise regression algorithm with initial_stepwise_columns.
    glm_2 = GLM(data=boston,
                input_columns=['crim','zn','indus','chas','nox','rm','age','dis','rad','tax','ptratio','black','lstat'],
                response_column='medv',
                family='GAUSSIAN',
                lambda1=0.02,
                alpha=0.33,
                batch_size=10,
                learning_rate='optimal',
                iter_max=36,
                iter_num_no_change=100,
                tolerance=0.0001,
                initial_eta=0.02,
                stepwise_direction='bidirectional',
                max_steps_num=10,
                initial_stepwise_columns=['rad','tax']
        )
 
    # Predict target values using generated regression model by GLM and newdata.
    res = TDGLMPredict(id_column="id",
                        newdata=boston,
                        object=glm_2,
                        accumulate='medv')
 
    # Print the result DataFrame.
    print(res.result)
 
    # Example 5 : TDGLMPredict() predicts the 'price' using generated regression model by GLM
    #             using partition by key.
    glm_3 = GLM(data=housing_seg,
                input_columns=['bedrooms', 'bathrms', 'stories', 'driveway', 'recroom', 'fullbase', 'gashw', 'airco'],
                response_column='price',
                family='GAUSSIAN',
                batch_size=10,
                iter_max=1000,
                data_partition_column='partition_id'
        )
 
    # Predict target values using generated regression model by GLM and newdata.
    res = TDGLMPredict(id_column="sn",
                        newdata=housing_seg,
                        object=glm_3,
                        accumulate='price',
                        newdata_partition_column='partition_id',
                        object_partition_column='partition_id')
    
    # Print the result DataFrame.
    print(res.result)
 
    # Example 6 : TDGLMPredict() predicts the 'price' using generated regression model by GLM
    #             using partition by key with attribute data.
    glm_4 = GLM(data=housing_seg,
                input_columns=['bedrooms', 'bathrms', 'stories', 'driveway', 'recroom', 'fullbase', 'gashw', 'airco'],
                response_column='price',
                family='GAUSSIAN',
                batch_size=10,
                iter_max=1000,
                data_partition_column='partition_id',
                attribute_data = housing_attribute,
                attribute_data_partition_column = 'partition_id'
        )
 
    # Predict target values using generated regression model by GLM and newdata.
    res = TDGLMPredict(id_column="sn",
            newdata=housing_seg,
            object=glm_4,
            accumulate='price',
            newdata_partition_column='partition_id',
            object_partition_column='partition_id')
    
    # Print the result DataFrame.
    print(res.result)
 
    # Example 7 : TDGLMPredict() predicts the 'homestyle' using generated generalized linear model by GLM
    #             using partition by key with parameter data.
    glm_5 = GLM(data=housing_seg,
                input_columns=['bedrooms', 'bathrms', 'stories', 'driveway', 'recroom', 'fullbase', 'gashw', 'airco'],
                response_column='homestyle',
                family='binomial',
                iter_max=1000,
                data_partition_column='partition_id',
                parameter_data = housing_parameter,
                parameter_data_partition_column = 'partition_id'
        )
 
    res = TDGLMPredict(id_column="sn",
                        newdata=housing_seg,
                        object=glm_5,
                        accumulate='homestyle',
                        newdata_partition_column='partition_id',
                        object_partition_column='partition_id')
    
    # Print the result DataFrame.
    print(res.result)