You are on page 1of 1

Multiple Regression

Multiple regression is a linear regression but it uses more than one independent variables.

Example: given Excel sheet has two independent variables (Study and Questions) and one dependent
variable (Marks) for 50 students.
Study: Number of hours the student studied
Questions: Number of past paper questions answered by the student
Marks: Marks got by the student at the examination

Example1- plot each independent variable and dependent variable to understand the relationship
import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
Result=pd.read_excel('D:/Ruwan/Courses/Python/Handouts/DataAnalysis/Multiple
Regression/StudyResult.xlsx')
fig, (study,questions) = plt.subplots(nrows=1, ncols=2)
study.scatter(Result['Study'],Result['Marks'])
study.set_title('Relationship between Study hours and Marks')
study.set_xlabel('Study Hours')
study.set_ylabel('Marks')
questions.scatter(Result['Questions'],Result['Marks'])
questions.set_title('Relationship between Answered questions and Marks')
questions.set_xlabel('Number of questions')
questions.set_ylabel('Marks')
plt.show()

Example2-Train the model and predict the values


import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
from sklearn import linear_model
Result=pd.read_excel('D:/Ruwan/Courses/Python/Handouts/DataAnalysis/Multiple
Regression/StudyResult.xlsx')
x=Result[['Study','Questions']].to_numpy()#join independent variables to x and convert to numpy array
y=Result['Marks'].to_numpy()#take dependent variable as y
regr = linear_model.LinearRegression()#create the regression object
regr.fit(x, y)#fill data to the regression object
predictedMarks=regr.predict([[81,30]])
print(predictedMarks)

You might also like