regr_slope
Aggregate function: returns the slope of the linear regression line for non-null pairs in a group, where y is the dependent variable and x is the independent variable.
Syntax
Python
from pyspark.sql import functions as sf
sf.regr_slope(y, x)
Parameters
Parameter | Type | Description |
|---|---|---|
|
| The dependent variable. |
|
| The independent variable. |
Returns
pyspark.sql.Column: the slope of the linear regression line for non-null pairs in a group.
Examples
Example 1: All pairs are non-null
Python
from pyspark.sql import functions as sf
df = spark.sql("SELECT * FROM VALUES (1, 1), (2, 2), (3, 3), (4, 4) AS tab(y, x)")
df.select(sf.regr_slope("y", "x")).show()
Output
+----------------+
|regr_slope(y, x)|
+----------------+
| 1.0|
+----------------+
Example 2: All pairs' x values are null
Python
from pyspark.sql import functions as sf
df = spark.sql("SELECT * FROM VALUES (1, null) AS tab(y, x)")
df.select(sf.regr_slope("y", "x")).show()
Output
+----------------+
|regr_slope(y, x)|
+----------------+
| NULL|
+----------------+
Example 3: All pairs' y values are null
Python
from pyspark.sql import functions as sf
df = spark.sql("SELECT * FROM VALUES (null, 1) AS tab(y, x)")
df.select(sf.regr_slope("y", "x")).show()
Output
+----------------+
|regr_slope(y, x)|
+----------------+
| NULL|
+----------------+