Multivariable
Linear Regression Example
As we had see an example where we took single
independent variable in our previous blog. Now lets take an example where we
will take multiple independent variable & use your model to train &
test them. Don’t worry I’ll not include math just practical example only.
Q.1) Let’s say you want an house in Mumbai but this
time based on multiple factors you will buy the house such as House Area,Number
of Bedrooms & Age of the house (How old that apartment is).
Key point : More Age of House Less the Price is &
if the number of bedroom is high then the price should also be high.
Steps :
First of all go to my github & download the House_2.csv
file.
Save it in a suitable location & then open you jupyter
notebook & let’s start to code
Remember after every step press shift+enter to
execute that code.
Step1:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
Step2:
df=pd.read_csv(r"E:\MACHINE_LEARNING\House_2.csv")
df
output:
Step3:
#if you had noticed above we have NaN value which
means that cell is empty
#Now let's see how to handle NaN values
#Let's insert the median value into the empty cells
med=df['Bedroom'].median()
med
Output:
3.5
Step4:
#As you all know the number of the bedroom cant be 3.5
so we have to conver it into an integer
import math
bed=math.floor(med)
bed
Output:
3
Step5:
#Now we are filling all the NaN values with the median
of that column
df.Bedroom=df.Bedroom.fillna(bed) #USe
the fill NaN value permanently
df
Output:
Step6:
#Now formula : y=m1*x1+m2*x2+m3*x3+c
#m1,m2,m3 = Co-efficient
#Area,Bedroom,Age = Independent Variable(x) [Features]
#c
= Intercept
x=df[['Area','Bedroom','Age']]
y=df['Price']
from sklearn.linear_model import LinearRegression
reg=LinearRegression()
reg.fit(x,y) #.fit is used to train
our Linear regression model
Output:
LinearRegression(copy_X=True, fit_intercept=True,
n_jobs=None, normalize=False)
Step7:
reg.coef_ #
values of m1,m2,m3
Output:
array([ 1.66769396e+02, 6.57543763e+05, -1.04049744e+05])
Step8:
reg.intercept_ # values of c
Output:
1769010.7017902858
Step9:
reg.predict([[3450,3,5]]) #Area,Bedroom,Age output is Price
Output:
array([3796747.68660065])
#Same this is done above this is just for your
understanding
((1.66769396e+02)*(3450))+((6.57543763e+05)*3)+((-1.04049744e+05)*5)+
1769010.7017902858
Output:
3796747.686990286
Here is the link of my Jupyter notebook go & check
it out for you better understanding.
https://github.com/Vegadhardik7/ALL_CSV/blob/master/Multivariable_Linear_Regression_House_2.ipynb
Hey guys, do you found our content informative? please leave a comment below & don't forget to share it with your friends.
Hey guys, do you found our content informative? please leave a comment below & don't forget to share it with your friends.
No comments:
No Spamming and No Offensive Language