6 Methods To Conduct Linear Regression In Python

Mandar Karhade, MD. PhD.
Geek Culture
Published in
5 min readMar 26, 2023

--

Generalized Linear Models (GLM) is the basis of the majority of regression-based models

Linear regression is a statistical method for modeling the relationship between a dependent variable and one or more independent variables. In Python, there are several methods to conduct linear regression. Here are some of the most commonly used methods:

Scikit-learn

Scikit-learn is a popular machine-learning library in Python that provides a simple and efficient way to conduct linear regression. Here’s an example of how to use it:

from sklearn.linear_model import LinearRegression
import numpy as np

# Create some data
X = np.array([[10, 2], [20, 3], [3, 4], [4, 50]])
y = np.array([3, 5, 7, 9])

# Create a linear regression object
model = LinearRegression()

# Fit the model to the data
model.fit(X, y)

# Predict the target variable
y_pred = model.predict(X)

Statsmodels

Statsmodels is another popular library in Python for statistical modeling. Here’s an example of how to use it for linear regression:

import statsmodels.api as sm
import numpy as np

# Create some data
X = np.array([[10, 2], [20, 3], [3, 4], [4, 50]])
y = np.array([3, 5, 7, 9])

# Add a constant to the independent variables
X =…

--

--