The Python statistics.linear_regression() function returns the slope and intercept of simple linear parameters using least squares.
Simple linear regression describes the relationship between an independent variable x and y in terms of the linear function i.e., represented as:
y = slope*x + intercept + noice
Here, slope and intercepts are the regression parameters that are estimated and noice represents the variability of the data that was given in the linear regression. These values are equal to the difference between predicted and actual values of the variables.
Input variables must be in the same length and the independent variable x cannot be a constant; otherwise this throws StatisticsError.
SyntaxFollowing is the basic syntax for the statistics.linear_regression() function.
statistics.linear_regression(x, y, /, *, proportional = False)Parameters
This function takes x and y values whereas, x is independent and y is dependent variable.
Return ValueRegression function returns the slope and intercept.
Example 1Here, we are demonstrating statistics.linear_regression() function to determine the x and y values.
import statistics x = [0, 1, 2, 3, 4, 5, 6, 7] y = [12, 13, 12, 15, 16, 17, 18, 19] slope, intercept = statistics.linear_regression(x, y) print("slope - ", slope) print("Intercept - ", intercept)Output
The result is produced as follows −
slope - 1.0714285714285714 Intercept - 11.5Example 2
In the below example we are calculating negative numbers using statistics.linear_regression() function.
import statistics x = [-3, -4, -5, -7, -9, -4, -2] y = [-13, -31, -14, -56, -35, -43, -23] slope, intercept = statistics.linear_regression(x, y) print("slope - ", slope) print("Intercept -", intercept)Output
We will get the output as follows −
slope - 3.262295081967214 Intercept - -14.868852459016392Example 3
In the following example we are predicting the cumulative number of Monty Python films that would have been produced by 2019 assuming that they had kept the peace.
import statistics x = [1971, 1975, 1979, 1982, 1983] y = [1, 2, 3, 4, 5] slope, intercept = statistics.linear_regression(x, y) print(round(slope * 2019 + intercept))Output
This will produce the following result −
16Example 4
Here, we are calculating the linear regression of the given values (asuming x =100).
import statistics x = [19, 75, 97, 82, 31] y = [21, 12, 43, 74, 35] slope, intercept = statistics.linear_regression(x, y) print(round(slope * 100 + intercept))Output
The output is obtained as follows −
49
python_modules.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4