Multiple Linear Regression¶
In [1]:
import pandas as pd
import numpy as np
from matplotlib.pyplot import subplots
house = pd.read_csv('house.csv')
house.head(3)
Out[1]:
| Order | PID | MS SubClass | MS Zoning | Lot Frontage | Lot Area | Street | Alley | Lot Shape | Land Contour | ... | Pool Area | Pool QC | Fence | Misc Feature | Misc Val | Mo Sold | Yr Sold | Sale Type | Sale Condition | SalePrice | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 526301100 | 20 | RL | 141.0 | 31770 | Pave | NaN | IR1 | Lvl | ... | 0 | NaN | NaN | NaN | 0 | 5 | 2010 | WD | Normal | 215000 |
| 1 | 2 | 526350040 | 20 | RH | 80.0 | 11622 | Pave | NaN | Reg | Lvl | ... | 0 | NaN | MnPrv | NaN | 0 | 6 | 2010 | WD | Normal | 105000 |
| 2 | 3 | 526351010 | 20 | RL | 81.0 | 14267 | Pave | NaN | IR1 | Lvl | ... | 0 | NaN | NaN | Gar2 | 12500 | 6 | 2010 | WD | Normal | 172000 |
3 rows × 82 columns
In [2]:
house = house[house['Bldg Type'] == '1Fam']
house = house[house['Sale Condition'] == 'Normal']
sales = house[['SalePrice', '1st Flr SF', '2nd Flr SF', 'Total Bsmt SF',
'Garage Area', 'Wood Deck SF', 'Open Porch SF', 'Lot Area',
'Year Built', 'Yr Sold']]
sales.head(3)
Out[2]:
| SalePrice | 1st Flr SF | 2nd Flr SF | Total Bsmt SF | Garage Area | Wood Deck SF | Open Porch SF | Lot Area | Year Built | Yr Sold | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 215000 | 1656 | 0 | 1080.0 | 528.0 | 210 | 62 | 31770 | 1960 | 2010 |
| 1 | 105000 | 896 | 0 | 882.0 | 730.0 | 140 | 0 | 11622 | 1961 | 2010 |
| 2 | 172000 | 1329 | 0 | 1329.0 | 312.0 | 393 | 36 | 14267 | 1958 | 2010 |
In [3]:
sales['SalePrice'].corr(sales['1st Flr SF'])
Out[3]:
np.float64(0.6424662541030226)
In [4]:
sales['SalePrice'].corr(sales['2nd Flr SF'])
Out[4]:
np.float64(0.3575218942800825)
In [5]:
sales['SalePrice'].corr((sales['2nd Flr SF'] + sales['1st Flr SF']))
Out[5]:
np.float64(0.7821920556134877)
In [7]:
n = sales.shape[0] # Number of observations
X = np.ones((n, 1))
X = np.insert(X, 1, sales['1st Flr SF'], axis=1)
X = np.insert(X, 2, sales['2nd Flr SF'], axis=1)
y = sales['SalePrice'].reset_index(drop=True)
X.shape
Out[7]:
(2002, 3)
In [9]:
Z = np.matmul(X.T, X)
t = np.linalg.inv(Z)@X.T@y
MSE = (1/n) * np.linalg.norm(y - X@t)**2
print('MSE = ', MSE)
print('Theta = ', t)
MSE = 1705349005.3197255 Theta = [-29985.3612312 155.19406373 91.35680257]
In [17]:
#Using Gradient Descent
learning_rate = 0.00000001
t = np.zeros(3).T
MSE = (1/n) * np.linalg.norm(y - X@t)**2
print('MSE, Theta:', MSE, t)
for i in range(11):
t = t + (2*learning_rate/n) * (X.T @ (y - X@t))
MSE = (1/n) * np.linalg.norm(y - X@t)**2
print('MSE, Theta:', MSE, t)
MSE = (1/n) * np.linalg.norm(y - X@t)**2
print('MSE, Theta:', MSE, t)
MSE, Theta: 37483374720.76574 [0. 0. 0.] MSE, Theta: 35329134615.48689 [3.58369254e-03 4.44420993e+00 1.45915604e+00] MSE, Theta: 33305851706.1281 [7.05555785e-03 8.74978866e+00 2.87760804e+00] MSE, Theta: 31405556990.79167 [1.04190511e-02 1.29210261e+01 4.25659660e+00] MSE, Theta: 29620766460.99461 [1.36775206e-02 1.69620795e+01 5.59732406e+00] MSE, Theta: 27944451584.46335 [1.68342113e-02 2.08769776e+01 6.90095566e+00] MSE, Theta: 26370011584.380478 [1.98922677e-02 2.46696247e+01 8.16862069e+00] MSE, Theta: 24891247405.749187 [2.28547373e-02 2.83438043e+01 9.40141360e+00] MSE, Theta: 23502337266.195404 [2.57245732e-02 3.19031829e+01 1.06003951e+01] MSE, Theta: 22197813694.776936 [2.85046374e-02 3.53513139e+01 1.17665931e+01] MSE, Theta: 20972541968.237617 [3.11977034e-02 3.86916405e+01 1.29010040e+01] MSE, Theta: 19821699859.656364 [3.38064589e-02 4.19274997e+01 1.40045933e+01] MSE, Theta: 19821699859.656364 [3.38064589e-02 4.19274997e+01 1.40045933e+01]
In [ ]: