Linear Regression in Python (scipy)

Context: FIT1043_MOC · the applied form of linear regression · fit a line with scipy · lab: 30_Projects/FIT1043_Labs/Week5-Regression-Solution.pdf Problem it solves: fit to two numeric columns, read the slope/intercept/, and plot the line.

Quick Revision

  • 🎯 Trigger: a linear trend between two numeric variables ➔ linregress(x, y) returns slope, intercept, r, p, std_err.
  • ⚡ Key Constraint: linregress unpacks five values in order; build predictions with a comprehension slope*xi + intercept.

🔧 Minimal Working Example

from scipy.stats import linregress
import matplotlib.pyplot as plt
 
slope, intercept, r_value, p_value, std_err = linregress(df['Age'], df['Income'])
line = [slope*xi + intercept for xi in df['Age']]     # predicted y for each x
 
plt.scatter(df['Age'], df['Income'])
plt.plot(df['Age'], line, 'r-')
plt.show()

Expected output: the fitted line over the scatter; slope/intercept define it, r_value its correlation strength.

  • Fitslope, intercept, r_value, p_value, std_err = linregress(x, y) (x = independent, y = dependent).
  • Predict / drawline = [slope*xi + intercept for xi in x], then plt.plot(x, line).
  • Read r_value is Pearson correlation (); its sign matches the slope’s (e.g. Age↑ vs Runs↓ gives negative slope and ).

🔀 Variations

  • Report the fitprint('slope %f intercept %f' % (slope, intercept)); print('r %f' % r_value).
  • Predict one pointslope*70 + intercept — but only trust it where the relationship is actually linear.

✍️ Practice

⚠️ Common Mistakes

  • 💡 Don’t extrapolate past the linear range ➔ a model fit on ages 18–40 can’t predict income at 70 if the true relation bends — linear regression assumes linearity.
  • 💡 Order of returns matterslinregress gives (slope, intercept, r, p, std_err); mis-unpacking silently mislabels them.