Applied Machine Learning in Matlab
当前位置:以往案例 > >Applied Machine Learning in Matlab
2023-01-04

Programming Project 2

In this programming project, we will modify the code from the first project to make it support linear regression with multiple variables. We will also vectorize our calculations.

Before you start programming:

  • ●  Make sure that you have Octave on your system. If not, see https://www.gnu.org/software/octave/download to learn how you can install it.
    ●  Download and extract the assignment zip from Gradescope. Make sure you have the files “data.txt” and “run.m” in the assignment directory.
    ●  Open a terminal and navigate to the assignment directory using the “cd” command.
    ●  Using the same terminal, run Octave with the “octave” command.
    ●  The “run” script is ready to use the code that you will implement in this assignment.
    Simply run it by typing “run” on the Octave console. It will initially give errors, but those errors will get resolved as you add the required functions. This script will also test your functions on some preset values and will print the expected and calculated values. If those values are significantly different, then please go back to your implementation and fix the error.
    ●  Do not change the contents of those existing files unless you are instructed to do so.

    Implement a function to load data into a Design Matrix and an output vector

    We will implement a function that reads the given input file (which has our training examples), and gives us a design matrix X for the x values and a vector for the y values. The name of the function will be “loadData” and it will be implemented in a file named “loadData.m” (this is also true for every other function: the function name has to match the file name, except the “.m” extension).

    The function should first read the data into a matrix using the “load” function. On the provided data file, this will give us a matrix of 100 x 4, but those dimensions can be different for any other data file and your code should work fine with them as well. We can generalize this by saying the dimensions of the initial matrix will be m x (n+1), where m is the number of training examples, n is the number of training features and the last column is the output feature. Initialize X by selecting the first n columns of the matrix, and generate y by selecting the last column of the matrix. The design matrix X has to have a column of ones as its first column. As a last step, add a column of ones to X and make it the first column. In the end, X will have n+1 columns.

    Implement feature scaling

    Each feature in our data has a different spread. We will apply feature scaling to the features in X to make them distributed around 0 with a standard deviation of 1. Remember:

You will implement the function “featureScale” that will take X as a parameter and return another X with rescaled numbers. To calculate the means and standard deviations, you can use the built-in functions “mean” and “std”, respectively. These functions intuitively operate on vectors. And when their parameter is a matrix, they do the calculation for each column of the matrix, and return a row vector as result. mean(X) and std(X) will give you row vectors of size n+1.

Remember that in Octave, when we add or subtract a row vector from a matrix, the operation works on all the rows of the matrix separately. Similarly, when we get the dot product (.*) or a dot division (./) of a matrix with a row vector, the operation is applied to all rows of the matrix separately. Use these tips for a vectorized implementation.

When you rescale the entire X, the first column that is supposed to have ones will change as well. To fix it, you need to reset the values in the first column to 1.

Implement the hypothesis function

We will implement a function that calculates h


在线提交订单