Teradata Package for Python Function Reference | 20.00 - GLMPredictPerSegment - 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 - 20.00

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
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_Enterprise_2000
lifecycle
latest
Product Category
Teradata Vantage
 
 
GLMPredictPerSegment

 
Functions
       
GLMPredictPerSegment(newdata=None, object=None, id_column=None, accumulate=None, output_prob=False, output_responses=None, partition_column=None, **generic_arguments)
DESCRIPTION:
    The GLMPredictPerSegment() function uses the model generated by
    the GLMPerSegment() function to predict target values (regression)
    and class labels (classification) on new input data.
 
    Notes:
        * All input features must be numeric. The categorical columns should be converted
          to numerical columns as preprocessing step, such as using the following functions:
              * OneHotEncodingFit() and OneHotEncodingTransform().
              * OrdinalEncodingFit() and OrdinalEncodingTransform().
              * TargetEncodingFit() and TargetEncodingTransform().
          The OneHotEncodingFit() and OneHotEncodingTransform() functions support segment
          functions. However, the OrdinalEncodingFit() and OrdinalEncodingTransform(),
          TargetEncodingFit() and TargetEncodingTransform() functions do not support segment
          functions. You must run these functions one-by-one on each partition.
        * The preprocessing steps carried out for GLMPerSegment() should be done for
          the test data set as well before prediction.
        * Prediction accuracy metrics such as MSE, precision, recall, ROC are not
          generated by the function. The user should use RegressionEvaluator(),
          ClassificationEvaluator() and ROC() functions as post-processing steps.
          These functions do not support segment functions and the workaround is to run
          these functions one by one on each partition.
        * Any observation with missing value in an input column is ignored and it shows
          in the output with specific error code. User can use some imputation function,
          such as SimpleImputeFit() and SimpleImputeTransform() to do imputation or filling
          of missing values.
 
 
PARAMETERS:
    newdata:
        Required Argument.
        Specifies the teradataml DataFrame containing the input data.
        Types: teradataml DataFrame
 
    object:
        Required Argument.
        Specifies the teradataml DataFrame containing the model data
        generated by GLMPerSegment() function or an instance of GLMPerSegment.
        Types: teradataml DataFrame or GLMPerSegment
 
    id_column:
        Required Argument.
        Specifies the input data column name that uniquely
        identifies an observation in the input data.
        Types: str
 
    accumulate:
        Optional Argument.
        Specifies the name(s) of input teradataml DataFrame column(s) to copy to the
        output.
        Types: str OR list of Strings (str)
 
    output_prob:
        Optional Argument.
        Specifies whether function should output the probability for each response.
        Default Value: False
        Types: bool
 
    output_responses:
        Optional Argument.
        Specifies the class labels for output probabilities. A label must be 0 or 1.
        If not specified, then the function outputs the probability of the predicted response.
        Note:
            * The "output_responses" argument works only when "output_prob"
              is True.
        Types: str OR list of Strings (str)
 
    partition_column:
        Optional Argument.
        Specifies the name of the "input_data" columns on which to partition the input.
        The name should be consistent with the "data_partition_column" name. If the
        "data_partition_column" is unicode with foreign language characters, then it is
        necessary to specify "partition_column" argument.
        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 GLMPredictPerSegment.
    Output teradataml DataFrames can be accessed using attribute
    references, such as GLMPredictPerSegmentObj.<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("decisionforestpredict", ["housing_train", "housing_test"])
 
    # Create teradataml DataFrame objects.
    housing_train = DataFrame.from_table("housing_train")
    housing_test = DataFrame.from_table("housing_test")
 
    # Check the list of available analytic functions.
    display_analytic_functions()
 
    # Filter the rows from train dataset with homestyle as Classic and Eclectic.
    binomial_housing_train = DataFrame('housing_train', index_label="homestyle")
    binomial_housing_train = binomial_housing_train.filter(like = 'ic', axis = 'rows')
 
    binomial_housing_test = DataFrame('housing_test', index_label="homestyle")
    binomial_housing_test = binomial_housing_test.filter(like = 'ic', axis = 'rows')
 
    # GLMPerSegment() function requires features in numeric format for processing,
    # so dropping the non-numeric columns.
    binomial_housing_train = binomial_housing_train.drop(columns=["driveway", "recroom",
                                                                  "gashw", "airco", "prefarea",
                                                                  "fullbase"])
    gaussian_housing_train = binomial_housing_train.drop(columns="homestyle")
 
    binomial_housing_test = binomial_housing_test.drop(columns=["driveway", "recroom",
                                                                "gashw", "airco", "prefarea",
                                                                "fullbase"])
    gaussian_housing_test = binomial_housing_test.drop(columns="homestyle")
 
    # Transform the train dataset categorical values to encoded values.
    housing_train_ordinal_encodingfit = OrdinalEncodingFit(
        target_column='homestyle',
        data=binomial_housing_train)
 
    housing_train_ordinal_encodingtransform = OrdinalEncodingTransform(
        data=binomial_housing_train,
        object=housing_train_ordinal_encodingfit.result,
        accumulate=["sn", "price", "lotsize",
                    "bedrooms", "bathrms", "stories"])
 
    housing_test_ordinal_encodingfit = OrdinalEncodingFit(
        target_column='homestyle',
        data=binomial_housing_test)
 
    housing_test_ordinal_encodingtransform = OrdinalEncodingTransform(
        data=binomial_housing_test,
        object=housing_test_ordinal_encodingfit.result,
        accumulate=["sn", "price", "lotsize",
                    "bedrooms", "bathrms", "stories"])
 
    # Example 1: Train the model using the 'Gaussian' family.
    #            Predict the price using GLMPredictPerSegment().
 
    # Train the model using the 'Gaussian' family.
    GLMPerSegment_out_1 = GLMPerSegment(data=gaussian_housing_train,
                                        data_partition_column="stories",
                                        input_columns=['garagepl', 'lotsize', 'bedrooms', 'bathrms'],
                                        response_column="price",
                                        family="Gaussian",
                                        iter_max=1000,
                                        batch_size=9)
 
    # Predict the price using GLMPredictPerSegment().
    GLMPredictPerSegment_out_1 = GLMPredictPerSegment(newdata=gaussian_housing_test,
                                                      newdata_partition_column="stories",
                                                      object=GLMPerSegment_out_1,
                                                      object_partition_column="stories",
                                                      id_column="sn")
 
    # Print the result DataFrame.
    print(GLMPredictPerSegment_out_1.result)
 
    # Example 2: Train the model using the 'Binomial' family.
    #            Predict the homestyle using GLMPredictPerSegment().
 
    # Train the model using the 'Binomial' family.
    GLMPerSegment_out_2 = GLMPerSegment(data=housing_train_ordinal_encodingtransform.result,
                                        data_partition_column="stories",
                                        input_columns=['price', 'lotsize', 'bedrooms', 'bathrms'],
                                        response_column="homestyle",
                                        family="Binomial",
                                        iter_max=100)
 
    # Predict the homestyle using GLMPredictPerSegment().
    GLMPredictPerSegment_out_2 = GLMPredictPerSegment(
        newdata=housing_test_ordinal_encodingtransform.result,
        newdata_partition_column="stories",
        object=GLMPerSegment_out_2,
        object_partition_column="stories",
        id_column="sn",
        output_prob=True,
        output_responses=["0", "1"]
        )
 
    # Print the result DataFrame.
    print(GLMPredictPerSegment_out_2.result)