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

Written by - Aionlinecourse1980 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.

Recommended Projects

Deep Learning Interview Guide

Topic modeling using K-means clustering to group customer reviews

Have you ever thought about the ways one can analyze a review to extract all the misleading or useful information?...

Natural Language Processing
Deep Learning Interview Guide

Medical Image Segmentation With UNET

Have you ever thought about how doctors are so precise in diagnosing any conditions based on medical images? Quite simply,...

Computer Vision
Deep Learning Interview Guide

Build A Book Recommender System With TF-IDF And Clustering(Python)

Have you ever thought about the reasons behind the segregation and recommendation of books with similarities? This project is aimed...

Machine LearningDeep LearningNatural Language Processing
Deep Learning Interview Guide

Automatic Eye Cataract Detection Using YOLOv8

Cataracts are a leading cause of vision impairment worldwide, affecting millions of people every year. Early detection and timely intervention...

Computer Vision
Deep Learning Interview Guide

Crop Disease Detection Using YOLOv8

In this project, we are utilizing AI for a noble objective, which is crop disease detection. Well, you're here if...

Computer Vision
Deep Learning Interview Guide

Vegetable classification with Parallel CNN model

The Vegetable Classification project shows how CNNs can sort vegetables efficiently. As industries like agriculture and food retail grow, automating...

Machine LearningDeep Learning
Deep Learning Interview Guide

Banana Leaf Disease Detection using Vision Transformer model

Banana cultivation is a significant agricultural activity in many tropical and subtropical regions, providing a vital source of income and...

Deep LearningComputer Vision