linregress(x, y=None, alternative='two-sided')
Missing values are considered pair-wise: if a value is missing in x, the corresponding value in y is masked.
For compatibility with older versions of SciPy, the return value acts like a namedtuple
of length 5, with fields slope
, intercept
, rvalue
, pvalue
and stderr
, so one can continue to write
slope, intercept, r, p, se = linregress(x, y)
With that style, however, the standard error of the intercept is not available. To have access to all the computed values, including the standard error of the intercept, use the return value as an object with attributes, e.g.
result = linregress(x, y)
print(result.intercept, result.intercept_stderr)
Two sets of measurements. Both arrays should have the same length. If only x is given (and y=None
), then it must be a two-dimensional array where one dimension has length 2. The two sets of measurements are then found by splitting the array along the length-2 dimension. In the case where y=None
and x is a 2x2 array, linregress(x)
is equivalent to linregress(x[0], x[1])
.
Defines the alternative hypothesis. Default is 'two-sided'. The following options are available:
The return value is an object with the following attributes:
slope
slope
intercept
intercept
rvalue
rvalue
pvalue
pvalue
stderr
stderr
intercept_stderr
intercept_stderr
Calculate a linear least-squares regression for two sets of measurements.
scipy.optimize.curve_fit
scipy.optimize.leastsq
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
rng = np.random.default_rng()
x = rng.random(10)
y = 1.6*x + rng.random(10)
res = stats.linregress(x, y)
print(f"R-squared: {res.rvalue**2:.6f}")
plt.plot(x, y, 'o', label='original data')
plt.plot(x, res.intercept + res.slope*x, 'r', label='fitted line')
plt.legend()
plt.show()
# Two-sided inverse Students t-distribution
# p - probability, df - degrees of freedom
from scipy.stats import t
tinv = lambda p, df: abs(t.ppf(p/2, df))
ts = tinv(0.05, len(x)-2)
print(f"slope (95%): {res.slope:.6f} +/- {ts*res.stderr:.6f}")
print(f"intercept (95%): {res.intercept:.6f}"
f" +/- {ts*res.intercept_stderr:.6f}")
The following pages refer to to this document either explicitly or contain code examples using this.
scipy.optimize._minpack_py:curve_fit
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them