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

Product
Teradata Package for Python
Release Number
17.00
Published
November 2021
Language
English (United States)
Last Update
2021-11-19
lifecycle
previous
Product Category
Teradata Vantage
teradataml.dataframe.dataframe.DataFrame.mlinreg = mlinreg(self, width, sort_column, drop_columns=False)
DESCRIPTION:
    Computes the moving linear regression for the current row and the
    preceding "width"-1 rows in a partition, by sorting the rows
    according to "sort_columns".
    Note:
        mlinreg does not support below type of columns.
            * BLOB
            * BYTE
            * CHAR
            * CLOB
            * DATE
            * PERIOD_DATE
            * PERIOD_TIME
            * PERIOD_TIMESTAMP
            * TIME
            * TIMESTAMP
            * VARBYTE
            * VARCHAR
 
PARAMETERS:
    width:
        Required Argument.
        Specifies the width of the partition. "width" must be
        greater than 2 and less than or equal to 4096.
        Types: int
 
    sort_column:
        Required Argument.
        Specifies the column to use for sorting.
        Note:
            "sort_column" does not support CLOB and BLOB type of
            columns.
        Types: str (or) ColumnExpression
 
    drop_columns:
        Optional Argument.
        Specifies whether to retain all the input DataFrame columns
        in the output or not. When set to False, columns from input
        DataFrame are retained, dropped otherwise.
        Default Value: False
        Types: bool
 
RAISES:
    TeradataMlException, TypeError
 
RETURNS:
    teradataml DataFrame.
 
EXAMPLES:
    # Load the data to run the example.
    >>> from teradataml import load_example_data
    >>> load_example_data("dataframe","admissions_train")
    >>> df = DataFrame('admissions_train')
    >>> print(df)
       masters   gpa     stats programming  admitted
    id
    15     yes  4.00  Advanced    Advanced         1
    7      yes  2.33    Novice      Novice         1
    22     yes  3.46    Novice    Beginner         0
    17      no  3.83  Advanced    Advanced         1
    13      no  4.00  Advanced      Novice         1
    38     yes  2.65  Advanced    Beginner         1
    26     yes  3.57  Advanced    Advanced         1
    5       no  3.44    Novice      Novice         0
    34     yes  3.85  Advanced    Beginner         0
    40     yes  3.95    Novice    Beginner         0
    >>>
 
    # Sorts the Data on column id in descending order and
    # calculates moving linear regression on the window of size 3.
    >>> df.mlinreg(width=3, sort_column=df.id.desc())
       masters   gpa     stats programming  admitted  mlinreg_id  mlinreg_gpa  mlinreg_admitted
    id
    6      yes  3.50  Beginner    Advanced         1         6.0         1.06               1.0
    17      no  3.83  Advanced    Advanced         1        17.0         5.64               2.0
    16      no  3.70  Advanced    Advanced         1        16.0         3.85               1.0
    28      no  3.93  Advanced    Advanced         1        28.0         4.21               0.0
    26     yes  3.57  Advanced    Advanced         1        26.0         3.99              -1.0
    40     yes  3.95    Novice    Beginner         0         NaN          NaN               NaN
    39     yes  3.75  Advanced    Beginner         0         NaN          NaN               NaN
    38     yes  2.65  Advanced    Beginner         1        38.0         3.55               0.0
    27     yes  3.96  Advanced    Advanced         0        27.0         3.86               2.0
    18     yes  3.81  Advanced    Advanced         1        18.0         0.06              -1.0
    >>>
 
    # Sorts the Data on column id in ascending order and
    # calculates moving linear regression by dropping the
    # input DataFrame columns on the window of size 3.
    >>> df.mlinreg(width=3, sort_column=df.id.asc(), drop_columns=True)
       mlinreg_id  mlinreg_gpa  mlinreg_admitted
    0        35.0         4.15              -1.0
    1        24.0         3.72               2.0
    2        25.0         0.15               1.0
    3        13.0         4.17               1.0
    4        15.0         2.90              -1.0
    5         NaN          NaN               NaN
    6         NaN          NaN               NaN
    7         3.0         3.57               0.0
    8        14.0         4.35               1.0
    9        23.0         3.05              -1.0
    >>>