Sesi 4-2B Linear Regression With Python - Jupyter Notebook
Sesi 4-2B Linear Regression With Python - Jupyter Notebook
Linear Regression
Your neighbor is a real estate agent and wants some help predicting housing prices for regions in the USA. It would be great if you could
somehow create a model for her that allows her to put in a few features of a house and returns back an estimate of what the house would
sell for.
She has asked you if you could help her out with your new data science skills. You say yes, and decide that Linear Regression might be a
good path to solve this problem!
Your neighbor then gives you some information about a bunch of houses in regions of the United States,it is all in the data set:
USA_Housing.csv.
The data contains the following columns:
'Avg. Area Income': Avg. Income of residents of the city house is located in.
'Avg. Area House Age': Avg Age of Houses in same city
'Avg. Area Number of Rooms': Avg Number of Rooms for Houses in same city
'Avg. Area Number of Bedrooms': Avg Number of Bedrooms for Houses in same city
'Area Population': Population of city house is located in
'Price': Price that the house sold at
'Address': Address for the house
Let's get started!
import numpy as np
%matplotlib inline
In [3]: df.head()
Out[3]: Avg. Area Avg. Area Avg. Area Number of Avg. Area Number of Area Price Address
Income House Age Rooms Bedrooms Population
0 79545.458574 5.682861 7.009188 4.09 23086.800503 1.059034e+06 208 Michael Ferry Apt.
674\nLaurabury, NE 3701...
1 79248.642455 6.002900 6.730821 3.09 40173.072174 1.505891e+06 188 Johnson Views Suite
079\nLake Kathleen, CA...
9127 Elizabeth
2 61287.067179 5.865890 8.512727 5.13 36882.159400 1.058988e+06 Stravenue\nDanieltown, WI
06482...
3 63345.240046 7.188236 5.586729 3.26 34310.242831 1.260617e+06 USS Barnett\nFPO AP 44820
4 59982.197226 5.040555 7.839388 4.23 26354.109472 6.309435e+05 USNS Raymond\nFPO AE
09386
In [4]: df.info()
localhost:8888/nbconvert/html/Documents/Pelatihan/ABCD/My Works/Day 4 Machine Learning/Linear Regression/Sesi 4-2B Linear Regression with Python.ipynb?download=false 2/12
11/29/22, 8:46 PM Sesi 4-2B Linear Regression with Python
<class 'pandas.core.frame.DataFrame'>
In [5]: df.describe()
Out[5]: Avg. Area Income Avg. Area House Age Avg. Area Number of Rooms Avg. Area Number of Bedrooms Area Population Price
count 5000.000000 5000.000000 5000.000000 5000.000000 5000.000000 5.000000e+03
mean 68583.108984 5.977222 6.987792 3.981330 36163.516039 1.232073e+06
std 10657.991214 0.991456 1.005833 1.234137 9925.650114 3.531176e+05
min 17796.631190 2.644304 3.236194 2.000000 172.610686 1.593866e+04
25% 61480.562388 5.322283 6.299250 3.140000 29403.928702 9.975771e+05
50% 68804.286404 5.970429 7.002902 4.050000 36199.406689 1.232669e+06
75% 75783.338666 6.650808 7.665871 4.490000 42861.290769 1.471210e+06
max 107701.748378 9.519088 10.759588 6.500000 69621.713378 2.469066e+06
In [6]: df.columns
Index(['Avg. Area Income', 'Avg. Area House Age', 'Avg. Area Number of Rooms',
Out[6]:
'Avg. Area Number of Bedrooms', 'Area Population', 'Price', 'Address'],
dtype='object')
EDA
localhost:8888/nbconvert/html/Documents/Pelatihan/ABCD/My Works/Day 4 Machine Learning/Linear Regression/Sesi 4-2B Linear Regression with Python.ipynb?download=false 3/12
11/29/22, 8:46 PM Sesi 4-2B Linear Regression with Python
<seaborn.axisgrid.PairGrid at 0x7fdfb2051070>
Out[7]:
localhost:8888/nbconvert/html/Documents/Pelatihan/ABCD/My Works/Day 4 Machine Learning/Linear Regression/Sesi 4-2B Linear Regression with Python.ipynb?download=false 4/12
11/29/22, 8:46 PM Sesi 4-2B Linear Regression with Python
localhost:8888/nbconvert/html/Documents/Pelatihan/ABCD/My Works/Day 4 Machine Learning/Linear Regression/Sesi 4-2B Linear Regression with Python.ipynb?download=false 5/12
11/29/22, 8:46 PM Sesi 4-2B Linear Regression with Python
In [8]: sns.displot(df['Price'])
<seaborn.axisgrid.FacetGrid at 0x7fdfa2da7580>
Out[8]:
In [9]: # df.corr
sns.heatmap(df.corr())
# sns.heatmap(df.corr(),annot=True)
localhost:8888/nbconvert/html/Documents/Pelatihan/ABCD/My Works/Day 4 Machine Learning/Linear Regression/Sesi 4-2B Linear Regression with Python.ipynb?download=false 6/12
11/29/22, 8:46 PM Sesi 4-2B Linear Regression with Python
<AxesSubplot:>
Out[9]:
X = df[['Avg. Area Income', 'Avg. Area House Age', 'Avg. Area Number of Rooms',
y = df['Price']
localhost:8888/nbconvert/html/Documents/Pelatihan/ABCD/My Works/Day 4 Machine Learning/Linear Regression/Sesi 4-2B Linear Regression with Python.ipynb?download=false 7/12
Train Test Split
11/29/22, 8:46 PM Sesi 4-2B Linear Regression with Python
Now let's split the data into a training set and a testing set. We will train out model on the training set and then use the test set to evaluate
the model.
In [11]: from sklearn.model_selection import train_test_split
In [14]: lm = LinearRegression()
In [15]: lm.fit(X_train,y_train)
LinearRegression()
Out[15]:
Model Evaluation
Let's evaluate the model by checking out it's coefficients and how we can interpret them.
In [16]: # print the intercept
print(lm.intercept_)
-2640159.796852677
coeff_df
localhost:8888/nbconvert/html/Documents/Pelatihan/ABCD/My Works/Day 4 Machine Learning/Linear Regression/Sesi 4-2B Linear Regression with Python.ipynb?download=false 8/12
11/29/22, 8:46 PM Sesi 4-2B Linear Regression with Python
Out[17]: Coefficient
Avg. Area Income 21.528276
Avg. Area House Age 164883.282027
Avg. Area Number of Rooms 122368.678027
Avg. Area Number of Bedrooms 2233.801864
Area Population 15.150420
In [19]: plt.scatter(y_test,predictions)
<matplotlib.collections.PathCollection at 0x7fdfa2f649a0>
Out[19]:
localhost:8888/nbconvert/html/Documents/Pelatihan/ABCD/My Works/Day 4 Machine Learning/Linear Regression/Sesi 4-2B Linear Regression with Python.ipynb?download=false 9/12
11/29/22, 8:46 PM Sesi 4-2B Linear Regression with Python
Residual Histogram
In [20]: sns.displot((y_test-predictions),bins=50);
localhost:8888/nbconvert/html/Documents/Pelatihan/ABCD/My Works/Day 4 Machine Learning/Linear Regression/Sesi 4-2B Linear Regression with Python.ipynb?download=false 10/12
11/29/22, 8:46 PM Sesi 4-2B Linear Regression with Python
Root Mean Squared Error (RMSE) is the square root of the mean of the squared errors:
n
1
2
^ )
∑ (yi − y i
⎷ n
i=1
localhost:8888/nbconvert/html/Documents/Pelatihan/ABCD/My Works/Day 4 Machine Learning/Linear Regression/Sesi 4-2B Linear Regression with Python.ipynb?download=false 11/12
11/29/22, 8:46 PM Sesi 4-2B Linear Regression with Python
MAE: 82288.22251914951
MSE: 10460958907.20899
RMSE: 102278.82922290903
Great Job!
localhost:8888/nbconvert/html/Documents/Pelatihan/ABCD/My Works/Day 4 Machine Learning/Linear Regression/Sesi 4-2B Linear Regression with Python.ipynb?download=false 12/12