{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "15c4831f-b5a6-4465-83c4-584f3197cba3",
   "metadata": {},
   "source": [
    "## Multiple Linear Regression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "437a6b6d-10b2-4d53-845d-c135966b3ed1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Order</th>\n",
       "      <th>PID</th>\n",
       "      <th>MS SubClass</th>\n",
       "      <th>MS Zoning</th>\n",
       "      <th>Lot Frontage</th>\n",
       "      <th>Lot Area</th>\n",
       "      <th>Street</th>\n",
       "      <th>Alley</th>\n",
       "      <th>Lot Shape</th>\n",
       "      <th>Land Contour</th>\n",
       "      <th>...</th>\n",
       "      <th>Pool Area</th>\n",
       "      <th>Pool QC</th>\n",
       "      <th>Fence</th>\n",
       "      <th>Misc Feature</th>\n",
       "      <th>Misc Val</th>\n",
       "      <th>Mo Sold</th>\n",
       "      <th>Yr Sold</th>\n",
       "      <th>Sale Type</th>\n",
       "      <th>Sale Condition</th>\n",
       "      <th>SalePrice</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>526301100</td>\n",
       "      <td>20</td>\n",
       "      <td>RL</td>\n",
       "      <td>141.0</td>\n",
       "      <td>31770</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>2010</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>215000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>526350040</td>\n",
       "      <td>20</td>\n",
       "      <td>RH</td>\n",
       "      <td>80.0</td>\n",
       "      <td>11622</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>MnPrv</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>2010</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>105000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>526351010</td>\n",
       "      <td>20</td>\n",
       "      <td>RL</td>\n",
       "      <td>81.0</td>\n",
       "      <td>14267</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Gar2</td>\n",
       "      <td>12500</td>\n",
       "      <td>6</td>\n",
       "      <td>2010</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>172000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 82 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Order        PID  MS SubClass MS Zoning  Lot Frontage  Lot Area Street  \\\n",
       "0      1  526301100           20        RL         141.0     31770   Pave   \n",
       "1      2  526350040           20        RH          80.0     11622   Pave   \n",
       "2      3  526351010           20        RL          81.0     14267   Pave   \n",
       "\n",
       "  Alley Lot Shape Land Contour  ... Pool Area Pool QC  Fence Misc Feature  \\\n",
       "0   NaN       IR1          Lvl  ...         0     NaN    NaN          NaN   \n",
       "1   NaN       Reg          Lvl  ...         0     NaN  MnPrv          NaN   \n",
       "2   NaN       IR1          Lvl  ...         0     NaN    NaN         Gar2   \n",
       "\n",
       "  Misc Val Mo Sold Yr Sold Sale Type  Sale Condition  SalePrice  \n",
       "0        0       5    2010       WD           Normal     215000  \n",
       "1        0       6    2010       WD           Normal     105000  \n",
       "2    12500       6    2010       WD           Normal     172000  \n",
       "\n",
       "[3 rows x 82 columns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from matplotlib.pyplot import subplots\n",
    "\n",
    "house = pd.read_csv('house.csv')\n",
    "house.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a884a475-15de-4395-9cce-feb389e0f2cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SalePrice</th>\n",
       "      <th>1st Flr SF</th>\n",
       "      <th>2nd Flr SF</th>\n",
       "      <th>Total Bsmt SF</th>\n",
       "      <th>Garage Area</th>\n",
       "      <th>Wood Deck SF</th>\n",
       "      <th>Open Porch SF</th>\n",
       "      <th>Lot Area</th>\n",
       "      <th>Year Built</th>\n",
       "      <th>Yr Sold</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>215000</td>\n",
       "      <td>1656</td>\n",
       "      <td>0</td>\n",
       "      <td>1080.0</td>\n",
       "      <td>528.0</td>\n",
       "      <td>210</td>\n",
       "      <td>62</td>\n",
       "      <td>31770</td>\n",
       "      <td>1960</td>\n",
       "      <td>2010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>105000</td>\n",
       "      <td>896</td>\n",
       "      <td>0</td>\n",
       "      <td>882.0</td>\n",
       "      <td>730.0</td>\n",
       "      <td>140</td>\n",
       "      <td>0</td>\n",
       "      <td>11622</td>\n",
       "      <td>1961</td>\n",
       "      <td>2010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>172000</td>\n",
       "      <td>1329</td>\n",
       "      <td>0</td>\n",
       "      <td>1329.0</td>\n",
       "      <td>312.0</td>\n",
       "      <td>393</td>\n",
       "      <td>36</td>\n",
       "      <td>14267</td>\n",
       "      <td>1958</td>\n",
       "      <td>2010</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   SalePrice  1st Flr SF  2nd Flr SF  Total Bsmt SF  Garage Area  \\\n",
       "0     215000        1656           0         1080.0        528.0   \n",
       "1     105000         896           0          882.0        730.0   \n",
       "2     172000        1329           0         1329.0        312.0   \n",
       "\n",
       "   Wood Deck SF  Open Porch SF  Lot Area  Year Built  Yr Sold  \n",
       "0           210             62     31770        1960     2010  \n",
       "1           140              0     11622        1961     2010  \n",
       "2           393             36     14267        1958     2010  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "house = house[house['Bldg Type'] == '1Fam']\n",
    "house = house[house['Sale Condition'] == 'Normal']\n",
    "sales = house[['SalePrice', '1st Flr SF', '2nd Flr SF', 'Total Bsmt SF', \n",
    "               'Garage Area', 'Wood Deck SF', 'Open Porch SF', 'Lot Area', \n",
    "               'Year Built', 'Yr Sold']]\n",
    "sales.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c6586bfa-881f-4bb4-8830-a93e68f97122",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.6424662541030226)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales['SalePrice'].corr(sales['1st Flr SF'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "36bfa44d-8d4e-43b1-9ffb-ec913f27263e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.3575218942800825)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales['SalePrice'].corr(sales['2nd Flr SF'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8899c0f5-8735-4baa-83f8-b3109d4165bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.float64(0.7821920556134877)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sales['SalePrice'].corr((sales['2nd Flr SF'] + sales['1st Flr SF']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1ed4e83d-4004-4956-8329-8526784311da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2002, 3)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = sales.shape[0] # Number of observations\n",
    "X = np.ones((n, 1))\n",
    "X = np.insert(X, 1, sales['1st Flr SF'], axis=1)\n",
    "X = np.insert(X, 2, sales['2nd Flr SF'], axis=1)\n",
    "y = sales['SalePrice'].reset_index(drop=True)\n",
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5991a480-6e65-46cf-9419-e3797ceeb473",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MSE =  1705349005.3197255\n",
      "Theta =  [-29985.3612312     155.19406373     91.35680257]\n"
     ]
    }
   ],
   "source": [
    "Z = np.matmul(X.T, X)\n",
    "t = np.linalg.inv(Z)@X.T@y\n",
    "MSE = (1/n) * np.linalg.norm(y - X@t)**2\n",
    "print('MSE = ', MSE)\n",
    "print('Theta = ', t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "b63dcceb-40f5-469a-b136-1e8089d818ab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MSE, Theta: 37483374720.76574 [0. 0. 0.]\n",
      "MSE, Theta: 35329134615.48689 [3.58369254e-03 4.44420993e+00 1.45915604e+00]\n",
      "MSE, Theta: 33305851706.1281 [7.05555785e-03 8.74978866e+00 2.87760804e+00]\n",
      "MSE, Theta: 31405556990.79167 [1.04190511e-02 1.29210261e+01 4.25659660e+00]\n",
      "MSE, Theta: 29620766460.99461 [1.36775206e-02 1.69620795e+01 5.59732406e+00]\n",
      "MSE, Theta: 27944451584.46335 [1.68342113e-02 2.08769776e+01 6.90095566e+00]\n",
      "MSE, Theta: 26370011584.380478 [1.98922677e-02 2.46696247e+01 8.16862069e+00]\n",
      "MSE, Theta: 24891247405.749187 [2.28547373e-02 2.83438043e+01 9.40141360e+00]\n",
      "MSE, Theta: 23502337266.195404 [2.57245732e-02 3.19031829e+01 1.06003951e+01]\n",
      "MSE, Theta: 22197813694.776936 [2.85046374e-02 3.53513139e+01 1.17665931e+01]\n",
      "MSE, Theta: 20972541968.237617 [3.11977034e-02 3.86916405e+01 1.29010040e+01]\n",
      "MSE, Theta: 19821699859.656364 [3.38064589e-02 4.19274997e+01 1.40045933e+01]\n",
      "MSE, Theta: 19821699859.656364 [3.38064589e-02 4.19274997e+01 1.40045933e+01]\n"
     ]
    }
   ],
   "source": [
    "#Using Gradient Descent\n",
    "learning_rate = 0.00000001\n",
    "t = np.zeros(3).T\n",
    "MSE = (1/n) * np.linalg.norm(y - X@t)**2\n",
    "print('MSE, Theta:', MSE, t)\n",
    "for i in range(11):\n",
    "    t = t + (2*learning_rate/n) * (X.T @ (y - X@t))\n",
    "    MSE = (1/n) * np.linalg.norm(y - X@t)**2\n",
    "    print('MSE, Theta:', MSE, t)\n",
    "\n",
    "MSE = (1/n) * np.linalg.norm(y - X@t)**2\n",
    "print('MSE, Theta:', MSE, t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3c521454-43b7-427b-bee5-6e61879f313c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
