How to build a simple Linear Regression Model? (2024)

This post is about implementing simple linear regression model for ML beginners in step by step way with detailed explanation.

If you are new to machine learning, check this post for getting a clear idea about Machine Learning and it’s basics.

What is the logic behind simple linear regression model?

As the name suggests, linear regression follows the linear mathematical model for determining the value of one dependent variable from value of one given independent variable. Remember the linear equation from school?

where y is the dependent variable, m is slope, x is the independent variable and c is the intercept for a given line.

We also have multiple regression model where multiple independent variables are used to calculate one dependent variable.

I have used Jupyter Notebook for implementation. Any Python IDE can be used of your choice. So let’s get rolling..

Step 1: Importing libraries

How to build a simple Linear Regression Model? (3)

There are already developed libraries in Python for implementation of Machine Learning models.

First library called matplotlib is used to plot the graph in last step. ”plt” is used as variable name for using this library in code ahead.

sklearn is official machine learning library in python for various model implementation.

numpy is used to convert data into arrays for actual use by sklearn library.

pandas is used to access .csv file of our dataset.

Step 2: Loading dataset

How to build a simple Linear Regression Model? (4)

Our dataset is in a .csv file type. Pandas variable pd is used to access the dataset with read_csv() function.

Step 3: Split to independent and dependent variables

How to build a simple Linear Regression Model? (5)

We define x as the independent variable in dataset by iloc(index location) value. [] is used to define array elements. “:” inside [] indicates consider all rows in dataset and separating by using “,” we specify the number of column which we want to use as independent or dependent variable values starting the count from zero in dataset.

Step 4: Splitting data into training and testing data

How to build a simple Linear Regression Model? (6)

Now, entire dataset is divided into training and testing set so that prediction does not overfit or underfit and correct values are obtained. train_test_split() is inbuilt function from scikit learn for splitting x and y variables data. “test_size” parameter is used to divide (1/3)rd of entire dataset(30%) into test data and remaining as training data.Setting random_state as null would not allow random values to be taken from dataset.

Step 5: Choosing the Model

How to build a simple Linear Regression Model? (7)

We reshape our independent variable as sklearn expects a 2D array as input.

Linear Regression is our model here with variable name of our model as “lin_reg”. We can try the same dataset with many other models as well. This part varies for any model otherwise all other steps are similar as described here.

Step 6: Fit our model

How to build a simple Linear Regression Model? (8)

We now fit our model to the linear regression model by training the model with our independent variable and dependent variables.

Step 7: Predict the output

How to build a simple Linear Regression Model? (9)

Finally our model predicts the dependent variable “lin_reg_pred” using the test values of independent variable.

We can see the coefficient,intercept values for our outlier and also the mean squared error and variance for the predicted values(lin_reg_pred) and actual test value of dependent variable(y_test). Inbuilt methods does the math with the predefined formulae for each value.

Step 8: Plot the graph

How to build a simple Linear Regression Model? (10)

We ultimately want to visualize the actual data values and predicted data values in a graphical format. “plt”, matplotlib variable, is used to plot points using “scatter()” and outlier using “plot()” functions.

Output might vary depending on various system features. Output I got is as follows:

How to build a simple Linear Regression Model? (11)
How to build a simple Linear Regression Model? (12)

This is it! Our first linear regression model is implemented! Below is the link to the entire code for the entire model that we implemented here.

Follow me on LinkedIn: www.linkedin.com/comm/mynetwork/discovery-see-all?usecase=PEOPLE_FOLLOWS&followMember=ditimodi

How to build a simple Linear Regression Model? (2024)
Top Articles
Latest Posts
Article information

Author: Tish Haag

Last Updated:

Views: 6189

Rating: 4.7 / 5 (67 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Tish Haag

Birthday: 1999-11-18

Address: 30256 Tara Expressway, Kutchburgh, VT 92892-0078

Phone: +4215847628708

Job: Internal Consulting Engineer

Hobby: Roller skating, Roller skating, Kayaking, Flying, Graffiti, Ghost hunting, scrapbook

Introduction: My name is Tish Haag, I am a excited, delightful, curious, beautiful, agreeable, enchanting, fancy person who loves writing and wants to share my knowledge and understanding with you.