×

Multiple Linear Regression using Python

Linear Regression:

Linear regression is a method that models the relationship between a dependent variable and one or more independent variables; in other terms, that models the relationship between a target variable and simple regression or multiple regression.

This model assumes a relationship between the given inputs and the output variables. We can also determine the coefficients required by the model to predict the new data.

Linear regression is of two forms; they are

  • Simple Linear Regression
  • Multiple Linear Regression

Simple Linear Regression:

Simple linear regression (SLR) predicts a response using a single feature or variable. In simple linear regression, all the variables are linked linearly. Here the main purpose is to find a linear equation used to predict the answer value of y concerning the feature or the independently derived variable (x).

Below is a dataset with x features and y responses respective to x.

Multiple Linear Regression Using Python

For simple understanding, we define

X as features, that is x = [10, 11, 12, ……, 19],

Y as response, that is y = [11, 13, 12, ……, 22]

We have considered 10 values in the above table

The graphical representation of the above dataset looks like this:

Multiple Linear Regression Using Python

After this, we have to find or identify the most suitable line for this scatter graph to find the response of any new value for a feature.

This line is referred to as the regression line. We have some equations of the regression line.

Multiple Linear Regression Using Python

Here,

  • h(xi) is the predicted response value for the ith.
  • β0  and β1xi are regression coefficients.

If we want to build the model, we must know how to estimate the value of regression coefficients. If we know how to estimate regression coefficients, then only we can use this model to get the responses.

Concept of Least Squares:

Yi = 0 + Ρ1xi + Ρi = h(xi) + Ρi Ρ Ρi = yi – h(xi)

Here, Ρi is a residual error in ith observation.

So, we have to minimize the total residual error.

The cost function or squared error, b as:

b(β0, β1) = 1/2nΣnI = 1 έ2i

We have to find the values of Ρ0 and Ρ1  to find b(Ρ0 and Ρ1) minimum.

Let us not go into deep calculations. We are presenting the result below:

β1 = SSab / SSaa

β0 = b – β1a

where SSab would be the sum of cross deviations of “b” and “a”:

SSab = ΣnI = 1 (ai – a)(bi – b) = ΣnI =1 biai – nab

And SSaa would be the sum of squared deviations of “a.”

SSaa = Σni = 1(ai – a)2 = Σni = 1ai2 – n(a)2

Modules used:

  • Numpy
  • Pandas
  • Matplotlib
  • Sklearn

Example:

#simple program for simple linear regression
import numpy as np  
import matplotlib.pyplot as mtp  
  
def estimate_coeff(p, q):  
# Here, we will assume the total number of points or observation  
    n1 = np.size(p)  
# Now, we will calculate the mean of a and b vector  
    p = np.mean(p)  
    q = np.mean(q)  
  
# here, we will calculate the cross deviation and deviation about a  
    SS_pq = np.sum(q * p) - n1 * q * p  
    SS_pp = np.sum(p * p) - n1 * p * p  
  
# here, we will calculate the regression coefficients  
    b1 = SS_pq / SS_pp  
    b0 = q - b1 * p  
  
    return (b0, b1)  
  
def plot_regression_line(p, q, b):  
# Now, we will plot the actual points or observations as a scatter plot  
    MTP.scatter(p, q, color = "m",  
            marker = "o", s = 30)  
  
\# here, we will calculate the predicted response vector  
    q_pred = b[0] + b[1] * p  
  
# here, we will plot the regression line  
    mtp.plot(p, q_pred, color = "g")  
  
# here, we will put the labels  
    mtp.xlabel('p')  
    mtp.ylabel('q')  
  
# here, we will define the function to show plot  
    mtp.show()  
  
def main():  
# entering the observation points or data  
    p = np.array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])  
    q = np.array([11, 13, 12, 15, 17, 18, 18, 19, 20, 22])  
  
# now, we will estimate the coefficients  
    b = estimate_coeff(p, q)  
    print("Estimated coefficients are :\nb0 = {} \  
        \nb_1 = {}".format(b[0], b[1]))  
  
# Now, we will plot the regression line  
    plot_regression_line(p, q, b)  
  
if __name__ == "__main__":  
    main()  

Output:96

Estimated coefficients are:
b0 = -0.5606006069
b1 = 1.17696867686
Multiple Linear Regression Using Python

As we can see from the output, we have drawn simple linear regression using Python and the modules matplotlib used to plot the graph and the numpy module.

Multiple Linear Regression:

We require only one independent variable as the input in simple linear regression. However, we provide multiple independent variables for a single dependent variable in multiple linear regression. It is a Machine learning algorithm.

Q(Feature Matrix) = Q is a matrix of size "a * b" where "Qij" represents the values of the jth attribute for the ith observation.

We represent in the matrix form.

Multiple Linear Regression Using Python

S (Response Vector) = It is a vector of size “a” representing the response value for the ith observation.

Multiple Linear Regression Using Python

“A” regression line is:

  h(Qi) = β0 + β1qi1 + β2qi2 + β3qi3 + β4qi4 + β5qi5 +………. + βbqib

Here, h(qi) is known as the response value for the ith observation point. We have to find the regression coefficients that are Ρ0, Ρ1, Ρ2……, Ρb .

We have another formula:

Si = β0 + β1qi1 + β2qi23qi3 + ……..+βbqibi

We can represent the linear model in matrix form

Here,

Multiple Linear Regression Using Python

And,

Multiple Linear Regression Using Python

Using the Least Squares algorithm, we can get the estimated value of b (b). We can only use the Least Squares method when the residual error is minimised.

The result will be shown as

Multiple Linear Regression Using Python

Here, (‘) represents the transpose of the matrix and -1 represents the reverse of a matrix.

Multiple Linear Regression Using Python

The above formula is used for calculating the multi-linear regression model. Here Y’ is the calculated response vector.

Example:

//program for multiple linear regression using Python
import matplotlib.pyplot as mtp 
import numpy as np  
from sklearn import datasets as data
from sklearn import linear_model as lmd  
from sklearn import metrics as mt  
  
# First, we will load the Boston dataset  
boston1 = data.load_boston(return_X_y = False)  
  
# Here, we will define the feature matrix(H) and response vector(f)  
H = boston1.data  
f = boston1.target  
  
# Now, we will split X and y datasets into training and testing sets  
from sklearn.model_selection import train_test_split as tts  
H_train, H_test, f_train, f_test = tts(H, f, test_size = 0.4,  
                                                    random_state = 1)  
  
# Here, we will create a linear regression object  
reg1 = lmd.LinearRegression()  
  
# Now, we will train the model by using the training sets  
reg1.fit(H_train, f_train)  
  
# here, we will print the regression coefficients  
print('The Regression Coefficients are: ', reg1.coef_)  
  
# Here, we will print the variance score: 1 means perfect prediction  
print('The Variance score is: {}'.format(reg1.score(H_test, f_test)))  
  
# Here, we will plot for residual error  
  
# here, we will set the plot style  
mtp. style.use('five thirty eight')  
  
# here, we will plot the residual errors in training data  
mtp.scatter(reg1.predict(H_train), reg1.predict(H_train) - f_train,  
            color = "green", s = 10, label = 'Train data')  
  
# Here, we will plot the residual errors in test data  
mtp.scatter(reg1.predict(H_test), reg1.predict(H_test) - f_test,  
            color = "blue", s = 10, label = 'The Test data')  
  
# Here, we will plot the line for zero residual error  
mtp. hlines(y = 0, xmin = 0, Xmax = 50, linewidth = 2)  
  
# here, we will plot the legend  
mtp.legend(loc = 'upper right')  
  
# now, we will plot the title  
mtp.title("The Residual errors")  
  
# here, we will define the method call for showing the plot  
mtp.show()  

Output:

The Regression Coefficients are:  [   -8.95714048e-02  6.73132853e-02  5.04649248e-02  2.18579583e+00       -1.72053975e+01  3.63606995e+00  2.05579939e-03 -1.36602888          2.89576718e-01      -1.22700072e-02         -8.34881849e-01  9.40360790e-03      -5.04008320e-01    ]


The Variance score is: 0.7209056672661751
Multiple Linear Regression Using Python

As we can see the output of the program how we have drawn the multiple linear regressions using Python. In the program, we have imported the modules matplotlib, numpy, and sklearn. Matplotlib is used to plot the graph and represent the graph on the output screen. Sklearn for dataset usage and numpy for the larger arrays of the dataset.


Related Topics

Write Dictionary to CSV in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

Reverse a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Standard GUI Unit Converter using PyQt5 in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

6 minutes read.

Python Operators

Operators in Python programming An operator is a symbol that operates on one or more operands. An operand is a value or a variable on which we perform the operation....

5 minutes read.

Python globals() function

Python globals() function The globals() function in Python returns the value of the named attribute of object where the ‘name’ must be a string. Syntax globals() Parameter NA Return This function returns the global symbol table as a dictionary. Example...

1 minute read.

How to plot a Histogram in Python

A Histogram is used to represent a given data provided as a chunk. This histogram is a graphical representation that uses bars to indicate the ranges of the data. In...

4 minutes read.

File Explorer using Tkinter in Python

File Explorer: Users can control folders and files on a device using an application known as a file manager or file explorer. Customers can access, edit, copy, delete, and start moving files...

3 minutes read.

How to check version of Python

How to check version of python The versions of Python come with different kinds of features and functionalities. It is not a herculean task to keep track of the updates these...

3 minutes read.

Find key from value in dictionary python

Python: Python programming language is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a...

5 minutes read.

Python Dictionary

Dictionary in Python is an unordered collection of data values, which is used to store data values like a map. Unlike different data types that hold just individual assets as...

4 minutes read.

Python Indentation

Overview The spaces at the start of a code line are known as an indentation in Python programming. Indentation is only used for readability in other programming languages like C, C++,...

8 minutes read.

Subprocess Module in Python

What is a Process? Every program will have its respective system state, i.e., the state in which it is running. A program that is in a running state or in a...

7 minutes read.

Python NewLine

In python, a new line character denoted by "\n" is known as an escape character or escape sequence, and it will force the cursor to change its position to the next...

6 minutes read.

Python Program for Linear Search

Introduction We employ specific algorithms to efficiently carry out our responsibilities, such as searching for an element in a given data structure. These algorithms fall under the category of searching algorithms....

4 minutes read.

How to Download all Modules in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Program of Cumulative Sum in Python

Introduction This tutorial, explains what is meant by lists, the cumulative total, several approaches to calculating the cumulative sum of digits in a list, etc. Learn the straightforward Python codes for...

5 minutes read.

Number pattern in Python

Number pattern in Python: This article explains how to print number patterns in Python. The FOR loop, while loop, and range() functions are used in the following Python programs to...

4 minutes read.

Python String isupper() method

Python String isupper() method The String.isupper() method in Python returns a boolean value ‘true’ if all cased characters in the string are uppercase else for any other value is returns false. Syntax String.isupper() Parameter NA Return This...

2 minutes read.

Installing Packages in Python

It's critical to understand that the term "package" here refers to a collection of software that must be installed (i.e. as a synonym for a distribution). It has nothing to...

6 minutes read.

Python Ways to find nth occurrence of substring in a string

Introduction In Python, a string is a collection of characters that can be employed to conduct additional operations. In Python, a substring is a group of characters that are a part...

4 minutes read.