Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save firmai/fa9e1ab2b85bd2bd66ed16c3650cee4d to your computer and use it in GitHub Desktop.
Save firmai/fa9e1ab2b85bd2bd66ed16c3650cee4d to your computer and use it in GitHub Desktop.
Normal Equations and Analytical Solution for Linear Regression Models.ipynb
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyOJUIlseDL7+3VQDRL/K0Di",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/firmai/fa9e1ab2b85bd2bd66ed16c3650cee4d/normal-equations-and-analytical-solution-for-linear-regression-models.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "uu9hYdyAHUNl"
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"beta_zero = np.array([1, 1, 1, 1, 1, 1])\n",
"sqr_feet = np.array([ 1000, 1500, 2000, 3000, 3800, 4500])\n",
"bedrooms = np.array([ 1, 2, 2, 3, 4, 5])\n",
"bathrooms = np.array([ 1, 1, 1, 2, 2, 2])\n",
"house_age = np.array([ 20, 24, 12, 8, 13, 14])\n",
"\n",
"X = np.array([beta_zero, sqr_feet, bedrooms, bathrooms, house_age]).T\n",
"y = np.array([ 100, 230, 150, 300, 390, 300]).reshape(X.shape[0],1)\n",
"B = np.linalg.inv(X.T @ X) @ (X.T @ y)\n",
"B = np.round(B,2)\n",
"preds = X @ B\n",
"preds = np.round(preds,2)"
]
},
{
"cell_type": "code",
"source": [
"## Print Statements\n",
"feats = [\"Beta Zero\",\"Sqr Feet\",\"Bedrooms\",\"Bathrooms\",\"House Age\"]\n",
"print(pd.DataFrame(np.c_[X, y, preds], columns=[\"Beta Zero\",\"Sqr Feet\",\"Bedrooms\",\"Bathrooms\",\"House Age\",\"Target\",\"Predictions\"]))\n",
"print(\"\\nThetas: {} \\n\".format(B.T))\n",
"formula = \"Prediction =\" + \"\".join([\"{} x {} +\".format(\" Beta_{}\".format(str(en)), feat) for en, feat in enumerate(feats)])[:-2] \n",
"print(\"Formula: \" + formula +\" \\n\" )\n",
"print(\"\\n\".join([\"{} = \".format(pred[0]) + \"\".join([\"{} x {} + \".format(the[0], feat) for the, feat in zip(B, X[en,:])])[:-3] for en, pred in enumerate(preds)]))"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "bcUJcOHcHdZa",
"outputId": "cc5f5b9c-f5fe-42a3-eae5-3311b0a52573"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
" Beta Zero Sqr Feet Bedrooms Bathrooms House Age Target Predictions\n",
"0 1.0 1000.0 1.0 1.0 20.0 100.0 139.30\n",
"1 1.0 1500.0 2.0 1.0 24.0 230.0 197.15\n",
"2 1.0 2000.0 2.0 1.0 12.0 150.0 128.79\n",
"3 1.0 3000.0 3.0 2.0 8.0 300.0 302.10\n",
"4 1.0 3800.0 4.0 2.0 13.0 390.0 310.98\n",
"5 1.0 4500.0 5.0 2.0 14.0 300.0 339.74\n",
"\n",
"Thetas: [[-1.0730e+01 -1.6000e-01 1.4173e+02 1.8770e+02 -9.7000e-01]] \n",
"\n",
"Formula: Prediction = Beta_0 x Beta Zero + Beta_1 x Sqr Feet + Beta_2 x Bedrooms + Beta_3 x Bathrooms + Beta_4 x House Age \n",
"\n",
"139.3 = -10.73 x 1 + -0.16 x 1000 + 141.73 x 1 + 187.7 x 1 + -0.97 x 20\n",
"197.15 = -10.73 x 1 + -0.16 x 1500 + 141.73 x 2 + 187.7 x 1 + -0.97 x 24\n",
"128.79 = -10.73 x 1 + -0.16 x 2000 + 141.73 x 2 + 187.7 x 1 + -0.97 x 12\n",
"302.1 = -10.73 x 1 + -0.16 x 3000 + 141.73 x 3 + 187.7 x 2 + -0.97 x 8\n",
"310.98 = -10.73 x 1 + -0.16 x 3800 + 141.73 x 4 + 187.7 x 2 + -0.97 x 13\n",
"339.74 = -10.73 x 1 + -0.16 x 4500 + 141.73 x 5 + 187.7 x 2 + -0.97 x 14\n"
]
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "VF4u8XnMHfsK"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment