Data Science Linear Regression

Linear regression fits the single straight line that best predicts a numeric outcome from one numeric input, and scipy.stats.linregress computes that line's exact slope and intercept straight from your data.

What Linear Regression Does

Linear regression looks for the straight line y = slope * x + intercept that sits as close as possible to every point in your data, where 'close' means minimizing the sum of squared vertical distances between the line and each point - a technique called least squares.

Computing the Best-Fit Line

scipy.stats.linregress(x, y) runs that least-squares calculation for you and returns five values: the slope, the intercept, the correlation coefficient r, a p-value, and a standard error.

Example

import numpy as np
from scipy import stats

hours = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
scores = np.array([50, 55, 65, 70, 63, 72, 80, 84, 90, 95])

slope, intercept, r, p, std_err = stats.linregress(hours, scores)

print("slope:", slope)
print("intercept:", intercept)

For this dataset the call returns a slope of about 4.75 and an intercept of about 46.27. In plain terms: the model expects a score of roughly 46.27 for zero hours studied, climbing about 4.75 points for every extra hour studied.

Making Predictions

Example

def predict(hours_studied):
    return slope * hours_studied + intercept

print(predict(11))    # about 98.53
print(predict(20))    # about 141.3 - well outside the original data
Note: predict(20) returns a number above what's realistically possible for many bounded scores, like a percentage capped at 100. A line fitted on hours 1 through 10 has no idea the real world caps out - extrapolating far past your data range is one of the easiest ways to get a confidently wrong answer.

How Good Is the Fit? The r-value

The r value returned by linregress measures how tightly the points hug that line, ranging from -1 to 1. Squaring it (r**2) gives the fraction of the variation in y the line explains - the closer to 1, the better the line summarizes the data.

r valueStrength of relationship
close to 1Strong positive relationship
close to -1Strong negative relationship
close to 0Weak or no linear relationship