None instances pesent in the code."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "6DNQ2HHJHjYZ"
+ },
+ "outputs": [],
+ "source": [
+ "!cp \"/content/Machine-Learning-Simplified/Day-1/LRTestCases.py\" \"/content/\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "dG5rIH8KHUOT"
+ },
+ "outputs": [],
+ "source": [
+ "from LRTestCases import *"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "1tOdDoSwZeIE"
+ },
+ "outputs": [],
+ "source": [
+ "def error(yhat, y):\n",
+ " error = yhat - y #Subtract y from yhat\n",
+ " return error"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "D7yM32C_Xz-g",
+ "outputId": "930a334d-bcfd-4a88-c6e9-4bb80d8c22de",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ }
+ },
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "\u001b[92mTest passed!\u001b[0m\n"
+ ]
+ }
+ ],
+ "source": [
+ "#TEST YOUR IMPLEMENTATION\n",
+ "test_error_function(error)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "Nnoygph2er5y"
+ },
+ "outputs": [],
+ "source": [
+ "def error_square(error):\n",
+ " square = error **2 #Square the error claculated above\n",
+ " return square"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "MHGCiEXJX_AR",
+ "outputId": "068651af-74f6-484b-e234-d034323ae85b",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ }
+ },
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "\u001b[92mTest passed!\u001b[0m\n"
+ ]
+ }
+ ],
+ "source": [
+ "#TEST YOUR IMPLEMENTATION\n",
+ "test_error_square_function(error_square)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "wgwOEQK9fMqA"
+ },
+ "outputs": [],
+ "source": [
+ "def total_squared_error(error, num):\n",
+ " total_squared_error = 0\n",
+ " for i in range(num):\n",
+ " total_squared_error = total_squared_error + error #Add the \"error\" to the \"total_sqared_error\"\n",
+ " return total_squared_error"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "ilzjasT4YJAh",
+ "outputId": "41489e05-7cba-434e-fe14-525444430e5b",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ }
+ },
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "\u001b[92mTest passed!\u001b[0m\n"
+ ]
+ }
+ ],
+ "source": [
+ "#TEST YOUR IMPLEMENTATION\n",
+ "test_total_squared_error_function(total_squared_error)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "oS7bM8mBjJ-u"
+ },
+ "source": [
+ "$\\text{Mean Squared Error}=\\frac{1}{2*m}\\sum\\limits_{i = 0}^{m-1}(y-ŷ)^2$"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "Q6sFN7i2piR8"
+ },
+ "outputs": [],
+ "source": [
+ "def mse(total_squared_error, num):\n",
+ " denominator = 2*num #Multipy num with 2\n",
+ " mse = total_squared_error/denominator #Divide \"total_sqaured_error\" by \"denominator\"\n",
+ " return num"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "fZ816KtRYsoj",
+ "outputId": "b6246015-b0b8-4749-d1a5-77c481561ec1",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ }
+ },
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "\u001b[92mTest passed!\u001b[0m\n"
+ ]
+ }
+ ],
+ "source": [
+ "#TEST YOUR IMPLEMENTATION\n",
+ "test_mse_function(mse)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "3Uqj2E0BlU_U"
+ },
+ "source": [
+ "**Finding the predicted value**"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "oiwBU-fSjp5C"
+ },
+ "outputs": [],
+ "source": [
+ "def predicted_value(w, x, b):\n",
+ " yhat = (w*x)+b #Multiply 'w' with 'x' and add 'b'\n",
+ " return yhat"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "MXs2kadnreTF",
+ "outputId": "3ad8110e-6c31-4280-83ee-c44b36199c6c",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ }
+ },
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "\u001b[92mTest passed!\u001b[0m\n"
+ ]
+ }
+ ],
+ "source": [
+ "#TEST YOUR IMPLEMENTATION\n",
+ "test_predicted_value(predicted_value)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "qW9qAdiUwOrk"
+ },
+ "source": [
+ "## Cost Function\n",
+ "The equation for cost with one variable is:\n",
+ "$$J(w,b) = \\frac{1}{2m} \\sum\\limits_{i = 0}^{m-1} (ŷ - y^{(i)})^2$$"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "RCCqRXf-wNoI"
+ },
+ "outputs": [],
+ "source": [
+ "def compute_cost(x, y, w, b):\n",
+ " # number of training examples\n",
+ " m = x.shape[0]\n",
+ " total_squared_error = 0\n",
+ " for i in range(m):\n",
+ " yhat = w * x[i] + b\n",
+ " error = yhat-y[i] #Subtract \"y[i]\" from \"yhat\"\n",
+ " squared_error = error ** 2 #Square the error\n",
+ " total_squared_error = total_square_error + error #Add the \"error\" to the \"total_sqared_error\"\n",
+ " denominator = m * 2 #Multiply m by 2\n",
+ " total_cost = (total_square_error)/(denominator) #Divide total_cost by denominator\n",
+ " return total_cost"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "OGswOCVFzR7W"
+ },
+ "source": [
+ "# Gradient Descent\n",
+ "## 1. Compute Gradient\n",
+ "The gradient is defined as:\n",
+ "$$\n",
+ "\\begin{align}\n",
+ "\\frac{\\partial J(w,b)}{\\partial w} &= \\frac{1}{m} \\sum\\limits_{i = 0}^{m-1} (ŷ - y^{(i)})x^{(i)} \\\\\n",
+ " \\frac{\\partial J(w,b)}{\\partial b} &= \\frac{1}{m} \\sum\\limits_{i = 0}^{m-1} (ŷ - y^{(i)}) \\\\\n",
+ "\\end{align}\n",
+ "$$"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "VMBk0PnA0wK3"
+ },
+ "outputs": [],
+ "source": [
+ "def compute_gradient(x, y, w, b):\n",
+ " # Number of training examples\n",
+ " m = x.shape[0]\n",
+ " dj_dw = 0\n",
+ " dj_db = 0\n",
+ "\n",
+ " for i in range(m):\n",
+ " yhat = w * x[i] + b\n",
+ " dj_dw_i = (yhat - y[i]) * x[i]\n",
+ " dj_db_i = yhat - y[i]\n",
+ " dj_db += dj_db_i\n",
+ " dj_dw += dj_dw_i\n",
+ " dj_dw = dj_dw / m\n",
+ " dj_db = dj_db / m\n",
+ "\n",
+ " return dj_dw, dj_db"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "3U-qZNSFE1QK",
+ "outputId": "a17fac05-3521-4ce4-abe6-9e46273a7549",
+ "colab": {
+ "base_uri": "https://localhost:8080/"
+ }
+ },
+ "outputs": [
+ {
+ "output_type": "stream",
+ "name": "stdout",
+ "text": [
+ "\u001b[92mTest passed!\u001b[0m\n"
+ ]
+ }
+ ],
+ "source": [
+ "test_compute_gradient(compute_gradient)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "AhWGXBq82p6P"
+ },
+ "source": [
+ "## 2. Update the parameters num_iterations times\n",
+ "$$\\begin{align*} \\text{repeat}&\\text{ until convergence:} \\; \\lbrace \\newline\n",
+ "\\; w &= w - \\alpha \\frac{\\partial J(w,b)}{\\partial w} \\; \\newline\n",
+ " b &= b - \\alpha \\frac{\\partial J(w,b)}{\\partial b} \\newline \\rbrace\n",
+ "\\end{align*}$$\n",
+ "where, parameters $w$, $b$ are updated simultaneously."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "OlNwBvu52cUv"
+ },
+ "outputs": [],
+ "source": [
+ "def gradient_descent(x, y, learning_rate, num_iterations):\n",
+ " # Initialize weights and bias\n",
+ " w = 0\n",
+ " b = 0\n",
+ " # Number of training examples\n",
+ " m = x.shape[0]\n",
+ " for _ in range(num_iterations):\n",
+ " # Compute gradients using the compute_gradient function\n",
+ " dj_dw, dj_db = compute_gradient(x, y, w, b)\n",
+ "\n",
+ " # Update weights and bias\n",
+ " w = w - learning_rate * dj_dw\n",
+ " b = b - learning_rate * dj_db\n",
+ " # Compute the cost for monitoring\n",
+ " cost = compute_cost(x, y, w, b)\n",
+ " print(f'Iteration {_+1}/{num_iterations}, Cost: {cost:.6f}')\n",
+ " return w, b"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "dpmvXbs4lmHO",
+ "outputId": "c26dd13b-46d3-4e7c-f6b4-44733c37838c",
+ "colab": {
+ "base_uri": "https://localhost:8080/",
+ "height": 370
+ }
+ },
+ "outputs": [
+ {
+ "output_type": "error",
+ "ename": "NameError",
+ "evalue": "ignored",
+ "traceback": [
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
+ "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
+ "\u001b[0;32m| \n", + " | mass_npea | \n", + "tumor_size | \n", + "
|---|---|---|
| 0 | \n", + "18.159306 | \n", + "7.490802 | \n", + "
| 1 | \n", + "39.693228 | \n", + "19.014286 | \n", + "
| 2 | \n", + "32.659956 | \n", + "14.639879 | \n", + "
| 3 | \n", + "27.556925 | \n", + "11.973170 | \n", + "
| 4 | \n", + "9.800536 | \n", + "3.120373 | \n", + "
| ... | \n", + "... | \n", + "... | \n", + "
| 995 | \n", + "5.343260 | \n", + "1.831641 | \n", + "
| 996 | \n", + "39.080774 | \n", + "18.346272 | \n", + "
| 997 | \n", + "8.435708 | \n", + "2.736373 | \n", + "
| 998 | \n", + "40.580192 | \n", + "19.004747 | \n", + "
| 999 | \n", + "20.147810 | \n", + "8.920115 | \n", + "
1000 rows × 2 columns
\n", + "| \n", + " | mass_npea | \n", + "tumor_size | \n", + "
|---|---|---|
| count | \n", + "1000.000000 | \n", + "1000.000000 | \n", + "
| mean | \n", + "22.709158 | \n", + "9.805131 | \n", + "
| std | \n", + "11.682122 | \n", + "5.842747 | \n", + "
| min | \n", + "1.575483 | \n", + "0.092640 | \n", + "
| 25% | \n", + "12.290811 | \n", + "4.719465 | \n", + "
| 50% | \n", + "22.968280 | \n", + "9.936148 | \n", + "
| 75% | \n", + "32.664439 | \n", + "14.886392 | \n", + "
| max | \n", + "44.255681 | \n", + "19.994353 | \n", + "
LinearRegression()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
LinearRegression()