  {"id":14,"date":"2023-07-11T12:30:00","date_gmt":"2023-07-11T11:30:00","guid":{"rendered":"https:\/\/machinelearning.com.ng\/?p=14"},"modified":"2023-07-11T09:33:51","modified_gmt":"2023-07-11T08:33:51","slug":"building-a-multiple-linear-regression-model-to-predict-real-estate-price","status":"publish","type":"post","link":"https:\/\/machinelearning.com.ng\/?p=14","title":{"rendered":"Building a Multiple Linear Regression Model to Predict Real Estate Price"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code># Import the Python Libraries\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n     \n\ndata = pd.read_csv(\"Real estate.csv\")\n  \ndata.info()\n     \n&lt;class 'pandas.core.frame.DataFrame'>\nRangeIndex: 414 entries, 0 to 413\nData columns (total 8 columns):\n #   Column                                  Non-Null Count  Dtype  \n---  ------                                  --------------  -----  \n 0   No                                      414 non-null    int64  \n 1   X1 transaction date                     414 non-null    float64\n 2   X2 house age                            414 non-null    float64\n 3   X3 distance to the nearest MRT station  414 non-null    float64\n 4   X4 number of convenience stores         414 non-null    int64  \n 5   X5 latitude                             414 non-null    float64\n 6   X6 longitude                            414 non-null    float64\n 7   Y house price of unit area              414 non-null    float64\ndtypes: float64(6), int64(2)\nmemory usage: 26.0 KB\n\n# Viewing the top half of the data\n\ndata.head()\n     \nNo\tX1 transaction date\tX2 house age\tX3 distance to the nearest MRT station\tX4 number of convenience stores\tX5 latitude\tX6 longitude\tY house price of unit area\n0\t1\t2012.917\t32.0\t84.87882\t10\t24.98298\t121.54024\t37.9\n1\t2\t2012.917\t19.5\t306.59470\t9\t24.98034\t121.53951\t42.2\n2\t3\t2013.583\t13.3\t561.98450\t5\t24.98746\t121.54391\t47.3\n\n#3\t4\t2013.500\t13.3\t561.98450\t5\t24.98746\t121.54391\t54.8\n4\t5\t2012.833\t5.0\t390.56840\t5\t24.97937\t121.54245\t43.1\n\n# Viewing the lower half of the data\ndata.tail()\n     \nNo\tX1 transaction date\tX2 house age\tX3 distance to the nearest MRT station\tX4 number of convenience stores\tX5 latitude\tX6 longitude\tY house price of unit area\n409\t410\t2013.000\t13.7\t4082.01500\t0\t24.94155\t121.50381\t15.4\n410\t411\t2012.667\t5.6\t90.45606\t9\t24.97433\t121.54310\t50.0\n411\t412\t2013.250\t18.8\t390.96960\t7\t24.97923\t121.53986\t40.6\n412\t413\t2013.000\t8.1\t104.81010\t5\t24.96674\t121.54067\t52.5\n413\t414\t2013.500\t6.5\t90.45606\t9\t24.97433\t121.54310\t63.9\n\n# To know the relationship\/correlation between the dependent variables and the independent variables; this was what led to the exclusion of the transaction date column while building the model.\n\ndata.corr()\n     \nNo\tX1 transaction date\tX2 house age\tX3 distance to the nearest MRT station\tX4 number of convenience stores\tX5 latitude\tX6 longitude\tY house price of unit area\nNo\t1.000000\t-0.048658\t-0.032808\t-0.013573\t-0.012699\t-0.010110\t-0.011059\t-0.028587\nX1 transaction date\t-0.048658\t1.000000\t0.017549\t0.060880\t0.009635\t0.035058\t-0.041082\t0.087491\nX2 house age\t-0.032808\t0.017549\t1.000000\t0.025622\t0.049593\t0.054420\t-0.048520\t-0.210567\nX3 distance to the nearest MRT station\t-0.013573\t0.060880\t0.025622\t1.000000\t-0.602519\t-0.591067\t-0.806317\t-0.673613\nX4 number of convenience stores\t-0.012699\t0.009635\t0.049593\t-0.602519\t1.000000\t0.444143\t0.449099\t0.571005\nX5 latitude\t-0.010110\t0.035058\t0.054420\t-0.591067\t0.444143\t1.000000\t0.412924\t0.546307\nX6 longitude\t-0.011059\t-0.041082\t-0.048520\t-0.806317\t0.449099\t0.412924\t1.000000\t0.523287\nY house price of unit area\t-0.028587\t0.087491\t-0.210567\t-0.673613\t0.571005\t0.546307\t0.523287\t1.000000\n\nX = data.iloc&#91;:, 2:-1].values\ny = data.iloc&#91;:, -1].values\n     \n\nprint(X)\n     \n&#91;&#91; 32.       84.87882  10.       24.98298 121.54024]\n &#91; 19.5     306.5947    9.       24.98034 121.53951]\n &#91; 13.3     561.9845    5.       24.98746 121.54391]\n ...\n &#91; 18.8     390.9696    7.       24.97923 121.53986]\n &#91;  8.1     104.8101    5.       24.96674 121.54067]\n &#91;  6.5      90.45606   9.       24.97433 121.5431 ]]\n\nprint(y)\n     \n&#91; 37.9  42.2  47.3  54.8  43.1  32.1  40.3  46.7  18.8  22.1  41.4  58.1\n  39.3  23.8  34.3  50.5  70.1  37.4  42.3  47.7  29.3  51.6  24.6  47.9\n  38.8  27.   56.2  33.6  47.   57.1  22.1  25.   34.2  49.3  55.1  27.3\n  22.9  25.3  47.7  46.2  15.9  18.2  34.7  34.1  53.9  38.3  42.   61.5\n  13.4  13.2  44.2  20.7  27.   38.9  51.7  13.7  41.9  53.5  22.6  42.4\n  21.3  63.2  27.7  55.   25.3  44.3  50.7  56.8  36.2  42.   59.   40.8\n  36.3  20.   54.4  29.5  36.8  25.6  29.8  26.5  40.3  36.8  48.1  17.7\n  43.7  50.8  27.   18.3  48.   25.3  45.4  43.2  21.8  16.1  41.   51.8\n  59.5  34.6  51.   62.2  38.2  32.9  54.4  45.7  30.5  71.   47.1  26.6\n  34.1  28.4  51.6  39.4  23.1   7.6  53.3  46.4  12.2  13.   30.6  59.6\n  31.3  48.   32.5  45.5  57.4  48.6  62.9  55.   60.7  41.   37.5  30.7\n  37.5  39.5  42.2  20.8  46.8  47.4  43.5  42.5  51.4  28.9  37.5  40.1\n  28.4  45.5  52.2  43.2  45.1  39.7  48.5  44.7  28.9  40.9  20.7  15.6\n  18.3  35.6  39.4  37.4  57.8  39.6  11.6  55.5  55.2  30.6  73.6  43.4\n  37.4  23.5  14.4  58.8  58.1  35.1  45.2  36.5  19.2  42.   36.7  42.6\n  15.5  55.9  23.6  18.8  21.8  21.5  25.7  22.   44.3  20.5  42.3  37.8\n  42.7  49.3  29.3  34.6  36.6  48.2  39.1  31.6  25.5  45.9  31.5  46.1\n  26.6  21.4  44.   34.2  26.2  40.9  52.2  43.5  31.1  58.   20.9  48.1\n  39.7  40.8  43.8  40.2  78.3  38.5  48.5  42.3  46.   49.   12.8  40.2\n  46.6  19.   33.4  14.7  17.4  32.4  23.9  39.3  61.9  39.   40.6  29.7\n  28.8  41.4  33.4  48.2  21.7  40.8  40.6  23.1  22.3  15.   30.   13.8\n  52.7  25.9  51.8  17.4  26.5  43.9  63.3  28.8  30.7  24.4  53.   31.7\n  40.6  38.1  23.7  41.1  40.1  23.  117.5  26.5  40.5  29.3  41.   49.7\n  34.   27.7  44.   31.1  45.4  44.8  25.6  23.5  34.4  55.3  56.3  32.9\n  51.   44.5  37.   54.4  24.5  42.5  38.1  21.8  34.1  28.5  16.7  46.1\n  36.9  35.7  23.2  38.4  29.4  55.   50.2  24.7  53.   19.1  24.7  42.2\n  78.   42.8  41.6  27.3  42.   37.5  49.8  26.9  18.6  37.7  33.1  42.5\n  31.3  38.1  62.1  36.7  23.6  19.2  12.8  15.6  39.6  38.4  22.8  36.5\n  35.6  30.9  36.3  50.4  42.9  37.   53.5  46.6  41.2  37.9  30.8  11.2\n  53.7  47.   42.3  28.6  25.7  31.3  30.1  60.7  45.3  44.9  45.1  24.7\n  47.1  63.3  40.   48.   33.1  29.5  24.8  20.9  43.1  22.8  42.1  51.7\n  41.5  52.2  49.5  23.8  30.5  56.8  37.4  69.7  53.3  47.3  29.3  40.3\n  12.9  46.6  55.3  25.6  27.3  67.7  38.6  31.3  35.3  40.3  24.7  42.5\n  31.9  32.2  23.   37.3  35.5  27.7  28.5  39.7  41.2  37.2  40.5  22.3\n  28.1  15.4  50.   40.6  52.5  63.9]\n\n# Dividing the data into the train and test set\n\nfrom sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)\n     \n\nprint(X_train&#91;:, 0])\n     \n&#91;18.2 13.6  3.8  4.  41.3  3.4 35.4  6.2 33.2 10.8 20.9 13.2 14.1 13.2\n 10.4 23.  28.6 13.8 12.8 13.2 35.3 27.5 17.7 16.4 12.5 37.3  9.1  3.6\n 17.2  3.7  1.1 13.3 13.3 13.   3.5  6.5 33.5 25.6 30.  31.7  4.1  3.8\n  8.9  4.   9.9  0.  13.2  4.3  0.  10.  33.3 31.3  9.9 17.9 17.   0.\n 34.4 32.   0.  16.1 32.4  4.1  6.6  3.1 35.8 34.5  8.5 28.2 16.4 11.5\n  6.2  2.1 13.1 36.1  3.5  4.1 31.   3.6 30.6  1.9 19.2 33.5 12.5 10.3\n  3.2 26.8 17.5 15.4 34.8  8.1 30.2 13.3 18.9 16.2 16.9  1.5 19.8 11.6\n 15.   2.7 12.2 18.8 12.4 31.4  1.1  8.4 18.  42.7 32.6 15.1 31.7  1.1\n  6.4 41.4  5.7  7.8 19.2 29.6 14.4 31.7 16.4 37.9 30.1 24.2 34.6 34.7\n 12.6 16.5  9.  21.2 13.9 15.6  2.6 16.1 41.3 21.7 39.2  1.  37.7  7.8\n 18.   8.  18.5 16.4 17.1 36.6 22.2 27.6  3.9 35.9 14.8 33.  29.3 29.1\n 10.5 19.  18.2 13.6 39.6 14.1 12.8 33.4 30.9 17.6 21.7 10.3  1.5 16.2\n\n 28.  33.6 15.9 16.2 38.3 11.6 28.4 30.6  0.  25.3  0.  24.   5.6 16.1\n 13.9 15.2 34.  13.5 33.6  0.  37.2 29.6 30.3 11.  40.9  9.7 16.2 16.4\n 38.3 12.   1.7 34.8 17.6  6.4  5.2 11.   3.5 38.   7.1 14.7 33.9  4.5\n 14.2 12.3 17.4 20.5  3.9  5.2 34.9 17.3 13.3 32.1 15.9 30.4 17.7 16.9\n 17.   4.9 14.7 16.5 35.3  6.8 32.8 12.7 16.9 18.1 13.6 17.4 17.8 16.6\n 11.4  0.  25.3 20.2 13.8  2.6 38.6 15.2 15.7 16.3 32.7  6.5  2.  16.5\n 29.3 22.8 17.5 35.7 34.9  4.   5.1 16.4  3.1 35.9 34.4  2.3 13.3  8.\n 29.4 12.7 25.9 13.6 20.6  0.  37.8 32.6 34.8 13.3 19.5  8.3 14.7 15.6\n 34.6 20.3  5.1  1.8 18.4 13.7 19.2 30.4 21.7 30.7  5.9  0.   1.1 19.1\n 13.1  4.7 32.8 13.  35.5 38.5 11.9 27.3 18.  15.6 16.9 31.5 32.5 37.1\n 12.9 12. ]\n\n# Building the model\n\nfrom sklearn.linear_model import LinearRegression\nregressor = LinearRegression()\nregressor.fit(X_train, y_train)\n     \nLinearRegression()\n\ny_pred = regressor.predict(X_test)\n\nprint(f\"Price = {round(regressor.intercept_, 2)} + {round(regressor.coef_&#91;0], 2)}house age + {round(regressor.coef_&#91;1], 4)}mri station + {round(regressor.coef_&#91;2], 2)}convenience store + {round(regressor.coef_&#91;3], 2)}latitude + {round(regressor.coef_&#91;4], 2)}longitude\" )\n\n# The Multiple Linear Regression Model\n    \nPrice = -433.79 + -0.24house age + -0.0047mri station + 1.09convenience store + 224.0latitude + -42.1longitude\n\n# Code for visualizing the data - mostly the relationships between the independent variables and the dependent variable.\n\nplt.suptitle(\"Model Showing the factors that affect Real Estate Price\")\n\nplt.subplot(3,2,1)\nplt.scatter(X_train&#91;:, 0], y_train, color=\"r\", s=5, alpha=0.5)\nplt.xlabel(\"House Age\")\nplt.ylabel(\"Price\")\n\nplt.subplot(3,2,2)\nplt.scatter(X_train&#91;:, 1], y_train, color=\"b\", s=5, alpha=0.5)\nplt.xlabel(\"Distance to MRI Station\")\nplt.ylabel(\"Price\")\n\nplt.subplot(3,2,3)\nplt.scatter(X_train&#91;:, 2], y_train, color=\"g\", s=5, alpha=0.5)\nplt.xlabel(\"Number of Convenience stores\")\nplt.ylabel(\"Price\")\n\nplt.subplot(3,2,4)\nplt.scatter(X_train&#91;:, 3], y_train, color=\"orange\", s=5, alpha=0.5)\nplt.xlabel(\"latitude\")\nplt.ylabel(\"Price\")\n\nplt.subplot(3,2,5)\nplt.scatter(X_train&#91;:, 4], y_train, color=\"purple\", s=5, alpha=0.5)\nplt.xlabel(\"longitude\")\nplt.ylabel(\"Price\")\n\nplt.subplots_adjust(left=0.1,\n                    bottom=0.1,\n                    right=1.2,\n                    wspace=0.4,\n                    hspace=0.6)\n\nplt.show()\n     <\/code><\/pre>\n\n\n\n<div style=\"height:10px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/machinelearning.com.ng\/wp-content\/uploads\/2023\/07\/subplots2.png\" alt=\"\" class=\"wp-image-17\" width=\"682\" height=\"376\" srcset=\"https:\/\/machinelearning.com.ng\/wp-content\/uploads\/2023\/07\/subplots2.png 808w, https:\/\/machinelearning.com.ng\/wp-content\/uploads\/2023\/07\/subplots2-300x165.png 300w, https:\/\/machinelearning.com.ng\/wp-content\/uploads\/2023\/07\/subplots2-768x423.png 768w\" sizes=\"(max-width: 682px) 100vw, 682px\" \/><figcaption class=\"wp-element-caption\">Subplots showing the relationship between the independent variables and the dependent variable (Real Estate Price).<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Building a Linear Regression Model to Predict Real Estate Price.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[8,7,9],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/machinelearning.com.ng\/index.php?rest_route=\/wp\/v2\/posts\/14"}],"collection":[{"href":"https:\/\/machinelearning.com.ng\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/machinelearning.com.ng\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/machinelearning.com.ng\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/machinelearning.com.ng\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14"}],"version-history":[{"count":4,"href":"https:\/\/machinelearning.com.ng\/index.php?rest_route=\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":20,"href":"https:\/\/machinelearning.com.ng\/index.php?rest_route=\/wp\/v2\/posts\/14\/revisions\/20"}],"wp:attachment":[{"href":"https:\/\/machinelearning.com.ng\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/machinelearning.com.ng\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/machinelearning.com.ng\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}