VOOZH about

URL: https://www.geeksforgeeks.org/machine-learning/python-linear-regression-using-sklearn/

⇱ Python | Linear Regression using sklearn - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python | Linear Regression using sklearn

Last Updated : 11 Jul, 2025

Linear Regression is a machine learning algorithm based on supervised learning. It performs a regression task. Regression models a target prediction value based on independent variables. It is mostly used for finding out the relationship between variables and forecasting. Different regression models differ based on – the kind of relationship between the dependent and independent variables, they are considering and the number of independent variables being used. This article is going to demonstrate how to use the various Python libraries to implement linear regression on a given dataset. We will demonstrate a binary linear model as this will be easier to visualize. In this demonstration, the model will use Gradient Descent to learn. You can learn about it here.

Step 1: Importing all the required libraries 

Step 2: Reading the dataset:

Output:

👁 Image

Step 3: Exploring the data scatter 

Output:

👁 Image

Step 4: Data cleaning 

Step 5: Training our model 

Output:

👁 Image

Step 6: Exploring our results 

Output:

👁 Image

The low accuracy score of our model suggests that our regressive model has not fit very well with the existing data. This suggests that our data is not suitable for linear regression. But sometimes, a dataset may accept a linear regressor if we consider only a part of it. Let us check for that possibility.   

Step 7: Working with a smaller dataset 

Output:

👁 Image

We can already see that the first 500 rows follow a linear model. Continuing with the same steps as before. 

Output:

👁 Image

Output:

👁 Image

Step 8: Evaluation Metrics For Regression

At last, we check the performance of the Linear Regression model with help of evaluation metrics. For Regression algorithms we widely use mean_absolute_error, and mean_squared_error metrics to check the model performance. 

Output:

MAE: 0.7927322046360309
MSE: 1.0251137190180517
RMSE: 1.0124789968281078
Comment
Article Tags: