Fit a straight-line model
Estimate the relationship between paired numeric observations.
SciPy · Function
Calculates a linear least-squares regression for two measurement sets. The result provides line coefficients, correlation, slope-test significance, and standard-error information.
The function fits the least-squares line relating two equal-length measurement arrays. It returns a result object containing the line coefficients, Pearson correlation, a slope test p-value, and uncertainty estimates; the alternative argument selects the slope hypothesis direction.
scipy.stats.linregress(*x*, *y=None*, *alternative='two-sided'*)| Name | Type | Description |
|---|---|---|
x, y | array_like | Two measurement arrays. They should have the same length; when y is omitted, x must be two-dimensional with one dimension of length 2. |
alternative | {'two-sided', 'less', 'greater'}, optional | Defines the alternative hypothesis: 'two-sided', 'less', or 'greater'. |
LinregressResult instance with slope, intercept, rvalue, pvalue, stderr, and, in the documented result attributes, intercept_stderr.
import numpy as np
from scipy.stats import linregress
x = np.array([0, 1, 2, 3])
y = np.array([1, 3, 5, 7])
result = linregress(x, y)
print(result.slope, result.intercept, result.rvalue**2)
Fits a line to four observations and prints its slope, intercept, and coefficient of determination.
Estimate the relationship between paired numeric observations.
Assess whether the fitted slope differs from zero or has a specified direction.
Summarize linear association and explained variation.
linregress fits a linear least-squares regression for two measurement sets. Its result includes the slope, intercept, Pearson correlation, p-value for testing a zero slope, and standard errors.
Follow-up: What does rvalue**2 represent? It is the coefficient of determination.
alternative controls the slope hypothesis: two-sided tests whether it is nonzero, less tests whether it is below zero, and greater tests whether it is above zero. The default is two-sided.
Follow-up: When was alternative added? The documentation marks it as added in SciPy 1.7.0.
x and y normally represent two equal-length measurement arrays. If y is omitted, x must be two-dimensional with one dimension of length two, and the measurements are split along that dimension.
Follow-up: What input-shape constraint still applies? The two measurement sets must have the same length.
scipy.stats.linregressLinregressResult