How to print intercept and slope of a simple linear regression in Python with scikit-learn?

Written by- Aionlinecourse1714 times views

To print the intercept and slope of a simple linear regression model in Python using scikit-learn, you can use the intercept_ and coef_ attributes of the LinearRegression object. Here's an example:
from sklearn.linear_model import LinearRegression

# fit a linear regression model
X = [[1], [2], [3], [4]]
y = [1, 2, 3, 4]
model = LinearRegression().fit(X, y)

# print the intercept and slope
print("Intercept:", model.intercept_)
print("Slope:", model.coef_)
This will print the intercept and slope of the fitted linear regression model.

Alternatively, you can also use the get_params method to print the intercept and slope, as well as other parameters of the model:

print(model.get_params())

This will print a dictionary containing the parameters of the model, including the intercept and slope.