# Linear Regression Python Homework

Online tutoring services

Need help with this question or any other Engineering assignment help task? Click on the button below to to hire an expert cheap.

All of the information needed to complete the homework is within the assignment. Please let me know if you have any further questions if the assignment is not clear.

## Get Help With a similar task to - Linear Regression Python Homework

{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#### Note: Please change the filename before submitting by writing your first name<br> in place of 'StudentName....' ####\n", "As an example, if I were submitting the HW the file name would be 'Jagannath_INFO_4150_E_HW_1.ipynb'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Import required modules for the exercise\n", "\n", "import numpy as np\n", "from matplotlib import pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The Dataset to use ###\n", "\n", "Here I have created a numpy array with some arbitrary values and then shaped them to form the dataset we want to use for this homework. All operations are from the numpy package.\n", "The variable $x_0$ is never given and we will have to additionally insert it as a column. This data set has one independent variables $x$ and corresponding y values which is the dependent variable." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " The dataset is: \n", "[[1.5 2.2 ]\n", " [2. 2.3 ]\n", " [2.5 2.35 ]\n", " [2.75 2.325]\n", " [3. 2.4 ]\n", " [3.5 2.5 ]\n", " [4. 2.45 ]\n", " [4.5 2.55 ]]\n", "\n", " The X column vector is: \n", "[[1.5 ]\n", " [2. ]\n", " [2.5 ]\n", " [2.75]\n", " [3. ]\n", " [3.5 ]\n", " [4. ]\n", " [4.5 ]]\n", "\n", " The y column vector is\n", "[[2.2 ]\n", " [2.3 ]\n", " [2.35 ]\n", " [2.325]\n", " [2.4 ]\n", " [2.5 ]\n", " [2.45 ]\n", " [2.55 ]]\n", " \n", " Shape of X is:(8, 1) and shape of y is: (8, 1)\n", " \n", " Number of data points in the data set: 8\n" ] } ], "source": [ "# In this cell we create the dataset\n", "\n", "values = [1.5,2.2,2,2.3,2.5,2.35,2.75,2.325,3,2.4,3.5,2.5,4,2.45,4.5,2.55] # Create a list of 16 values.\n", "dataset = np.array(values) # Convert the list to a numpy array based dataset\n", "dataset = dataset.reshape(-1,2) # Reshape the array into 8x2 numpy array or 7x2 matrix.\n", "\n", "# The column 1 of this array or matrix corresponds to the 'X' value and column 2 the 'y' value.\n", "\n", "X = dataset[:,0].reshape(-1,1) # Numpy operation to assign the column 1 values to the variable X as a 8x1.\n", "y = dataset[:,1].reshape(-1,1) # Numpy operation to assign the column 2 values to the variable y as 8x1\n", "\n", "\n", "# Print out the values to check correctness. Please do notice the elegant formatting used so that\n", "# the outputs are clearly legible and well presented.\n", "\n", "print(f\" The dataset is: \\n{dataset}\\n\\n The X column vector is: \\n{X}\\n\\n The y column vector is\\n{y}\")\n", "print(f\" \\n Shape of X is:{X.shape} and shape of y is: {y.shape}\")\n", "print(f\" \\n Number of data points in the data set: {len(X)}\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "<function matplotlib.pyplot.show(close=None, block=None)>" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD4CAYAAADiry33AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAVw0lEQVR4nO3df2zc933f8eerNIsRtgcBFRdXtBQJraEAaeLRYJUFLNK4WCJ7C2YlNRAjnYKuDYQFSWEDhhbLf2RY+0ccCDCS5ccMoTYyY06NYKY4I7bDCLWLxAjsmfqRMBLDQnNcRKQw0V4YyRixiMxrf9yX7vly5N1JJ5H30esBELr7fD735fvjj/y60/f7ve9XtomIiHL9xnoXEBERV1aCPiKicAn6iIjCJegjIgqXoI+IKNx1611AM5s3b/b27dvXu4yIiJ5x9OjR120PNuvbkEG/fft2Jicn17uMiIieIekfVuvLrpuIiMIl6CMiCpegj4goXII+IqJwCfqI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Y0br7d9g/WNosVN5HtiXap/Vp8AzgFHbPfkurQxD+iRNQG+BPwH4Fer9PfEmlS+xNpzgS6uS6lBf4zadR9uBb4CjK9vOWvT2jdeb/sG6xtBi7n0zLrYXrb9z6nd53iXpN9rGNIT69LGPHpiTSR9BDhn++haw5q0bbg1aXMuXV2XIoPe9vmVf7JWd7jql7R5nctqSq1vvH5VbrDeDa3m0kvrssL2AvB3wB0NXT2zLrD6PHpoTUaBfyPpNeBJ4I8k/beGMb2yJi3n0u11KTLoJd0k1W5eK2kXtXm+sb5V/bqqxjVvvA48DXyyOqPgXwC/sH32qhXZpnbm0kPrMihpU/V4APiXwE8ahm34dWlnHr2yJrYP2L7Z9nbgHuB52/+2YdiGXxNoby7dXpeePOtG0t8AHwQ2SzoD/EdqB5pWblZ+N/BpSUvAInCPN+ZXgNu58fqzwL8CTgP/F/h3V7/MtrQzl15Zl98G/qukPmr/g33L9rcl/XvoqXVpZx69siZN9eCarOpKrksugRARUbgid91ERMQ/StBHRBQuQR8RUbgEfURE4RL0ERGFS9BHRBQuQR8RUbj/D/bIR2Wr3caaAAAAAElFTkSuQmCC\n", "text/plain": [ "<Figure size 432x288 with 1 Axes>" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "# Let us plot the data points to get a sense for the data\n", "# We shall use matplotlib to plot a scatter plot\n", "\n", "plt.scatter(X,y)\n", "plt.show" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " New shape of X is (8, 2), and the modified array X is: \n", " [[1. 1.5 ]\n", " [1. 2. ]\n", " [1. 2.5 ]\n", " [1. 2.75]\n", " [1. 3. ]\n", " [1. 3.5 ]\n", " [1. 4. ]\n", " [1. 4.5 ]]\n" ] } ], "source": [ "# Insert an extra column of x0 = 1 values in array X.\n", "\n", "X = np.insert(X,0,[1],axis=1) # Numpy function to insert column 0 with values of 1.\n", "print(f\" New shape of X is {X.shape}, and the modified array X is: \\n {X}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Matrices and Vectorization for a single data point (one row of data): ###\n", "$X = \\begin{bmatrix} x_0\\\\x_1\\end{bmatrix}$ and y = $[y_1]$ where $x_0 = 1$ always<br><br>\n", "and assume $\\Theta = \\begin{bmatrix} \\theta_0\\\\\\theta_1\\end{bmatrix}$ which are the intercept and slope of a line.<br><br>\n", "Therefore for the single data point, X above, we can write the equation in Matrix or Vecotrized form as:<br><br>\n", "$$\\pmb{y' = \\Theta^TX}$$<br> and if you do the Matrix multiplication we get back:<br><br>\n", "$y' = \\theta_0x_0 + \\theta_1x_1$<br><br>\n", "and using the terminology we have agreed upon it would be, the hypothesis or predicted value is:<br><br>\n", "$$\\pmb{h_\\theta(x) = \\Theta^TX}$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vectorization of a complete dataset - example ###\n", "\n", "With the construct that we created above for a single data point, we can now extend the argument to a whole dataset with m data points in the following way:<br><br>\n", "We usually always have the values of every data point as a row. This example below, is a dataset with 2 variables $x_1,x_2$. <br>$x_0$ is the inserted variable and is equal to 1 and there are m data points in this dataset.<br><br>\n", "$X = \\begin{bmatrix} x_{10}&x_{11}&x_{12}\\\\x_{20}&x_{21}&x_{22}\\\\\\dots\\\\x_{i0}&x_{i1}&x_{i2}\\\\\\vdots\\\\x_{m0}&x_{m1}&x_{m2}\\end{bmatrix}$ and $y = \\begin{bmatrix} y_1\\\\y_2\\\\\\dots\\\\y_i\\\\\\vdots\\\\y_m\\end{bmatrix}$<br><br>\n", "We initialize $\\Theta = \\begin{bmatrix} \\theta_0\\\\\\theta_1\\\\\\theta_2\\end{bmatrix}$ because each data point is now $h_\\theta(x) = \\theta_0x_0 +\\theta_1x_1 + \\theta_2x2$<br><br>\n", "So X = m x 3 matrix and $\\Theta$ = 3 x 1 matrix, and therefore the vectorized form in this case will again be:<br><br>\n", "$\\pmb{h_\\theta(x) = X\\Theta}$ which will give us a m x 1 matrix as a result.<br><br>\n", "This is dimensionally correct as y is also a m x 1 matrix and now we can perform the $h_\\theta(x) - y$ operation, which with matrix operation is nothing but:<br><br>\n", "prediction error = $X\\Theta - y$<br><br>\n", "And finally, if you square this expresion and sum it up, you will get the value of J.<br><br>\n", "The final value of J = $\\pmb{\\frac{1}{2m}\\Sigma_m(X\\Theta - y)^2}$<br>\n", "and this will be the only line you will code to do a single shot estimation of J (without any \"for loops\", etc) in 1 iteration. The only for loop will be for the \"n iterations\" of calculating new J values for the new $\\Theta$ values after every gradient descent step. We do this to minimize the J value to its lowest." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The next step would be to calculate the first step of Gradient Descent and update the $\\Theta$ values. ###\n", "We have our initialized $\\Theta$ column vector or matrix and the gradient descent step in each iteration should update these values. Mathematically that looks as follows:<br><br>\n", "$\\begin{bmatrix} \\theta_0\\\\\\theta_1\\end{bmatrix}=\\begin{bmatrix} \\theta_0\\\\\\theta_1\\end{bmatrix}-\\alpha\\begin{bmatrix}\\frac{\\partial J}{\\partial \\theta_0}\\\\\\frac{\\partial J}{\\partial \\theta_1}\\end{bmatrix}$<br><br>\n", "Where $\\alpha$ is the learning rate. The value of alpha should not be too large (causes oscilattion and never finds the lowest point) OR too small (gradient descent can take too many iterations to converge). A good starting value could be 0.01 or 0.001 and then one can tune it. <br><br> As one can see, again the update step is a single operation and all values of $\\Theta$ get updated simultaneously in each iteration because of the vectorized way of doing the computations.<br><br>\n", "On the slides in the lecture videos, I have already given the equations for the partial differentials $\\frac{\\partial J}{\\partial \\theta_0},\\frac{\\partial J}{\\partial \\theta_1}$ and you only have to implement them in code using matrix operations.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Please code the 'for' loop within the area marked as #ToDo and #EndToDo\n", "# All other lines of code have already been provided and explained.\n", "# Code the for loop for 500 iterations.\n", "\n", "\n", "# Before the for loop, initial 2 empty lists 'J_hist' and 'iteration' to store values of J & iteration number.\n", "# Also initalize the value of alpha\n", "# Start the for loop and within it write the code for,\n", "# a) Compute the value J using the functions np.dot(),np.power(),sum(),np.sum() & len(x) in one line of code.\n", "# b) The line of code to compute the new theta values - the gradient descent part.\n", "# c) Store the value of J and the iteration number by appending to the empty lists created.\n", "\n", "J_hist = []\n", "iterations = []\n", "alpha = 0.001 \n", "theta = np.array([[1],[2]]) # Initialize theta as a 2x1 array or matrix containing theta0 and theta1\n", "\n", "#ToDo\n", "\n", " \n", " \n", "#EndToDo \n", "\n", "# If everything goes well the print statement should print \n", "# i) value of max J as 11.9436...\n", "# ii) value of min J as 0.09546...\n", "\n", "print(f\" Max value of J is: {max(J_hist)} \\n Min value of J is: {min(J_hist)} \\n Final value of theta is: \\n {theta}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Plot the iterations versus J_hist curve\n", "# Use the matplotlib function 'plt.plot()'\n", "\n", "#ToDo\n", "\n", "#EndToDo\n", "\n", "plt.title(\"Gradient Descent Curve\")\n", "plt.xlabel('Iterations')\n", "plt.ylabel('J')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write Observations if any in this Cell" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.8.8" } }, "nbformat": 4, "nbformat_minor": 4 }

## Related Questions

Similar orders to Linear Regression Python Homework
20
Views
0
Create a Simple Blackjack Game in c++
I need the code for a simple 1 player vs computer blackjack game code. The game is blackjack, and the code doesn't have to be very advanced and professional. I’m barely on the principal engineering classes of college....
26
Views
0
information on refrigeration systems
information on completing some questions on refrigeration systems souch as 3. A refrigeration system control circuit has a low-pressure control and a thermostat connected. If the low-pressure control is intended to be a safety control against loss of charge, describe how the electrical connection may be made between the thermostat, the low-pressure control and the motor circuit? You may illustrate your answer with a simple circuit diagram...
40
Views
0