Linear Regression in Machine Learning
So now we saw all the basic components of the Linear regression
it’s time to take some real life examples & code it in our jupyter notebook.
First of all go to my github page & download this
2 csv files i.e House1 , House_2 .
Save it in a suitable location & open your jupyter
notebook & let’s get started.
press Shit+Enter to execute every step.
Q.1)Home Price in NewYork. Given these Home Prices
find out prices of homes whose Area is 3300sqft.
Step1:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
#For execution -> (Press : Shift + Enter)
Step2:
df=pd.read_csv(r"E:\MACHINE_LEARNING\House1.csv") #give path of where you
saved the file
df.head(3)
output:
Step3:
#plot a graph
plt.xlabel("Area in sqft")
plt.ylabel("Price in USD")
plt.scatter(df.Area,df.Price)
output:
Step4:
from sklearn.linear_model import LinearRegression
reg=LinearRegression()
reg.fit(df[['Area']],df.Price) #df[['Area']]
== because dependent variable show be in 2D array
#.fit means we are training our model using data
points
Step5:
#It is 'x' in y=m*x+c i.e x=3300
x=reg.predict([[3300]])
x
output:
array([815538.27751196])
#Now notice the output. We got the
house price.
To get the output
just like that we can use the formula.
y=m*x+c
m=reg.coef_ #Its the ‘m’ in y=m*x+c.
m
output:
c=reg.intercept_ #Its the ‘c’ in y=m*x+c.
c
output:
#Now to find y which is our dependent variable (y=m*x+c)
y=m*3300+c
y
output:
array([815538.27751196]) #Its is exactly same as reg.predict.
Now compare both the output of x & y you can see
the output values we got is same.
The math we did of y=m*x+b is done in LinearRegression
model that why the predicted value is same as calculated value.
As we predicted the price of 1 house
Now we will take house area of multiple houses &
predict it's price based on our model.
code:
d=pd.read_csv(r"E:\MACHINE_LEARNING\Areas.csv")
d.head()
output:
p=reg.predict(d)
d['Price']=p #help
us to create new column in Area.csv file
d
output:
d.to_csv("E:\MACHINE_LEARNING\Predictions_of_prices.csv",index=False)
use to create new CSV file named :
Predictions_of_prices.csv
Now go & see prediction_of_prices.csv
file all the price will be predicted their
pp=pd.read_csv(r"E:\MACHINE_LEARNING\Predictions_of_prices.csv")
pp.head()
output:
plt.xlabel("Area in sqft")
plt.ylabel("Price in USD")
plt.scatter(df.Area,df.Price)
plt.plot(df.Area,reg.predict(df[['Area']]),color="red")
output:
Go check my jupyter notebook for better understanding.
Hey guys, do you found our content informative? please leave a comment below & don't forget to share it with your friends.
Lucky Club Casino Site » Online Gambling in Malaysia
ReplyDeleteLive casino online gaming with over 5000 casino games from leading providers. Lucky Club Casino is an online luckyclub.live casino for players from Malaysia. Rating: 4.8 · Review by LuckyClub.live