How to do linear regression in Python

1 min

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.