Linear regression in python

Share this post

Linear regression is a statistical method for modeling the linear relationship between a dependent variable and one or more independent variables. In Python, this relationship can be modeled using the 'statsmodels‘ or 'sklearn‘ libraries.

Linear Regression

Here is an example of how to perform linear regression in Python using the statsmodels library:

# Import the required libraries
import numpy as np
import statsmodels.api as sm

# Define the independent and dependent variables
x = np.array([1, 2, 3, 4, 5])
y = np.array([2, 3, 4, 5, 6])

# Add a constant term to the independent variable
x = sm.add_constant(x)

# Create a linear regression model
model = sm.OLS(y, x)

# Fit the model to the data
results = model.fit()

# Print the model summary
print(results.summary())

In this example, the independent variable is ‘x’ and the dependent variable is ‘y’. The ‘statsmodels’ library is used to add a constant term to the independent variable, create a linear regression model, and fit the model to the data. Finally, the model summary is printed, which provides information about the coefficients, p-values, and other statistical measures associated with the model.

Alternatively, you can use the ‘sklearn’ library to perform linear regression in Python. Here is an example of how to do this:

# Import the required libraries
import numpy as np
from sklearn.linear_model import LinearRegression

# Define the independent and dependent variables
x = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y = np.array([2, 3, 4, 5, 6])

# Create a linear regression model
model = LinearRegression()

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

# Print the coefficients and intercept
print('Coefficients:', model.coef_)
print('Intercept:', model.intercept_)

In this example, the 'sklearn' library is used to create a linear regression model and fit it to the data. The coefficients and intercept of the model are then printed.

Both of these examples demonstrate how to perform linear regression in Python using two different libraries. The 'statsmodels‘ library provides a more detailed summary of the model, while the ‘sklearn’ the library is simpler to use and allows you to easily access the coefficients and intercept of the model.


Share this post

Leave a Comment

Your email address will not be published. Required fields are marked *