Description
In this exercise, you will implement linear regression and get to see it work
on data. Before starting on this programming exercise, we strongly recommend watching the video lectures and completing the review questions for
the associated topics.
To get started with the exercise, you will need to download the starter
code and unzip its contents to the directory where you wish to complete the
exercise. If needed, use the cd command in Octave/MATLAB to change to
this directory before starting this exercise.
You can also find instructions for installing Octave/MATLAB in the “Environment Setup Instructions” of the course website.
Files included in this exercise
ex1.m – Octave/MATLAB script that steps you through the exercise
ex1 multi.m – Octave/MATLAB script for the later parts of the exercise
ex1data1.txt – Dataset for linear regression with one variable
ex1data2.txt – Dataset for linear regression with multiple variables
submit.m – Submission script that sends your solutions to our servers
[?] warmUpExercise.m – Simple example function in Octave/MATLAB
[?] plotData.m – Function to display the dataset
[?] computeCost.m – Function to compute the cost of linear regression
[?] gradientDescent.m – Function to run gradient descent
[†] computeCostMulti.m – Cost function for multiple variables
[†] gradientDescentMulti.m – Gradient descent for multiple variables
[†] featureNormalize.m – Function to normalize features
[†] normalEqn.m – Function to compute the normal equations
? indicates files you will need to complete
† indicates optional exercises
1Throughout the exercise, you will be using the scripts ex1.m and ex1 multi.m.
These scripts set up the dataset for the problems and make calls to functions
that you will write. You do not need to modify either of them. You are only
required to modify functions in other files, by following the instructions in
this assignment.
For this programming exercise, you are only required to complete the first
part of the exercise to implement linear regression with one variable. The
second part of the exercise, which is optional, covers linear regression with
multiple variables.
Where to get help
The exercises in this course use Octave1 or MATLAB, a high-level programming language well-suited for numerical computations. If you do not have
Octave or MATLAB installed, please refer to the installation instructions in
the “Environment Setup Instructions” of the course website.
At the Octave/MATLAB command line, typing help followed by a function name displays documentation for a built-in function. For example, help
plot will bring up help information for plotting. Further documentation for
Octave functions can be found at the Octave documentation pages. MATLAB documentation can be found at the MATLAB documentation pages.
We also strongly encourage using the online Discussions to discuss exercises with other students. However, do not look at any source code written
by others or share your source code with others.
1 Simple Octave/MATLAB function
The first part of ex1.m gives you practice with Octave/MATLAB syntax and
the homework submission process. In the file warmUpExercise.m, you will
find the outline of an Octave/MATLAB function. Modify it to return a 5 x
5 identity matrix by filling in the following code:
A = eye(5);
1Octave is a free alternative to MATLAB. For the programming exercises, you are free
to use either Octave or MATLAB.
2When you are finished, run ex1.m (assuming you are in the correct directory, type “ex1” at the Octave/MATLAB prompt) and you should see
output similar to the following:
ans =
Diagonal Matrix
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Now ex1.m will pause until you press any key, and then will run the code
for the next part of the assignment. If you wish to quit, typing ctrl-c will
stop the program in the middle of its run.
1.1 Submitting Solutions
After completing a part of the exercise, you can submit your solutions for
grading by typing submit at the Octave/MATLAB command line. The submission script will prompt you for your login e-mail and submission token
and ask you which files you want to submit. You can obtain a submission
token from the web page for the assignment.
You should now submit your solutions.
You are allowed to submit your solutions multiple times, and we will take
only the highest score into consideration.
2 Linear regression with one variable
In this part of this exercise, you will implement linear regression with one
variable to predict profits for a food truck. Suppose you are the CEO of a
restaurant franchise and are considering different cities for opening a new
outlet. The chain already has trucks in various cities and you have data for
profits and populations from the cities.
3You would like to use this data to help you select which city to expand
to next.
The file ex1data1.txt contains the dataset for our linear regression problem. The first column is the population of a city and the second column is
the profit of a food truck in that city. A negative value for profit indicates a
loss.
The ex1.m script has already been set up to load this data for you.
2.1 Plotting the Data
Before starting on any task, it is often useful to understand the data by
visualizing it. For this dataset, you can use a scatter plot to visualize the
data, since it has only two properties to plot (profit and population). (Many
other problems that you will encounter in real life are multi-dimensional and
can’t be plotted on a 2-d plot.)
In ex1.m, the dataset is loaded from the data file into the variables X
and y:
data = load(‘ex1data1.txt’); % read comma separated data
X = data(:, 1); y = data(:, 2);
m = length(y); % number of training examples
Next, the script calls the plotData function to create a scatter plot of
the data. Your job is to complete plotData.m to draw the plot; modify the
file and fill in the following code:
plot(x, y, ‘rx’, ‘MarkerSize’, 10); % Plot the data
ylabel(‘Profit in $10,000s’); % Set the y−axis label
xlabel(‘Population of City in 10,000s’); % Set the x−axis label
Now, when you continue to run ex1.m, our end result should look like
Figure 1, with the same red “x” markers and axis labels.
To learn more about the plot command, you can type help plot at the
Octave/MATLAB command prompt or to search online for plotting documentation. (To change the markers to red “x”, we used the option ‘rx’
together with the plot command, i.e., plot(..,[your options here],..,
‘rx’); )
44 6 8 10 12 14 16 18 20 22 24
−5
0
5
10
15
20
25
Profit in $10,000s
Population of City in 10,000s
Figure 1: Scatter plot of training data
2.2 Gradient Descent
In this part, you will fit the linear regression parameters θ to our dataset
using gradient descent.
2.2.1 Update Equations
The objective of linear regression is to minimize the cost function
J(θ) = 1
2m
Xm
i=1


