Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

stats.linregress

Calculates a linear least-squares regression for two measurement sets. The result provides line coefficients, correlation, slope-test significance, and standard-error information.

Since
1.7.0

How it works

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.

Syntax

scipy.stats.linregress(*x*, *y=None*, *alternative='two-sided'*)

Parameters

NameTypeDescription
x, yarray_likeTwo 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'}, optionalDefines the alternative hypothesis: 'two-sided', 'less', or 'greater'.

Returns

LinregressResult instance with slope, intercept, rvalue, pvalue, stderr, and, in the documented result attributes, intercept_stderr.

Key points

  • Use linregress to estimate a straight-line relationship between two measurement sets.
  • Square rvalue to obtain the coefficient of determination for the fitted line.
  • Unless changed, the slope hypothesis test uses a two-sided alternative.
  • Choose alternative='less' when the hypothesis concerns a negative slope.
  • Choose alternative='greater' when the hypothesis concerns a positive slope.
  • stderr quantifies uncertainty in the estimated slope, subject to the documented assumptions.

Common mistakes

  • Pass x and y with matching lengths; otherwise the regression input is invalid.
  • Interpret pvalue as a Wald test based on a t distribution, not as a measure of prediction error.
  • The reported standard errors rely on normally distributed residuals.

Example: Fit and inspect a line

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.

When to use it

Fit a straight-line model

Estimate the relationship between paired numeric observations.

Test a slope hypothesis

Assess whether the fitted slope differs from zero or has a specified direction.

Measure correlation and fit quality

Summarize linear association and explained variation.

Interview questions

QWhat does scipy.stats.linregress return?

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.

QHow does alternative affect the p-value?

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.

QWhat input forms does linregress accept?

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.linregress
  • LinregressResult

Source: scipy.stats.linregress. Checked 2026-10-15.