Teradata Package for Python Function Reference on VantageCloud Lake - regr_avgy - 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
- teradataml.dataframe.window.regr_avgy = regr_avgy(expression)
- DESCRIPTION:
Function returns the mean of the dependent variable for all non-null data
pairs of the dependent and independent variable arguments over the specified
window. The function considers ColumnExpression as an independent variable
and "expression" as a dependent variable.
Note:
When there are fewer than two non-null data point pairs in the data used
for the computation, the function returns None.
PARAMETERS:
expression:
Required Argument.
Specifies a ColumnExpression of a column or name of the column or a literal
representing an independent variable for the regression.
Types: ColumnExpression OR int OR float OR str
RETURNS:
* teradataml DataFrame - When aggregate is executed using window created
on teradataml DataFrame.
* ColumnExpression, also known as, teradataml DataFrameColumn - When aggregate is
executed using window created on ColumnExpression.
RAISES:
RuntimeError - If column does not support the aggregate operation.
EXAMPLES:
# Load the data to run the example.
>>> load_example_data("dataframe", "admissions_train")
>>>
# Create a DataFrame on 'admissions_train' table.
>>> admissions_train = DataFrame("admissions_train")
>>> admissions_train
masters gpa stats programming admitted
id
22 yes 3.46 Novice Beginner 0
36 no 3.00 Advanced Novice 0
15 yes 4.00 Advanced Advanced 1
38 yes 2.65 Advanced Beginner 1
5 no 3.44 Novice Novice 0
17 no 3.83 Advanced Advanced 1
34 yes 3.85 Advanced Beginner 0
13 no 4.00 Advanced Novice 1
26 yes 3.57 Advanced Advanced 1
19 yes 1.98 Advanced Advanced 0
>>>
# Note:
# In the examples here, ColumnExpression is passed as input. User can
# choose to pass column name instead of the ColumnExpression.
# Example 1: Calculate the mean of the column 'admitted' for all
# non-null data pairs with independent variable as 'gpa',
# in a Rolling window, partitioned over 'programming'.
# Create a Rolling window on 'gpa'.
>>> window = admissions_train.gpa.window(partition_columns="programming",
... window_start_point=-2,
... window_end_point=0)
>>>
# Execute regr_avgy() on the Rolling window and attach it to the DataFrame.
# Note: DataFrame.assign() allows combining multiple window aggregate
# operations in one single call. In this example, we are executing
# regr_avgy() along with count() window aggregate operations.
>>> df = admissions_train.assign(regr_avgy_gpa=window.regr_avgy(admissions_train.admitted),
... count_gpa=window.count())
>>> df
masters gpa stats programming admitted count_gpa regr_avgy_gpa
id
9 no 3.82 Advanced Advanced 1 3 3.750000
6 yes 3.50 Beginner Advanced 1 3 3.760000
28 no 3.93 Advanced Advanced 1 3 3.796667
27 yes 3.96 Advanced Advanced 0 3 3.796667
18 yes 3.81 Advanced Advanced 1 3 3.250000
10 no 3.71 Advanced Advanced 1 3 3.166667
1 yes 3.95 Beginner Beginner 0 1 3.950000
40 yes 3.95 Novice Beginner 0 2 3.950000
22 yes 3.46 Novice Beginner 0 3 3.786667
39 yes 3.75 Advanced Beginner 0 3 3.720000
>>>
# Example 2: Calculate the mean of the column 'admitted' for all non-null
# data pairs with independent variable as all valid columns,
# in an Expanding window, partitioned over 'programming',
# and order by 'id' in descending order.
# Create an Expanding window on DataFrame.
>>> window = admissions_train.window(partition_columns=admissions_train.masters,
... order_columns=admissions_train.id.desc(),
... window_start_point=None,
... window_end_point=0)
>>>
# Execute regr_avgy() on the Expanding window.
>>> df = window.regr_avgy(admissions_train.admitted)
>>> df
masters gpa stats programming admitted admitted_regr_avgy gpa_regr_avgy id_regr_avgy
id
38 yes 2.65 Advanced Beginner 1 0.333333 3.450000 39.000000
32 yes 3.46 Advanced Beginner 0 0.200000 3.532000 36.600000
31 yes 3.50 Advanced Beginner 1 0.333333 3.526667 35.666667
30 yes 3.79 Advanced Novice 0 0.285714 3.564286 34.857143
27 yes 3.96 Advanced Advanced 0 0.222222 3.656667 33.333333
26 yes 3.57 Advanced Advanced 1 0.300000 3.648000 32.600000
37 no 3.52 Novice Novice 1 1.000000 3.520000 37.000000
36 no 3.00 Advanced Novice 0 0.500000 3.260000 36.500000
35 no 3.68 Novice Beginner 1 0.666667 3.400000 36.000000
33 no 3.55 Novice Novice 1 0.750000 3.437500 35.250000
>>>
# Example 3: Calculate the mean of the column 'gpa' for all non-null
# data pairs with independent variable as all other columns,
# which are grouped by 'masters' and 'gpa' in a Contracting
# window, partitioned over 'masters' and order by 'masters'
# with nulls listed last.
# Perform group_by() operation on teradataml DataFrame.
>>> group_by_df = admissions_train.groupby(["masters", "gpa"])
# Create a Contracting window on teradataml DataFrameGroupBy object.
>>> window = group_by_df.window(partition_columns="masters",
... order_columns=group_by_df.masters.nulls_first(),
... window_start_point=-5,
... window_end_point=None)
# Execute regr_avgy() on the Contracting window.
>>> window.regr_avgy(admissions_train.gpa)
masters gpa gpa_regr_avgy
0 yes 3.79 3.632500
1 yes 3.50 3.529000
2 yes 3.96 3.554545
3 yes 4.00 3.579167
4 yes 3.90 3.464286
5 yes 2.33 3.464000
6 no 3.52 3.265000
7 no 3.83 3.320000
8 no 3.82 3.405000
9 no 3.55 3.438889
>>>