How to do linear regression in Python

1 min readMachine LearningDataAIData Science
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

There are multiple ways to perform linear regression in Python, here are a few examples:

  1. Using scipy.stats.linregress(): This function calculates a linear least-squares regression for two sets of measurements.
from scipy.stats import linregress
slope, intercept, r_value, p_value, std_err = linregress(x,y)
  1. Using numpy.polyfit(): This function returns the coefficients of a polynomial of a specified order that is the best fit for the data using a least-squares approach. To perform linear regression, set the order to 1.
import numpy as np
coefficients = np.polyfit(x, y, 1)
  1. Using scikit-learn: This library provides a LinearRegression class in the linear_model module for performing linear regression.
from sklearn.linear_model import LinearRegression
reg = LinearRegression().fit(X, y)
  1. Using statsmodels: This library provides a OLS class in the stats module for performing ordinary least squares linear regression.
import statsmodels.api as sm
model = sm.OLS(y, X).fit()

In all the above examples, x and y should be numpy arrays or pandas dataframe or series containing the independent and dependent variables respectively. Linear regression can be used to model the relationship between a scalar dependent variable y and one or more explanatory variables denoted X.

You can also use the coefficients obtained to predict the y values for a given x values by using the equation y = slope*x + intercept.

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.