{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "UQ9Cw8pMSu8u" }, "source": [ "# Multivariate Linear Regression in Python with Numpy\n", "\n", "Welcome to one more tutorial! In the last post (see here) we saw how to do a linear regression on Python using barely no library but native functions (except for visualization).\n", "\n", "In this exercise, we will see how to implement a linear regression with multiple inputs using Numpy. We will also use the Gradient Descent algorithm to train our model.\n", "\n", "The first step is to import all the necessary libraries. The ones we will use are:\n", "- Numpy - for numerical calculations;\n", "- Pandas - to read csv and data processing;\n", "- Matplotlib - for visualization" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "GpQjC6oYzgGo" }, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "tHhIs2BNUbq_" }, "source": [ "Now import the data that we want to work on. This data (hypothetical) consists in the following information from real state properties:\n", "- Size (the size of the housing in square meters);\n", "- Nr Bedrooms -> the number of bedrooms;\n", "- Nr Bathrooms -> the number of bathrooms\n", "- Price -> The price of the house, in terms of thousands of dollars (or any other currency since the data is hypothetical)\n", "\n", "**Hypothesis**\n", "The price is linearly correlated with the size, nr of bedrooms and nr of bathrooms of a housing.\n", "\n", "We will check validity of the above hypothesis through linear regression.\n", "\n", " Pandas function `read_csv()` is used to read the csv file 'housingprices.csv' and place it as a dataframe." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 195 }, "colab_type": "code", "id": "pibqK2ndzqUv", "outputId": "df5e4e94-dea9-44ac-9282-05e83397c299" }, "outputs": [ { "data": { "text/html": [ "
\n", " | Size | \n", "Nr Bedrooms | \n", "Nr Bathrooms | \n", "Price | \n", "
---|---|---|---|---|
0 | \n", "84 | \n", "1 | \n", "1 | \n", "43.747 | \n", "
1 | \n", "45 | \n", "4 | \n", "1 | \n", "30.100 | \n", "
2 | \n", "73 | \n", "1 | \n", "3 | \n", "39.481 | \n", "
3 | \n", "34 | \n", "2 | \n", "3 | \n", "23.908 | \n", "
4 | \n", "31 | \n", "4 | \n", "3 | \n", "24.144 | \n", "