• 2nd Floor, #7a, High School Road, Secretariat Colony Ambattur, Chennai-600053 (Landmark: SRM School) Tamil Nadu, India
• pro@slogix.in
• +91- 81240 01111

### How to implement multiple linear regression using sklearn library in python?

###### Description

To implement multiple linear regression using sklearn library in python.

###### Process

Import necessary libraries.

Import LinearRegression()from sklearn.

Plot scatter diagram to check linearity.

Assign dependent variable(Y).

Build the regression model.

Fit X and Y.

###### Sample Code

#import libraries

from sklearn import linear_model

import pandas as pd

import matplotlib.pyplot as plt

/Sathish/Pythonfiles/Employee.csv’)

#creating data frame

df=pd.DataFrame(data)

print(df)

#plotting the scatter diagram for independent variable 1

plt.scatter(df[‘rating’], df[‘salary’], color=’red’)

plt.title(‘rating vs salary’, fontsize=14)

plt.xlabel(‘rating’, fontsize=14)

plt.ylabel(‘salary’, fontsize=14)

plt.grid(True)

plt.show()

#plotting the scatter diagram for independent variable 2

plt.scatter(df[‘bonus’], df[‘salary’], color=’green’)

plt.title(‘bonus vs salary’, fontsize=14)

plt.xlabel(‘bonus’, fontsize=14)

plt.ylabel(‘salary’, fontsize=14)

plt.grid(True)

plt.show()

#assigning the independent variable

X = df[[‘rating’,’bonus’]]

#assigning the dependent variable

Y = df[‘salary’]

#Build multiple linear regression

regr = linear_model.LinearRegression()

#fit the variables in to the linear model

regr.fit(X, Y)

#print the intercept and regression co-efficient

print(‘Intercept: \n’, regr.intercept_)

print(‘Coefficients: \n’, regr.coef_)