{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "i6sPnhMH0raw"
   },
   "source": [
    "# House Price Prediction\n",
    "\n",
    "Think of finding the perfect house as a complex journey involving negotiations, research, and decision-making. Now, imagine having a smart guide that helps you navigate through this maze by analyzing data and predicting outcomes. Linear regression is that guide. In this tutorial, we'll explore how linear regression helps us understand and predict relationships in data, just like finding the ideal house by matching features with price. Let's get started and discover how this powerful tool can simplify your data-driven decisions!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "huwdh4Es3EZ2"
   },
   "source": [
    "## Setup\n",
    "\n",
    "The House Price Prediction Dataset contains 13 features\n",
    "\n",
    "| #  | Column Name    | Description                                                         |\n",
    "|----|----------------|---------------------------------------------------------------------|\n",
    "| 1  | Id             | To count the records.                                               |\n",
    "| 2  | MSSubClass     | Identifies the type of dwelling involved in the sale.               |\n",
    "| 3  | MSZoning       | Identifies the general zoning classification of the sale.           |\n",
    "| 4  | LotArea        | Lot size in square feet.                                            |\n",
    "| 5  | LotConfig      | Configuration of the lot                                            |\n",
    "| 6  | BldgType       | Type of dwelling                                                    |\n",
    "| 7  | OverallCond    | Rates the overall condition of the house                            |\n",
    "| 8  | YearBuilt      | Original construction year                                          |\n",
    "| 9  | YearRemodAdd   | Remodel date (same as construction date if no remodeling or additions). |\n",
    "| 10 | Exterior1st    | Exterior covering on house                                          |\n",
    "| 11 | BsmtFinSF2     | Type 2 finished square feet.                                        |\n",
    "| 12 | TotalBsmtSF    | Total square feet of basement area                                  |\n",
    "| 13 | SalePrice      | To be predicted                                                     |\n",
    "\n",
    "\n",
    "Run the cell below to download dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "W5Y7ZPKz0nf8",
    "outputId": "59427fb0-eca8-4437-848e-4fdbd8b9f022"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b7\u001b[1A\u001b[1G\u001b[27G[Files: 0  Bytes: 0  [0 B/s] Re]\u001b8\u001b7\u001b[2A\u001b[1G\u001b[27G[https://docs.google.com/spread]\u001b8\u001b7\u001b[2A\u001b[1G0 files              100% [=============================>]     349     --.-KB/s\u001b8\u001b7\u001b[1A\u001b[1G\u001b[27G[Files: 0  Bytes: 349  [311 B/s]\u001b8\u001b7\u001b[2A\u001b[1G0 files              100% [=============================>]     349     --.-KB/s\u001b8\u001b7\u001b[2A\u001b[1G\u001b[27G[https://doc-08-30-sheets.googl]\u001b8\u001b7\u001b[2A\u001b[1GhouseDataset.csv     100% [=============================>]   42.28K    --.-KB/s\u001b8\u001b7\u001b[1A\u001b[1G\u001b[27G[Files: 1  Bytes: 42.63K [20.06]\u001b8\u001b[m\u001b[m\u001b[m\u001b[m"
     ]
    }
   ],
   "source": [
    "!wget -O houseDataset.csv \"https://docs.google.com/spreadsheets/d/1caaR9pT24GNmq3rDQpMiIMJrmiTGarbs/export?format=csv&id=1caaR9pT24GNmq3rDQpMiIMJrmiTGarbs&gid=1150341366\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ecr_wCDI7tcT"
   },
   "source": [
    "## EDA"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "KLdgiCGJ9dN-"
   },
   "source": [
    "### Create and Display DataFrame\n",
    "\n",
    "pd.read_csv: Reads the CSV file into a DataFrame.\n",
    "\n",
    "df.columns.tolist(): Retrieves the column names from the DataFrame.\n",
    "\n",
    "df.head(): Displays the first few rows of the DataFrame for a quick preview."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "NhdMHsM4831s",
    "outputId": "474be38d-461a-43f5-cb58-7b04f6a8ea77"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DataFrame created successfully!\n",
      "\n",
      "Column Names:\n",
      "['Id', 'MSSubClass', 'MSZoning', 'LotArea', 'LotConfig', 'BldgType', 'OverallCond', 'YearBuilt', 'YearRemodAdd', 'Exterior1st', 'BsmtFinSF2', 'TotalBsmtSF', 'SalePrice']\n",
      "\n",
      "Data:\n",
      "   Id  MSSubClass MSZoning  LotArea LotConfig BldgType  OverallCond  \\\n",
      "0   0          60       RL     8450    Inside     1Fam            5   \n",
      "1   1          20       RL     9600       FR2     1Fam            8   \n",
      "2   2          60       RL    11250    Inside     1Fam            5   \n",
      "3   3          70       RL     9550    Corner     1Fam            5   \n",
      "4   4          60       RL    14260       FR2     1Fam            5   \n",
      "\n",
      "   YearBuilt  YearRemodAdd Exterior1st  BsmtFinSF2  TotalBsmtSF  SalePrice  \n",
      "0       2003          2003     VinylSd         0.0        856.0   208500.0  \n",
      "1       1976          1976     MetalSd         0.0       1262.0   181500.0  \n",
      "2       2001          2002     VinylSd         0.0        920.0   223500.0  \n",
      "3       1915          1970     Wd Sdng         0.0        756.0   140000.0  \n",
      "4       2000          2000     VinylSd         0.0       1145.0   250000.0  \n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "def create_and_display_dataframe(file_path):\n",
    "    df = pd.read_csv(file_path)\n",
    "\n",
    "    print(\"DataFrame created successfully!\")\n",
    "    print(\"\\nColumn Names:\")\n",
    "    print(df.columns.tolist())\n",
    "\n",
    "    print(\"\\nData:\")\n",
    "    print(df.head())\n",
    "\n",
    "file_path = 'houseDataset.csv'\n",
    "create_and_display_dataframe(file_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "hSf2soXG-ceA"
   },
   "source": [
    "### Correlation Matrix\n",
    "\n",
    "The correlation coefficient between two variables\n",
    "X and\n",
    "Y can be calculated using the formula:\n",
    "\n",
    "![cov1.png](data:image/png;base64,[encoded data, 10372 characters])\n",
    "\n",
    "Here’s how you can calculate the correlation matrix **manually** using the pandas DataFrame:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "HCg9xSy_9VUB",
    "outputId": "fc88ebd1-54c0-4f38-f29a-a32d53a9e83f"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Correlation Matrix:\n",
      "Id: {'Id': np.float64(0.9996574169236039), 'MSSubClass': np.float64(0.008927562480306352), 'LotArea': np.float64(-0.0407321028950552), 'OverallCond': np.float64(-0.002838510751975099), 'YearBuilt': np.float64(-0.01657533184893819), 'YearRemodAdd': np.float64(-0.05042031403232767), 'BsmtFinSF2': np.float64(0.018245950570756973), 'TotalBsmtSF': np.float64(-0.024917399703561802), 'SalePrice': np.float64(-0.010956479916617771)}\n",
      "MSSubClass: {'Id': np.float64(0.008927562480306352), 'MSSubClass': np.float64(0.9996574169236039), 'LotArea': np.float64(-0.20166093967197754), 'OverallCond': np.float64(-0.06560255579623428), 'YearBuilt': np.float64(0.03439695580602711), 'YearRemodAdd': np.float64(0.04330007459603745), 'BsmtFinSF2': np.float64(-0.07250762840452592), 'TotalBsmtSF': np.float64(-0.21989847983040878), 'SalePrice': np.float64(-0.08379642185504654)}\n",
      "LotArea: {'Id': np.float64(-0.0407321028950552), 'MSSubClass': np.float64(-0.20166093967197754), 'LotArea': np.float64(0.999657416923604), 'OverallCond': np.float64(-0.03560499881535773), 'YearBuilt': np.float64(0.0241195581530019), 'YearRemodAdd': np.float64(0.021604859270227504), 'BsmtFinSF2': np.float64(0.08404013983833278), 'TotalBsmtSF': np.float64(0.2540817476113896), 'SalePrice': np.float64(0.333674130842095)}\n",
      "OverallCond: {'Id': np.float64(-0.002838510751975099), 'MSSubClass': np.float64(-0.06560255579623428), 'LotArea': np.float64(-0.03560499881535773), 'OverallCond': np.float64(0.999657416923604), 'YearBuilt': np.float64(-0.36835045461557436), 'YearRemodAdd': np.float64(0.04763768915013037), 'BsmtFinSF2': np.float64(0.04148225197214482), 'TotalBsmtSF': np.float64(-0.17392254934878815), 'SalePrice': np.float64(-0.07777940410944484)}\n",
      "YearBuilt: {'Id': np.float64(-0.01657533184893819), 'MSSubClass': np.float64(0.03439695580602711), 'LotArea': np.float64(0.0241195581530019), 'OverallCond': np.float64(-0.36835045461557436), 'YearBuilt': np.float64(0.9996574169236039), 'YearRemodAdd': np.float64(0.6120248285532728), 'BsmtFinSF2': np.float64(-0.02758664362635253), 'TotalBsmtSF': np.float64(0.4083956282217249), 'SalePrice': np.float64(0.5210118777503243)}\n",
      "YearRemodAdd: {'Id': np.float64(-0.05042031403232767), 'MSSubClass': np.float64(0.04330007459603745), 'LotArea': np.float64(0.021604859270227504), 'OverallCond': np.float64(0.04763768915013037), 'YearBuilt': np.float64(0.6120248285532728), 'YearRemodAdd': np.float64(0.9996574169236041), 'BsmtFinSF2': np.float64(-0.062113952131322744), 'TotalBsmtSF': np.float64(0.2979181175899782), 'SalePrice': np.float64(0.5007161212690244)}\n",
      "BsmtFinSF2: {'Id': np.float64(0.018245950570756973), 'MSSubClass': np.float64(-0.07250762840452592), 'LotArea': np.float64(0.08404013983833278), 'OverallCond': np.float64(0.04148225197214482), 'YearBuilt': np.float64(-0.02758664362635253), 'YearRemodAdd': np.float64(-0.062113952131322744), 'BsmtFinSF2': np.float64(0.9996572995202193), 'TotalBsmtSF': np.float64(0.08937931109175348), 'SalePrice': np.float64(-0.010840379745732428)}\n",
      "TotalBsmtSF: {'Id': np.float64(-0.024917399703561802), 'MSSubClass': np.float64(-0.21989847983040878), 'LotArea': np.float64(0.2540817476113896), 'OverallCond': np.float64(-0.17392254934878815), 'YearBuilt': np.float64(0.4083956282217249), 'YearRemodAdd': np.float64(0.2979181175899782), 'BsmtFinSF2': np.float64(0.08937931109175348), 'TotalBsmtSF': np.float64(0.9996572995202194), 'SalePrice': np.float64(0.6102932777069716)}\n",
      "SalePrice: {'Id': np.float64(-0.010956479916617771), 'MSSubClass': np.float64(-0.08379642185504654), 'LotArea': np.float64(0.333674130842095), 'OverallCond': np.float64(-0.07777940410944484), 'YearBuilt': np.float64(0.5210118777503243), 'YearRemodAdd': np.float64(0.5007161212690244), 'BsmtFinSF2': np.float64(-0.010840379745732428), 'TotalBsmtSF': np.float64(0.6102932777069716), 'SalePrice': np.float64(0.9993150684931505)}\n"
     ]
    }
   ],
   "source": [
    "class DataFrame:\n",
    "    def __init__(self, file_path):\n",
    "        self.df = pd.read_csv(file_path)\n",
    "\n",
    "    def calculate_correlation_matrix(self):\n",
    "        # Get numerical columns\n",
    "        numerical_columns = self.df.select_dtypes(include=['float64', 'int64']).columns\n",
    "        num_cols = len(numerical_columns)\n",
    "\n",
    "        # Create an empty correlation matrix\n",
    "        corr_matrix = {col: {col2: 0 for col2 in numerical_columns} for col in numerical_columns}\n",
    "\n",
    "        for i in range(num_cols):\n",
    "            for j in range(i, num_cols):\n",
    "                col1 = numerical_columns[i]\n",
    "                col2 = numerical_columns[j]\n",
    "\n",
    "                # Calculate mean\n",
    "                mean1 = self.df[col1].mean()\n",
    "                mean2 = self.df[col2].mean()\n",
    "\n",
    "                # Calculate covariance\n",
    "                covariance = ((self.df[col1] - mean1) * (self.df[col2] - mean2)).mean()\n",
    "\n",
    "                # Calculate standard deviations\n",
    "                std_dev1 = self.df[col1].std()\n",
    "                std_dev2 = self.df[col2].std()\n",
    "\n",
    "                # Calculate correlation\n",
    "                correlation = covariance / (std_dev1 * std_dev2)\n",
    "\n",
    "                # Store in matrix\n",
    "                corr_matrix[col1][col2] = correlation\n",
    "                corr_matrix[col2][col1] = correlation  # Symmetric matrix\n",
    "\n",
    "        return corr_matrix\n",
    "\n",
    "    def display_correlation_matrix(self):\n",
    "        corr_matrix = self.calculate_correlation_matrix()\n",
    "        print(\"Correlation Matrix:\")\n",
    "        for col1, row in corr_matrix.items():\n",
    "            print(f\"{col1}: {row}\")\n",
    "\n",
    "file_path = 'dataset.csv'\n",
    "df = DataFrame(file_path)\n",
    "df.display_correlation_matrix()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "5J-Lcp5S_0O1"
   },
   "source": [
    "Using pandas, you can easily compute the correlation matrix with just a single method call. The output is cleaner, easier to interpret, and consistent with standard practices, making it a more efficient choice for handling real-world data tasks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 788
    },
    "id": "ZkzU19EO_vLw",
    "outputId": "1eb228eb-4d7e-4fd1-e519-fca38e6caf44"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 124968 characters]\n",
      "text/plain": [
       "<Figure size 1000x800 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "df = pd.read_csv('/content/dataset.csv')\n",
    "\n",
    "\n",
    "numeric_df = df.select_dtypes(include=['float64', 'int64'])\n",
    "\n",
    "# Compute the correlation matrix\n",
    "corr_matrix = numeric_df.corr()\n",
    "\n",
    "plt.figure(figsize=(10, 8))\n",
    "sns.heatmap(corr_matrix, annot=True, fmt='.2f', cmap='coolwarm', vmin=-1, vmax=1, center=0)\n",
    "\n",
    "plt.title('Correlation Matrix Heatmap')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "lh3HJUOXBnRH"
   },
   "source": [
    "### Mean, Median, Mode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "nFLvh7cPArWH",
    "outputId": "61e8edda-0547-48ce-8e85-1e4c5af20b21"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Statistics for Numeric Columns:\n",
      "\n",
      "Column: Id\n",
      "Mean: 1459.0\n",
      "Median: 1459.0\n",
      "Mode: 0\n",
      "\n",
      "Column: MSSubClass\n",
      "Mean: 57.1377183967112\n",
      "Median: 50.0\n",
      "Mode: 20\n",
      "\n",
      "Column: LotArea\n",
      "Mean: 10168.11408016444\n",
      "Median: 9453.0\n",
      "Mode: 9600\n",
      "\n",
      "Column: OverallCond\n",
      "Mean: 5.564576909900651\n",
      "Median: 5.0\n",
      "Mode: 5\n",
      "\n",
      "Column: YearBuilt\n",
      "Mean: 1971.3127783487496\n",
      "Median: 1973.0\n",
      "Mode: 2005\n",
      "\n",
      "Column: YearRemodAdd\n",
      "Mean: 1984.2644741349777\n",
      "Median: 1993.0\n",
      "Mode: 1950\n",
      "\n",
      "Column: BsmtFinSF2\n",
      "Mean: 49.58224811514736\n",
      "Median: 0.0\n",
      "Mode: 0.0\n",
      "\n",
      "Column: TotalBsmtSF\n",
      "Mean: 1051.7775873886224\n",
      "Median: 989.5\n",
      "Mode: 0.0\n",
      "\n",
      "Column: SalePrice\n",
      "Mean: 180921.19589041095\n",
      "Median: 163000.0\n",
      "Mode: 140000.0\n"
     ]
    }
   ],
   "source": [
    "print(\"Statistics for Numeric Columns:\")\n",
    "for column in numeric_df.columns:\n",
    "    print(f\"\\nColumn: {column}\")\n",
    "    print(f\"Mean: {numeric_df[column].mean()}\")\n",
    "    print(f\"Median: {numeric_df[column].median()}\")\n",
    "    print(f\"Mode: {numeric_df[column].mode()[0]}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "sEmslgVqB_EK"
   },
   "source": [
    "### Count NA values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "fZlMMiu_B6hP",
    "outputId": "c96a35d3-48f5-4556-f445-6cff6ff74cbb"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Columns with NA values:\n",
      "MSZoning: 4\n",
      "Exterior1st: 1\n",
      "BsmtFinSF2: 1\n",
      "TotalBsmtSF: 1\n",
      "SalePrice: 1459\n"
     ]
    }
   ],
   "source": [
    "def count_na_values(df):\n",
    "    na_counts = {col: 0 for col in df.columns}\n",
    "\n",
    "    # Iterate through each row and column\n",
    "    for index, row in df.iterrows():\n",
    "        for col in df.columns:\n",
    "            # Check for NA values\n",
    "            if pd.isna(row[col]):\n",
    "                na_counts[col] += 1\n",
    "\n",
    "    print(\"Columns with NA values:\")\n",
    "    for column, count in na_counts.items():\n",
    "        if count > 0:\n",
    "            print(f\"{column}: {count}\")\n",
    "\n",
    "count_na_values(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "axqEIpelDKWz"
   },
   "source": [
    "### Range"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "KWQy6HdmCbs6",
    "outputId": "29cd97bd-0f9a-48ec-cc00-97f3c9698ca2"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Minimum and Maximum Values for Numeric Columns:\n",
      "Id: Min = 0, Max = 2918\n",
      "MSSubClass: Min = 20, Max = 190\n",
      "LotArea: Min = 1300, Max = 215245\n",
      "OverallCond: Min = 1, Max = 9\n",
      "YearBuilt: Min = 1872, Max = 2010\n",
      "YearRemodAdd: Min = 1950, Max = 2010\n",
      "BsmtFinSF2: Min = 0.0, Max = 1526.0\n",
      "TotalBsmtSF: Min = 0.0, Max = 6110.0\n",
      "SalePrice: Min = 34900.0, Max = 755000.0\n"
     ]
    }
   ],
   "source": [
    "def calculate_min_max(df):\n",
    "    min_values = {}\n",
    "    max_values = {}\n",
    "\n",
    "    # Iterate over each column\n",
    "    for col in df.columns:\n",
    "        # Skip non-numeric columns\n",
    "        if df[col].dtype in ['float64', 'int64']:\n",
    "            min_value = float('inf')\n",
    "            max_value = float('-inf')\n",
    "\n",
    "            # Iterate over each value in the column\n",
    "            for value in df[col]:\n",
    "                # Check for NA values and skip them\n",
    "                if pd.isna(value):\n",
    "                    continue\n",
    "\n",
    "                if value < min_value:\n",
    "                    min_value = value\n",
    "                if value > max_value:\n",
    "                    max_value = value\n",
    "\n",
    "            if min_value != float('inf') and max_value != float('-inf'):\n",
    "                min_values[col] = min_value\n",
    "                max_values[col] = max_value\n",
    "\n",
    "    print(\"Minimum and Maximum Values for Numeric Columns:\")\n",
    "    for col in min_values:\n",
    "        print(f\"{col}: Min = {min_values[col]}, Max = {max_values[col]}\")\n",
    "\n",
    "calculate_min_max(df)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "JirLmOE4DNZZ"
   },
   "source": [
    "### Categorical Columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 563
    },
    "id": "2lxPgYzsEjkh",
    "outputId": "2a7f72f0-6e12-47e8-e871-c0c1973f570f"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32644 characters]\n",
      "text/plain": [
       "<Figure size 1000x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "categorical_columns = df.select_dtypes(include=['object', 'category']).columns\n",
    "\n",
    "unique_counts = df[categorical_columns].nunique()\n",
    "\n",
    "plt.figure(figsize=(10, 6))\n",
    "unique_counts.sort_values().plot(kind='barh', color='skyblue')\n",
    "plt.xlabel('Number of Unique Values')\n",
    "plt.ylabel('Categorical Features')\n",
    "plt.title('Number of Unique Values in Categorical Features')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "OY-x5zN6F55B"
   },
   "source": [
    "To findout the actual count of each category we can plot the bargraph of each four features separately."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 382
    },
    "id": "FH6Pf9AcEyYL",
    "outputId": "ed142cd9-0e02-4d1a-b375-30ae016dde85"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-9-e739720434fe>:8: MatplotlibDeprecationWarning: Auto-removal of overlapping axes is deprecated since 3.6 and will be removed two minor releases later; explicitly call ax.remove() as needed.\n",
      "  plt.subplot(11, 4, index)\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 59068 characters]\n",
      "text/plain": [
       "<Figure size 1800x3600 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(18, 36))\n",
    "plt.title('Categorical Features: Distribution')\n",
    "plt.xticks(rotation=90)\n",
    "index = 1\n",
    "\n",
    "for col in categorical_columns:\n",
    "    y = df[col].value_counts()\n",
    "    plt.subplot(11, 4, index)\n",
    "    plt.xticks(rotation=90)\n",
    "    sns.barplot(x=list(y.index), y=y)\n",
    "    index += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "KtYExjQRHBQo"
   },
   "source": [
    "# Preprocessing"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "y6V1OOb8E-9D"
   },
   "source": [
    "As in our dataset, there are some columns that are not important and irrelevant for the model training. So, we can drop that column before training. There are 2 approaches to dealing with empty/null values\n",
    "\n",
    "We can easily delete the column/row (if the feature or record is not much important).\n",
    "\n",
    "Filling the empty slots with mean/mode/0/NA/etc. (depending on the dataset requirement).\n",
    "\n",
    "Dropping ID Column as it doesn't participate in prediction\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "x7n5GmAWwoO_"
   },
   "outputs": [],
   "source": [
    "df.drop(['Id'],\n",
    "\t\t\taxis=1,\n",
    "\t\t\tinplace=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "TuPS3m9fGmnf",
    "outputId": "7fbb4b86-f311-4e3c-ad2b-ad1324bf36d9"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Columns with NA values:\n",
      "MSZoning: 4\n",
      "Exterior1st: 1\n",
      "BsmtFinSF2: 1\n",
      "TotalBsmtSF: 1\n"
     ]
    }
   ],
   "source": [
    "# Custom function to fill missing values with the mean\n",
    "pd.options.mode.chained_assignment = None  # default='warn'\n",
    "\n",
    "df_copy = df.copy()\n",
    "def fill_missing_with_mean(data, column_name):\n",
    "    \"\"\"\n",
    "    Args:\n",
    "        data: The pandas DataFrame containing the data.\n",
    "        column_name: The name of the column to fill missing values in.\n",
    "    \"\"\"\n",
    "    total_sum = 0\n",
    "    count = 0\n",
    "    for value in data[column_name]:\n",
    "        if not pd.isna(value):  # Check for missing values\n",
    "            total_sum += value\n",
    "            count += 1\n",
    "    mean_value = total_sum / count\n",
    "\n",
    "    # Replace missing values with the calculated mean\n",
    "    for i in range(len(data[column_name])):\n",
    "        if pd.isna(data[column_name][i]):\n",
    "            data[column_name][i] = mean_value\n",
    "\n",
    "fill_missing_with_mean(df_copy, 'SalePrice')\n",
    "count_na_values(df_copy)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "RLOKcTLZGyGB"
   },
   "source": [
    "It's a lot easier with Pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "XFWhKDBVGqGe"
   },
   "outputs": [],
   "source": [
    "# df['SalePrice'].fillna(df['SalePrice'].mean(), inplace=True)\n",
    "# count_na_values(df)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "94P_JKZtGxjJ"
   },
   "source": [
    "As we can see there are still NA values available in the dataset but they're very less in number. So, dropping the records with Null values!\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "2LrDmZryG9Pm",
    "outputId": "45c648d4-bc87-4a2d-c251-02522d32a1da"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Columns with NA values:\n"
     ]
    }
   ],
   "source": [
    "new_data = df_copy.dropna()\n",
    "count_na_values(new_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "WbG2fnsIHQmn"
   },
   "source": [
    "One hot Encoding is the best way to convert categorical data into binary vectors. This maps the values to integer values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "N1LeRnnTG_x9",
    "outputId": "70f77b9d-c86c-4659-fce8-1922514005a5"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   MSSubClass  LotArea LotConfig BldgType  OverallCond  YearBuilt  \\\n",
      "0          60     8450    Inside     1Fam            5       2003   \n",
      "1          20     9600       FR2     1Fam            8       1976   \n",
      "2          60    11250    Inside     1Fam            5       2001   \n",
      "3          70     9550    Corner     1Fam            5       1915   \n",
      "4          60    14260       FR2     1Fam            5       2000   \n",
      "\n",
      "   YearRemodAdd Exterior1st  BsmtFinSF2  TotalBsmtSF  SalePrice  MSZoning_RL  \\\n",
      "0          2003     VinylSd         0.0        856.0   208500.0            1   \n",
      "1          1976     MetalSd         0.0       1262.0   181500.0            1   \n",
      "2          2002     VinylSd         0.0        920.0   223500.0            1   \n",
      "3          1970     Wd Sdng         0.0        756.0   140000.0            1   \n",
      "4          2000     VinylSd         0.0       1145.0   250000.0            1   \n",
      "\n",
      "   MSZoning_RM  MSZoning_C (all)  MSZoning_FV  MSZoning_RH  \n",
      "0            0                 0            0            0  \n",
      "1            0                 0            0            0  \n",
      "2            0                 0            0            0  \n",
      "3            0                 0            0            0  \n",
      "4            0                 0            0            0  \n"
     ]
    }
   ],
   "source": [
    "def one_hot_encode(df, column_name):\n",
    "  \"\"\"\n",
    "  Args:\n",
    "    df: The pandas DataFrame containing the data.\n",
    "    column_name: The name of the column to one-hot encode.\n",
    "\n",
    "  Returns:\n",
    "    The DataFrame with the one-hot encoded columns.\n",
    "  \"\"\"\n",
    "  unique_values = df[column_name].unique()\n",
    "  for value in unique_values:\n",
    "    df[column_name + \"_\" + str(value)] = (df[column_name] == value).astype(int)\n",
    "  df = df.drop(column_name, axis=1)\n",
    "  return df\n",
    "\n",
    "df_encoded = one_hot_encode(new_data.copy(), 'MSZoning')\n",
    "print(df_encoded.head())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "BjNQciWoHbTW"
   },
   "source": [
    "This can be easily implemented with Sklearn library"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Q0PeWhNYHTdl",
    "outputId": "e1c90011-1985-41b4-c694-b4503f7d715d"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Categorical variables:\n",
      "['MSZoning', 'LotConfig', 'BldgType', 'Exterior1st']\n",
      "No. of. categorical features:  4\n"
     ]
    }
   ],
   "source": [
    "from sklearn.preprocessing import OneHotEncoder\n",
    "\n",
    "s = (new_data.dtypes == 'object')\n",
    "object_cols = list(s[s].index)\n",
    "print(\"Categorical variables:\")\n",
    "print(object_cols)\n",
    "print('No. of. categorical features: ',\n",
    "\tlen(object_cols))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 298
    },
    "id": "HwH4nB9QILS8",
    "outputId": "aaa8fb92-0fc0-47ab-bf95-4e2aac1ec932"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.10/dist-packages/sklearn/preprocessing/_encoders.py:975: FutureWarning: `sparse` was renamed to `sparse_output` in version 1.2 and will be removed in 1.4. `sparse_output` is ignored unless you leave `sparse` to its default value.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "application/vnd.google.colaboratory.intrinsic+json": {
       "type": "dataframe",
       "variable_name": "df_final"
      },
      "text/html": [
       "\n",
       "  <div id=\"df-b18f156d-20a0-4271-8e34-6f46c31982a2\" class=\"colab-df-container\">\n",
       "    <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>MSSubClass</th>\n",
       "      <th>LotArea</th>\n",
       "      <th>OverallCond</th>\n",
       "      <th>YearBuilt</th>\n",
       "      <th>YearRemodAdd</th>\n",
       "      <th>BsmtFinSF2</th>\n",
       "      <th>TotalBsmtSF</th>\n",
       "      <th>SalePrice</th>\n",
       "      <th>MSZoning_C (all)</th>\n",
       "      <th>MSZoning_FV</th>\n",
       "      <th>...</th>\n",
       "      <th>Exterior1st_CemntBd</th>\n",
       "      <th>Exterior1st_HdBoard</th>\n",
       "      <th>Exterior1st_ImStucc</th>\n",
       "      <th>Exterior1st_MetalSd</th>\n",
       "      <th>Exterior1st_Plywood</th>\n",
       "      <th>Exterior1st_Stone</th>\n",
       "      <th>Exterior1st_Stucco</th>\n",
       "      <th>Exterior1st_VinylSd</th>\n",
       "      <th>Exterior1st_Wd Sdng</th>\n",
       "      <th>Exterior1st_WdShing</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>60</td>\n",
       "      <td>8450</td>\n",
       "      <td>5</td>\n",
       "      <td>2003</td>\n",
       "      <td>2003</td>\n",
       "      <td>0.0</td>\n",
       "      <td>856.0</td>\n",
       "      <td>208500.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20</td>\n",
       "      <td>9600</td>\n",
       "      <td>8</td>\n",
       "      <td>1976</td>\n",
       "      <td>1976</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1262.0</td>\n",
       "      <td>181500.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>60</td>\n",
       "      <td>11250</td>\n",
       "      <td>5</td>\n",
       "      <td>2001</td>\n",
       "      <td>2002</td>\n",
       "      <td>0.0</td>\n",
       "      <td>920.0</td>\n",
       "      <td>223500.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>70</td>\n",
       "      <td>9550</td>\n",
       "      <td>5</td>\n",
       "      <td>1915</td>\n",
       "      <td>1970</td>\n",
       "      <td>0.0</td>\n",
       "      <td>756.0</td>\n",
       "      <td>140000.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>60</td>\n",
       "      <td>14260</td>\n",
       "      <td>5</td>\n",
       "      <td>2000</td>\n",
       "      <td>2000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1145.0</td>\n",
       "      <td>250000.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 38 columns</p>\n",
       "</div>\n",
       "    <div class=\"colab-df-buttons\">\n",
       "\n",
       "  <div class=\"colab-df-container\">\n",
       "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-b18f156d-20a0-4271-8e34-6f46c31982a2')\"\n",
       "            title=\"Convert this dataframe to an interactive table.\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
       "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
       "  </svg>\n",
       "    </button>\n",
       "\n",
       "  <style>\n",
       "    .colab-df-container {\n",
       "      display:flex;\n",
       "      gap: 12px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert {\n",
       "      background-color: #E8F0FE;\n",
       "      border: none;\n",
       "      border-radius: 50%;\n",
       "      cursor: pointer;\n",
       "      display: none;\n",
       "      fill: #1967D2;\n",
       "      height: 32px;\n",
       "      padding: 0 0 0 0;\n",
       "      width: 32px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert:hover {\n",
       "      background-color: #E2EBFA;\n",
       "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "      fill: #174EA6;\n",
       "    }\n",
       "\n",
       "    .colab-df-buttons div {\n",
       "      margin-bottom: 4px;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert {\n",
       "      background-color: #3B4455;\n",
       "      fill: #D2E3FC;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert:hover {\n",
       "      background-color: #434B5C;\n",
       "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
       "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
       "      fill: #FFFFFF;\n",
       "    }\n",
       "  </style>\n",
       "\n",
       "    <script>\n",
       "      const buttonEl =\n",
       "        document.querySelector('#df-b18f156d-20a0-4271-8e34-6f46c31982a2 button.colab-df-convert');\n",
       "      buttonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "\n",
       "      async function convertToInteractive(key) {\n",
       "        const element = document.querySelector('#df-b18f156d-20a0-4271-8e34-6f46c31982a2');\n",
       "        const dataTable =\n",
       "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
       "                                                    [key], {});\n",
       "        if (!dataTable) return;\n",
       "\n",
       "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
       "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
       "          + ' to learn more about interactive tables.';\n",
       "        element.innerHTML = '';\n",
       "        dataTable['output_type'] = 'display_data';\n",
       "        await google.colab.output.renderOutput(dataTable, element);\n",
       "        const docLink = document.createElement('div');\n",
       "        docLink.innerHTML = docLinkHtml;\n",
       "        element.appendChild(docLink);\n",
       "      }\n",
       "    </script>\n",
       "  </div>\n",
       "\n",
       "\n",
       "<div id=\"df-dd32d7a6-4099-4166-a557-a14a66aa420f\">\n",
       "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-dd32d7a6-4099-4166-a557-a14a66aa420f')\"\n",
       "            title=\"Suggest charts\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
       "     width=\"24px\">\n",
       "    <g>\n",
       "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
       "    </g>\n",
       "</svg>\n",
       "  </button>\n",
       "\n",
       "<style>\n",
       "  .colab-df-quickchart {\n",
       "      --bg-color: #E8F0FE;\n",
       "      --fill-color: #1967D2;\n",
       "      --hover-bg-color: #E2EBFA;\n",
       "      --hover-fill-color: #174EA6;\n",
       "      --disabled-fill-color: #AAA;\n",
       "      --disabled-bg-color: #DDD;\n",
       "  }\n",
       "\n",
       "  [theme=dark] .colab-df-quickchart {\n",
       "      --bg-color: #3B4455;\n",
       "      --fill-color: #D2E3FC;\n",
       "      --hover-bg-color: #434B5C;\n",
       "      --hover-fill-color: #FFFFFF;\n",
       "      --disabled-bg-color: #3B4455;\n",
       "      --disabled-fill-color: #666;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart {\n",
       "    background-color: var(--bg-color);\n",
       "    border: none;\n",
       "    border-radius: 50%;\n",
       "    cursor: pointer;\n",
       "    display: none;\n",
       "    fill: var(--fill-color);\n",
       "    height: 32px;\n",
       "    padding: 0;\n",
       "    width: 32px;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart:hover {\n",
       "    background-color: var(--hover-bg-color);\n",
       "    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "    fill: var(--button-hover-fill-color);\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart-complete:disabled,\n",
       "  .colab-df-quickchart-complete:disabled:hover {\n",
       "    background-color: var(--disabled-bg-color);\n",
       "    fill: var(--disabled-fill-color);\n",
       "    box-shadow: none;\n",
       "  }\n",
       "\n",
       "  .colab-df-spinner {\n",
       "    border: 2px solid var(--fill-color);\n",
       "    border-color: transparent;\n",
       "    border-bottom-color: var(--fill-color);\n",
       "    animation:\n",
       "      spin 1s steps(1) infinite;\n",
       "  }\n",
       "\n",
       "  @keyframes spin {\n",
       "    0% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "      border-left-color: var(--fill-color);\n",
       "    }\n",
       "    20% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    30% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    40% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    60% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    80% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "    90% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "  }\n",
       "</style>\n",
       "\n",
       "  <script>\n",
       "    async function quickchart(key) {\n",
       "      const quickchartButtonEl =\n",
       "        document.querySelector('#' + key + ' button');\n",
       "      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n",
       "      quickchartButtonEl.classList.add('colab-df-spinner');\n",
       "      try {\n",
       "        const charts = await google.colab.kernel.invokeFunction(\n",
       "            'suggestCharts', [key], {});\n",
       "      } catch (error) {\n",
       "        console.error('Error during call to suggestCharts:', error);\n",
       "      }\n",
       "      quickchartButtonEl.classList.remove('colab-df-spinner');\n",
       "      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
       "    }\n",
       "    (() => {\n",
       "      let quickchartButtonEl =\n",
       "        document.querySelector('#df-dd32d7a6-4099-4166-a557-a14a66aa420f button');\n",
       "      quickchartButtonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "    })();\n",
       "  </script>\n",
       "</div>\n",
       "\n",
       "    </div>\n",
       "  </div>\n"
      ],
      "text/plain": [
       "   MSSubClass  LotArea  OverallCond  YearBuilt  YearRemodAdd  BsmtFinSF2  \\\n",
       "0          60     8450            5       2003          2003         0.0   \n",
       "1          20     9600            8       1976          1976         0.0   \n",
       "2          60    11250            5       2001          2002         0.0   \n",
       "3          70     9550            5       1915          1970         0.0   \n",
       "4          60    14260            5       2000          2000         0.0   \n",
       "\n",
       "   TotalBsmtSF  SalePrice  MSZoning_C (all)  MSZoning_FV  ...  \\\n",
       "0        856.0   208500.0               0.0          0.0  ...   \n",
       "1       1262.0   181500.0               0.0          0.0  ...   \n",
       "2        920.0   223500.0               0.0          0.0  ...   \n",
       "3        756.0   140000.0               0.0          0.0  ...   \n",
       "4       1145.0   250000.0               0.0          0.0  ...   \n",
       "\n",
       "   Exterior1st_CemntBd  Exterior1st_HdBoard  Exterior1st_ImStucc  \\\n",
       "0                  0.0                  0.0                  0.0   \n",
       "1                  0.0                  0.0                  0.0   \n",
       "2                  0.0                  0.0                  0.0   \n",
       "3                  0.0                  0.0                  0.0   \n",
       "4                  0.0                  0.0                  0.0   \n",
       "\n",
       "   Exterior1st_MetalSd  Exterior1st_Plywood  Exterior1st_Stone  \\\n",
       "0                  0.0                  0.0                0.0   \n",
       "1                  1.0                  0.0                0.0   \n",
       "2                  0.0                  0.0                0.0   \n",
       "3                  0.0                  0.0                0.0   \n",
       "4                  0.0                  0.0                0.0   \n",
       "\n",
       "   Exterior1st_Stucco  Exterior1st_VinylSd  Exterior1st_Wd Sdng  \\\n",
       "0                 0.0                  1.0                  0.0   \n",
       "1                 0.0                  0.0                  0.0   \n",
       "2                 0.0                  1.0                  0.0   \n",
       "3                 0.0                  0.0                  1.0   \n",
       "4                 0.0                  1.0                  0.0   \n",
       "\n",
       "   Exterior1st_WdShing  \n",
       "0                  0.0  \n",
       "1                  0.0  \n",
       "2                  0.0  \n",
       "3                  0.0  \n",
       "4                  0.0  \n",
       "\n",
       "[5 rows x 38 columns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "OH_encoder = OneHotEncoder(sparse=False)\n",
    "OH_cols = pd.DataFrame(OH_encoder.fit_transform(new_data[object_cols]))\n",
    "OH_cols.index = new_data.index\n",
    "# Use get_feature_names_out() for newer scikit-learn versions\n",
    "OH_cols.columns = OH_encoder.get_feature_names_out()\n",
    "df_final = new_data.drop(object_cols, axis=1)\n",
    "df_final = pd.concat([df_final, OH_cols], axis=1)\n",
    "df_final.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "eZDFB6FZv1k-"
   },
   "source": [
    "### Scaling"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "bFOLI05z2940",
    "outputId": "5a725fdb-1772-4962-c505-51926d71d4c6"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(755000.0, 34900.0)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_max, label_min = df[\"SalePrice\"].max(), df[\"SalePrice\"].min()\n",
    "label_max, label_min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "70VWqfAKv3J-"
   },
   "outputs": [],
   "source": [
    "df_normalized = df_final.apply(lambda x: (x - x.min()) / (x.max() - x.min()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "Ve81LFh9wEs0"
   },
   "outputs": [],
   "source": [
    "# from sklearn.preprocessing import MinMaxScaler\n",
    "# scaler = MinMaxScaler()\n",
    "# df_normalized = pd.DataFrame(scaler.fit_transform(df), columns=df.columns)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "TCfu66R_cyAJ"
   },
   "source": [
    "# Data splitting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "SqKZLT33eAjM"
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "def train_test_split(X, y, test_size=0.2, random_state=None):\n",
    "    if random_state:\n",
    "        np.random.seed(random_state)\n",
    "\n",
    "    # Split the data\n",
    "    split_index = int(len(X) * (1 - test_size))\n",
    "\n",
    "    X_train = X[:split_index]\n",
    "    X_test = X[split_index:]\n",
    "    y_train = y[:split_index]\n",
    "    y_test = y[split_index:]\n",
    "\n",
    "    return X_train, X_test, y_train, y_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "OF43AvddezUr",
    "outputId": "8213b14a-cb42-4b26-d338-bc201e2b556e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['MSSubClass', 'LotArea', 'OverallCond', 'YearBuilt', 'YearRemodAdd',\n",
       "       'BsmtFinSF2', 'TotalBsmtSF', 'SalePrice', 'MSZoning_C (all)',\n",
       "       'MSZoning_FV', 'MSZoning_RH', 'MSZoning_RL', 'MSZoning_RM',\n",
       "       'LotConfig_Corner', 'LotConfig_CulDSac', 'LotConfig_FR2',\n",
       "       'LotConfig_FR3', 'LotConfig_Inside', 'BldgType_1Fam', 'BldgType_2fmCon',\n",
       "       'BldgType_Duplex', 'BldgType_Twnhs', 'BldgType_TwnhsE',\n",
       "       'Exterior1st_AsbShng', 'Exterior1st_AsphShn', 'Exterior1st_BrkComm',\n",
       "       'Exterior1st_BrkFace', 'Exterior1st_CBlock', 'Exterior1st_CemntBd',\n",
       "       'Exterior1st_HdBoard', 'Exterior1st_ImStucc', 'Exterior1st_MetalSd',\n",
       "       'Exterior1st_Plywood', 'Exterior1st_Stone', 'Exterior1st_Stucco',\n",
       "       'Exterior1st_VinylSd', 'Exterior1st_Wd Sdng', 'Exterior1st_WdShing'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_normalized.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "Wrqjlf9-gJEr"
   },
   "outputs": [],
   "source": [
    "df_normalized = df_normalized.sample(frac = 1)\n",
    "X, y = df_normalized[['MSSubClass', 'LotArea', 'OverallCond', 'YearBuilt', 'YearRemodAdd',\n",
    "       'BsmtFinSF2', 'TotalBsmtSF', 'MSZoning_C (all)',\n",
    "       'MSZoning_FV', 'MSZoning_RH', 'MSZoning_RL', 'MSZoning_RM',\n",
    "       'LotConfig_Corner', 'LotConfig_CulDSac', 'LotConfig_FR2',\n",
    "       'LotConfig_FR3', 'LotConfig_Inside', 'BldgType_1Fam', 'BldgType_2fmCon',\n",
    "       'BldgType_Duplex', 'BldgType_Twnhs', 'BldgType_TwnhsE',\n",
    "       'Exterior1st_AsbShng', 'Exterior1st_AsphShn', 'Exterior1st_BrkComm',\n",
    "       'Exterior1st_BrkFace', 'Exterior1st_CBlock', 'Exterior1st_CemntBd',\n",
    "       'Exterior1st_HdBoard', 'Exterior1st_ImStucc', 'Exterior1st_MetalSd',\n",
    "       'Exterior1st_Plywood', 'Exterior1st_Stone', 'Exterior1st_Stucco',\n",
    "       'Exterior1st_VinylSd', 'Exterior1st_Wd Sdng', 'Exterior1st_WdShing']], df_normalized[['SalePrice']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "collapsed": true,
    "id": "7y3Skg0ZguD4",
    "jupyter": {
     "outputs_hidden": true
    },
    "outputId": "ab42d14d-0d70-4437-8745-037c03d75035"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(      MSSubClass   LotArea  OverallCond  YearBuilt  YearRemodAdd  BsmtFinSF2  \\\n",
       " 2468    0.176471  0.020473        0.250   0.210145      0.000000    0.000000   \n",
       " 1269    0.176471  0.046947        0.500   0.623188      0.133333    0.000000   \n",
       " 2674    0.588235  0.007993        0.500   0.963768      0.933333    0.000000   \n",
       " 1647    0.000000  0.030522        0.500   0.710145      0.333333    0.000000   \n",
       " 530     0.352941  0.041599        0.500   0.840580      0.650000    0.000000   \n",
       " ...          ...       ...          ...        ...           ...         ...   \n",
       " 2710    0.352941  0.060904        0.375   0.739130      0.400000    0.325688   \n",
       " 1127    0.000000  0.062035        0.500   0.956522      0.900000    0.000000   \n",
       " 2519    0.235294  0.051658        0.500   0.913043      0.816667    0.000000   \n",
       " 1505    0.000000  0.042796        0.625   0.688406      0.283333    0.000000   \n",
       " 1603    0.588235  0.011737        0.500   0.956522      0.916667    0.000000   \n",
       " \n",
       "       TotalBsmtSF  MSZoning_C (all)  MSZoning_FV  MSZoning_RH  ...  \\\n",
       " 2468     0.158592               0.0          0.0          0.0  ...   \n",
       " 1269     0.143044               0.0          0.0          0.0  ...   \n",
       " 2674     0.186907               0.0          0.0          0.0  ...   \n",
       " 1647     0.193126               0.0          0.0          0.0  ...   \n",
       " 530      0.239116               0.0          0.0          0.0  ...   \n",
       " ...           ...               ...          ...          ...  ...   \n",
       " 2710     0.286088               0.0          0.0          0.0  ...   \n",
       " 1127     0.250409               0.0          0.0          0.0  ...   \n",
       " 2519     0.139607               0.0          0.0          0.0  ...   \n",
       " 1505     0.299345               0.0          0.0          0.0  ...   \n",
       " 1603     0.260884               0.0          0.0          0.0  ...   \n",
       " \n",
       "       Exterior1st_CemntBd  Exterior1st_HdBoard  Exterior1st_ImStucc  \\\n",
       " 2468                  0.0                  0.0                  0.0   \n",
       " 1269                  0.0                  0.0                  0.0   \n",
       " 2674                  0.0                  0.0                  0.0   \n",
       " 1647                  0.0                  1.0                  0.0   \n",
       " 530                   0.0                  1.0                  0.0   \n",
       " ...                   ...                  ...                  ...   \n",
       " 2710                  0.0                  0.0                  0.0   \n",
       " 1127                  0.0                  0.0                  0.0   \n",
       " 2519                  0.0                  0.0                  0.0   \n",
       " 1505                  0.0                  1.0                  0.0   \n",
       " 1603                  1.0                  0.0                  0.0   \n",
       " \n",
       "       Exterior1st_MetalSd  Exterior1st_Plywood  Exterior1st_Stone  \\\n",
       " 2468                  0.0                  0.0                0.0   \n",
       " 1269                  1.0                  0.0                0.0   \n",
       " 2674                  0.0                  0.0                0.0   \n",
       " 1647                  0.0                  0.0                0.0   \n",
       " 530                   0.0                  0.0                0.0   \n",
       " ...                   ...                  ...                ...   \n",
       " 2710                  0.0                  0.0                0.0   \n",
       " 1127                  0.0                  0.0                0.0   \n",
       " 2519                  0.0                  0.0                0.0   \n",
       " 1505                  0.0                  0.0                0.0   \n",
       " 1603                  0.0                  0.0                0.0   \n",
       " \n",
       "       Exterior1st_Stucco  Exterior1st_VinylSd  Exterior1st_Wd Sdng  \\\n",
       " 2468                 0.0                  0.0                  0.0   \n",
       " 1269                 0.0                  0.0                  0.0   \n",
       " 2674                 0.0                  1.0                  0.0   \n",
       " 1647                 0.0                  0.0                  0.0   \n",
       " 530                  0.0                  0.0                  0.0   \n",
       " ...                  ...                  ...                  ...   \n",
       " 2710                 0.0                  0.0                  0.0   \n",
       " 1127                 0.0                  1.0                  0.0   \n",
       " 2519                 0.0                  1.0                  0.0   \n",
       " 1505                 0.0                  0.0                  0.0   \n",
       " 1603                 0.0                  0.0                  0.0   \n",
       " \n",
       "       Exterior1st_WdShing  \n",
       " 2468                  0.0  \n",
       " 1269                  0.0  \n",
       " 2674                  0.0  \n",
       " 1647                  0.0  \n",
       " 530                   0.0  \n",
       " ...                   ...  \n",
       " 2710                  1.0  \n",
       " 1127                  0.0  \n",
       " 2519                  0.0  \n",
       " 1505                  0.0  \n",
       " 1603                  0.0  \n",
       " \n",
       " [2913 rows x 37 columns],\n",
       "       SalePrice\n",
       " 2468   0.202779\n",
       " 1269   0.151507\n",
       " 2674   0.202779\n",
       " 1647   0.202779\n",
       " 530    0.194556\n",
       " ...         ...\n",
       " 2710   0.202779\n",
       " 1127   0.311207\n",
       " 2519   0.202779\n",
       " 1505   0.202779\n",
       " 1603   0.202779\n",
       " \n",
       " [2913 rows x 1 columns])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X, y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "wiiNfRQRgR1G"
   },
   "outputs": [],
   "source": [
    "# X, y = df_final.loc[:, df_final.columns != 'SalePrice'], df_final[['SalePrice']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "gMCC4I4bgjf2"
   },
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "o5k0PNG_pMGY",
    "outputId": "df0cd3cb-be60-4c92-8148-ffa013e2dec9"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((2330, 37), (583, 37), (2330, 1), (583, 1))"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.shape, X_test.shape, y_train.shape, y_test.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "egogzbyuca1N"
   },
   "source": [
    "# Model Building"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "AaESDSGxccOn"
   },
   "outputs": [],
   "source": [
    "def init_params(n_features):\n",
    "    W = np.zeros((n_features, 1))\n",
    "    # W = np.random.randn(n_features, 1) * 0.01\n",
    "    b = 0\n",
    "    return W, b\n",
    "\n",
    "def compute(W, b, X):\n",
    "    return np.dot(X, W)+b\n",
    "\n",
    "def loss(y_true, y_pred):\n",
    "\n",
    "    # return np.mean((y_true - y_pred) ** 3)\n",
    "    # return np.abs(y_true - y_pred)\n",
    "    return np.mean((y_true - y_pred) ** 2)\n",
    "\n",
    "def update_params(W, b, learning_rate, dw, db):\n",
    "    W = W - learning_rate * dw\n",
    "    b = b - learning_rate * db\n",
    "    return W, b\n",
    "\n",
    "def train(X, y, learning_rate, epochs):\n",
    "    W, b = init_params(X.shape[1])\n",
    "    for i in range(epochs):\n",
    "        y_pred = compute(W, b, X)\n",
    "        loss_value = loss(y, y_pred)\n",
    "        val=np.dot(X.T, (y_pred - y))\n",
    "        dw = 2 * (val) / X.shape[0]\n",
    "        db = 2 * np.sum(y_pred - y) / X.shape[0]\n",
    "        #MAE\n",
    "        # dw = np.dot(X.T, np.sign(y_pred - y)) / X.shape[0]\n",
    "        # db = np.mean(np.sign(y_pred - y))/X.shape[0]\n",
    "\n",
    "        W, b = update_params(W, b, learning_rate, dw, db)\n",
    "        if i % 100 == 0:\n",
    "            print(f\"Epoch {i}, Loss: {loss_value}\")\n",
    "    return W, b\n",
    "\n",
    "def predict(X, W, b):\n",
    "    return compute(W, b, X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "collapsed": true,
    "id": "_noV0GqEung6",
    "jupyter": {
     "outputs_hidden": true
    },
    "outputId": "101cc185-75f9-472a-e721-75b6228f0a1e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 0, Loss: 0.013475994850334714\n",
      "Epoch 100, Loss: 0.010112863902498263\n",
      "Epoch 200, Loss: 0.0077710043254110425\n",
      "Epoch 300, Loss: 0.006124139651526763\n",
      "Epoch 400, Loss: 0.004952419738792096\n",
      "Epoch 500, Loss: 0.004107425447527552\n",
      "Epoch 600, Loss: 0.00348871533029027\n",
      "Epoch 700, Loss: 0.0030281034489645126\n",
      "Epoch 800, Loss: 0.0026791161540046697\n",
      "Epoch 900, Loss: 0.002409918587932048\n",
      "Epoch 1000, Loss: 0.00219856615978934\n",
      "Epoch 1100, Loss: 0.0020298142591249884\n",
      "Epoch 1200, Loss: 0.001892972633088114\n",
      "Epoch 1300, Loss: 0.001780460386887536\n",
      "Epoch 1400, Loss: 0.0016868311087060443\n",
      "Epoch 1500, Loss: 0.0016081136627608367\n",
      "Epoch 1600, Loss: 0.0015413651255459615\n",
      "Epoch 1700, Loss: 0.0014843664553348648\n",
      "Epoch 1800, Loss: 0.001435414338462741\n",
      "Epoch 1900, Loss: 0.001393177967440798\n",
      "Epoch 2000, Loss: 0.0013565997664611684\n",
      "Epoch 2100, Loss: 0.0013248259573537863\n",
      "Epoch 2200, Loss: 0.001297157470558068\n",
      "Epoch 2300, Loss: 0.0012730147991938858\n",
      "Epoch 2400, Loss: 0.0012519124707982042\n",
      "Epoch 2500, Loss: 0.0012334402062217607\n",
      "Epoch 2600, Loss: 0.0012172487732475903\n",
      "Epoch 2700, Loss: 0.0012030391742015342\n",
      "Epoch 2800, Loss: 0.0011905542329937924\n",
      "Epoch 2900, Loss: 0.001179571935209004\n",
      "Epoch 3000, Loss: 0.0011699000703183258\n",
      "Epoch 3100, Loss: 0.0011613718581660176\n",
      "Epoch 3200, Loss: 0.0011538423329357215\n",
      "Epoch 3300, Loss: 0.0011471853204830548\n",
      "Epoch 3400, Loss: 0.0011412908884005691\n",
      "Epoch 3500, Loss: 0.0011360631786213778\n",
      "Epoch 3600, Loss: 0.0011314185539164387\n",
      "Epoch 3700, Loss: 0.0011272840050934913\n",
      "Epoch 3800, Loss: 0.0011235957769516957\n",
      "Epoch 3900, Loss: 0.0011202981793648865\n",
      "Epoch 4000, Loss: 0.001117342556127892\n",
      "Epoch 4100, Loss: 0.0011146863889989725\n",
      "Epoch 4200, Loss: 0.001112292518115407\n",
      "Epoch 4300, Loss: 0.0011101284629309617\n",
      "Epoch 4400, Loss: 0.0011081658302209546\n",
      "Epoch 4500, Loss: 0.0011063797976618168\n",
      "Epoch 4600, Loss: 0.001104748663117037\n",
      "Epoch 4700, Loss: 0.0011032534511223272\n",
      "Epoch 4800, Loss: 0.0011018775692126983\n",
      "Epoch 4900, Loss: 0.0011006065077128553\n",
      "Epoch 5000, Loss: 0.0010994275774502873\n",
      "Epoch 5100, Loss: 0.0010983296805711037\n",
      "Epoch 5200, Loss: 0.001097303110260924\n",
      "Epoch 5300, Loss: 0.0010963393757118238\n",
      "Epoch 5400, Loss: 0.0010954310491437887\n",
      "Epoch 5500, Loss: 0.0010945716320954711\n",
      "Epoch 5600, Loss: 0.001093755438552666\n",
      "Epoch 5700, Loss: 0.0010929774927910686\n",
      "Epoch 5800, Loss: 0.0010922334400785187\n",
      "Epoch 5900, Loss: 0.0010915194686163434\n",
      "Epoch 6000, Loss: 0.0010908322413039778\n",
      "Epoch 6100, Loss: 0.001090168836089712\n",
      "Epoch 6200, Loss: 0.0010895266938263691\n",
      "Epoch 6300, Loss: 0.0010889035726870718\n",
      "Epoch 6400, Loss: 0.00108829750831525\n",
      "Epoch 6500, Loss: 0.0010877067789871327\n",
      "Epoch 6600, Loss: 0.0010871298751558573\n",
      "Epoch 6700, Loss: 0.0010865654728257634\n",
      "Epoch 6800, Loss: 0.0010860124102749114\n",
      "Epoch 6900, Loss: 0.0010854696677045073\n",
      "Epoch 7000, Loss: 0.001084936349446968\n",
      "Epoch 7100, Loss: 0.0010844116684107412\n",
      "Epoch 7200, Loss: 0.0010838949324804728\n",
      "Epoch 7300, Loss: 0.001083385532626564\n",
      "Epoch 7400, Loss: 0.0010828829325090917\n",
      "Epoch 7500, Loss: 0.0010823866593881464\n",
      "Epoch 7600, Loss: 0.001081896296176271\n",
      "Epoch 7700, Loss: 0.001081411474489376\n",
      "Epoch 7800, Loss: 0.0010809318685705742\n",
      "Epoch 7900, Loss: 0.001080457189977157\n",
      "Epoch 8000, Loss: 0.001079987182934783\n",
      "Epoch 8100, Loss: 0.001079521620274965\n",
      "Epoch 8200, Loss: 0.0010790602998825545\n",
      "Epoch 8300, Loss: 0.0010786030415890929\n",
      "Epoch 8400, Loss: 0.001078149684455985\n",
      "Epoch 8500, Loss: 0.0010777000843985223\n",
      "Epoch 8600, Loss: 0.001077254112107883\n",
      "Epoch 8700, Loss: 0.0010768116512336967\n",
      "Epoch 8800, Loss: 0.0010763725967944066\n",
      "Epoch 8900, Loss: 0.0010759368537868273\n",
      "Epoch 9000, Loss: 0.0010755043359698515\n",
      "Epoch 9100, Loss: 0.0010750749648004532\n",
      "Epoch 9200, Loss: 0.0010746486685028333\n",
      "Epoch 9300, Loss: 0.0010742253812540017\n",
      "Epoch 9400, Loss: 0.0010738050424711667\n",
      "Epoch 9500, Loss: 0.0010733875961881495\n",
      "Epoch 9600, Loss: 0.0010729729905096495\n",
      "Epoch 9700, Loss: 0.0010725611771335807\n",
      "Epoch 9800, Loss: 0.0010721521109329397\n",
      "Epoch 9900, Loss: 0.0010717457495897328\n"
     ]
    }
   ],
   "source": [
    "W_, b_ = train(X_train.values, y_train.values, 0.0001, 10000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "13I1Nl0ousyP"
   },
   "outputs": [],
   "source": [
    "vals = predict(X_test.values, W_, b_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "-5KLnykg2N4u",
    "outputId": "4c128c3b-1115-411f-db09-230cfc6344cd"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "615394293499.5585"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "reg_vals = vals * (label_max - label_min) + label_min\n",
    "y_reg_test = y_test * (label_max - label_min) + label_min\n",
    "\n",
    "loss_value = loss(y_reg_test.values, reg_vals)/len(reg_vals)\n",
    "loss_value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "K2UP9ZdZ5jDQ"
   },
   "source": [
    "### The above number makes no sense. But the below one does. Why?\n",
    "What is the practical meaning of loss?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "MDOOkrmZ2OWx",
    "outputId": "4c9d775f-f526-4711-f850-8c3fbe78dd72"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32554.313600997815"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "difference = np.sum(abs(y_reg_test.values - reg_vals))/len(reg_vals)\n",
    "difference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "2BIaJ4oN2Q78"
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "ai511",
   "language": "python",
   "name": "ai511"
  },
  "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.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
