Linear Regression

Soumyadeep Mukherjee
3 min readNov 18, 2020

In the world of machine learning, it is the most easiest and understandable model.

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

The concept used behind this comes from school level mathematics, i.e straight line,y=mx+c.

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

Now let's describe the execution of the model step by step:

Step 1:

importing library ;(you can use any ide for this, I have used Jupyter NoteBook)

importing libraries

These are the pre-existing libraries present in the python package;

The first library called numpy is used to convert data into array, “np” is used a variable.

matplotlib.pylot is used to plot graph in the model

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

Step 2:

Loading of dataset

importing dataset

For importing values from the given csv file we use panda, and here we stored that into dataset variable

Step 3:

Split the test case into dependent and independent variable

splitting dataset

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:

Spliting data into test and training set

We use sklearn library to import train_test_split class from it, which is used to split the data of our model.“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/Importing model

We again use sklearn library which is an open source library for python programming.We import LinearRegression class from sklearn, after creating an object we fit in our data into the model.

Step 6:

Predicting output

We use objectname.predict to predict the output on the set. Objectname.coef_ to predict the coefficient and objectname.intercept_ to predict the intercept.

Step 7:

Plot graph

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.

So that's it our first model on linear Regression is ready.

--

--