Starting from:

$29.99

Logistic Regression_assignment 2

Programming Exercise 2: Logistic Regression

1 Logistic Regression
In this part of the exercise, you will build a logistic regression model to
predict whether a student gets admitted into a university.
Suppose that you are the administrator of a university department and
you want to determine each applicant's chance of admission based on their
1Octave is a free alternative to MATLAB. For the programming exercises, you are free
to use either Octave or MATLAB.
2
results on two exams. You have historical data from previous applicants
that you can use as a training set for logistic regression. For each training
example, you have the applicant's scores on two exams and the admissions
decision.
Your task is to build a classi?cation model that estimates an applicant's
probability of admission based the scores from those two exams. This outline
and the framework code in ex2.m will guide you through the exercise.
1.1 Visualizing the data
Before starting to implement any learning algorithm, it is always good to
visualize the data if possible. In the ?rst part of ex2.m, the code will load the
data and display it on a 2-dimensional plot by calling the function plotData.
You will now complete the code in plotData so that it displays a ?gure
like Figure 1, where the axes are the two exam scores, and the positive and
negative examples are shown with di?erent markers.
30 40 50 60 70 80 90 100
30
40
50
60
70
80
90
100
Exam 1 score
Exam 2 score
Admitted
Not admitted
Figure 1: Scatter plot of training data
To help you get more familiar with plotting, we have left plotData.m
empty so you can try to implement it yourself. However, this is an optional
(ungraded) exercise. We also provide our implementation below so you can
copy it or refer to it. If you choose to copy our example, make sure you learn
what each of its commands is doing by consulting the Octave documentation.
3
% Find Indices of Positive and Negative Examples
pos = find(y==1); neg = find(y == 0);
% Plot Examples
plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, ...
'MarkerSize', 7);
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y', ...
'MarkerSize', 7);
1.2 Implementation
1.2.1 Warmup exercise: sigmoid function
Before you start with the actual cost function, recall that the logistic regres-
sion hypothesis is de?ned as:
h?(x) = g(?T x);
where function g is the sigmoid function. The sigmoid function is de?ned as:
g(z) =
1
1 + e􀀀z :
Your ?rst step is to implement this function in sigmoid.m so it can be
called by the rest of your program. When you are ?nished, try testing a few
values by calling sigmoid(x) at the octave command line. For large positive
values of x, the sigmoid should be close to 1, while for large negative values,
the sigmoid should be close to 0. Evaluating sigmoid(0) should give you
exactly 0.5. Your code should also work with vectors and matrices. For
a matrix, your function should perform the sigmoid function on
every element.
You can submit your solution for grading by typing submit at the Octave
command line. The submission script will prompt you for your username and
password and ask you which ?les you want to submit. You can obtain a sub-
mission password from the website.
You should now submit the warm up exercise.
1.2.2 Cost function and gradient
Now you will implement the cost function and gradient for logistic regression.
Complete the code in costFunction.m to return the cost and gradient.
4
Recall that the cost function in logistic regression is
J(?) =
1
m
Xm
i=1
?
􀀀y(i) log(h?(x(i))) 􀀀 (1 􀀀 y(i)) log(1 􀀀 h?(x(i)))
?
;
and the gradient of the cost is a vector of the same length as ? where the jth
element (for j = 0; 1; : : : ; n) is de?ned as follows:
@J(?)
@?j
=
1
m
Xm
i=1
(h?(x(i)) 􀀀 y(i))x(i)
j
Note that while this gradient looks identical to the linear regression gra-
dient, the formula is actually di?erent because linear and logistic regression
have di?erent de?nitions of h?(x).
Once you are done, ex2.m will call your costFunction using the initial
parameters of ?. You should see that the cost is about 0.693.
You should now submit the cost function and gradient for logistic re-
gression. Make two submissions: one for the cost function and one for the
gradient.
1.2.3 Learning parameters using fminunc
In the previous assignment, you found the optimal parameters of a linear
regression model by implementing gradent descent. You wrote a cost function
and calculated its gradient, then took a gradient descent step accordingly.
This time, instead of taking gradient descent steps, you will use an Octave
built-in function called fminunc.
Octave's fminunc is an optimization solver that ?nds the minimum of an
unconstrained2 function. For logistic regression, you want to optimize the
cost function J(?) with parameters ?.
Concretely, you are going to use fminunc to ?nd the best parameters ?
for the logistic regression cost function, given a ?xed dataset (of X and y
values). You will pass to fminunc the following inputs:
ˆ The initial values of the parameters we are trying to optimize.
2Constraints in optimization often refer to constraints on the parameters, for example,
constraints that bound the possible values ? can take (e.g., ? ? 1). Logistic regression
does not have such constraints since ? is allowed to take any real value.
5
ˆ A function that, when given the training set and a particular ?, computes
the logistic regression cost and gradient with respect to ? for the dataset
(X, y)
In ex2.m, we already have code written to call fminunc with the correct
arguments.
% Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);
% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(@(t)(costFunction(t, X, y)), initial theta, options);
In this code snippet, we ?rst de?ned the options to be used with fminunc.
Speci?cally, we set the GradObj option to on, which tells fminunc that our
function returns both the cost and the gradient. This allows fminunc to
use the gradient when minimizing the function. Furthermore, we set the
MaxIter option to 400, so that fminunc will run for at most 400 steps before
it terminates.
To specify the actual function we are minimizing, we use a \short-hand"
for specifying functions with the @(t) ( costFunction(t, X, y) ) . This
creates a function, with argument t, which calls your costFunction. This
allows us to wrap the costFunction for use with fminunc.
If you have completed the costFunction correctly, fminunc will converge
on the right optimization parameters and return the ?nal values of the cost
and ?. Notice that by using fminunc, you did not have to write any loops
yourself, or set a learning rate like you did for gradient descent. This is all
done by fminunc: you only needed to provide a function calculating the cost
and the gradient.
Once fminunc completes, ex2.m will call your costFunction function
using the optimal parameters of ?. You should see that the cost is about
0.203.
This ?nal ? value will then be used to plot the decision boundary on the
training data, resulting in a ?gure similar to Figure 2. We also encourage
you to look at the code in plotDecisionBoundary.m to see how to plot such
a boundary using the ? values.
6
30 40 50 60 70 80 90 100
30
40
50
60
70
80
90
100
Exam 1 score
Exam 2 score
Admitted
Not admitted
Figure 2: Training data with decision boundary
1.2.4 Evaluating logistic regression
After learning the parameters, you can use the model to predict whether a
particular student will be admitted. For a student with an Exam 1 score
of 45 and an Exam 2 score of 85, you should expect to see an admission
probability of 0.776.
Another way to evaluate the quality of the parameters we have found
is to see how well the learned model predicts on our training set. In this
part, your task is to complete the code in predict.m. The predict function
will produce \1" or \0" predictions given a dataset and a learned parameter
vector ?.
After you have completed the code in predict.m, the ex2.m script will
proceed to report the training accuracy of your classi?er by computing the
percentage of examples it got correct.
You should now submit the prediction function for logistic regression.
7
2 Regularized logistic regression
In this part of the exercise, you will implement regularized logistic regression
to predict whether microchips from a fabrication plant passes quality assur-
ance (QA). During QA, each microchip goes through various tests to ensure
it is functioning correctly.
Suppose you are the product manager of the factory and you have the
test results for some microchips on two di?erent tests. From these two tests,
you would like to determine whether the microchips should be accepted or
rejected. To help you make the decision, you have a dataset of test results
on past microchips, from which you can build a logistic regression model.
You will use another script, ex2 reg.m to complete this portion of the
exercise.
2.1 Visualizing the data
Similar to the previous parts of this exercise, plotData is used to generate a
gure like Figure 3, where the axes are the two test scores, and the positive
(y = 1, accepted) and negative (y = 0, rejected) examples are shown with
di?erent markers.
−1 −0.5 0 0.5 1 1.5
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1
1.2
Microchip Test 1
Microchip Test 2
y = 1
y = 0
Figure 3: Plot of training data
Figure 3 shows that our dataset cannot be separated into positive and
8
negative examples by a straight-line through the plot. Therefore, a straight-
forward application of logistic regression will not perform well on this dataset
since logistic regression will only be able to ?nd a linear decision boundary.
2.2 Feature mapping
One way to ?t the data better is to create more features from each data
point. In the provided function mapFeature.m, we will map the features into
all polynomial terms of x1 and x2 up to the sixth power.
mapFeature(x) =
2
6666666666666664
1
x1
x2
x21
x1x2
x22
x31
...
x1x52
x62
3
7777777777777775
As a result of this mapping, our vector of two features (the scores on
two QA tests) has been transformed into a 28-dimensional vector. A logistic
regression classi?er trained on this higher-dimension feature vector will have
a more complex decision boundary and will appear nonlinear when drawn in
our 2-dimensional plot.
While the feature mapping allows us to build a more expressive classi?er,
it also more susceptible to over?tting. In the next parts of the exercise, you
will implement regularized logistic regression to ?t the data and also see for
yourself how regularization can help combat the over?tting problem.
2.3 Cost function and gradient
Now you will implement code to compute the cost function and gradient for
regularized logistic regression. Complete the code in costFunctionReg.m to
return the cost and gradient.
Recall that the regularized cost function in logistic regression is
J(?) =
1
m
Xm
i=1
?
􀀀y(i) log(h?(x(i))) 􀀀 (1 􀀀 y(i)) log(1 􀀀 h?(x(i)))
?
+
?
2m
Xn
j=1
?2
j :
9
Note that you should not regularize the parameter ?0. In Octave, re-
call that indexing starts from 1, hence, you should not be regularizing the
theta(1) parameter (which corresponds to ?0) in the code. The gradient of
the cost function is a vector where the jth element is de?ned as follows:
@J(?)
@?0
=
1
m
Xm
i=1
(h?(x(i)) 􀀀 y(i))x(i)
j for j = 0
@J(?)
@?j
=

1
m
Xm
i=1
(h?(x(i)) 􀀀 y(i))x(i)
j
!
+
?
m
?j for j ? 1
Once you are done, ex2 reg.m will call your costFunctionReg function
using the initial value of ? (initialized to all zeros). You should see that the
cost is about 0.693.
You should now submit the cost function and gradient for regularized lo-
gistic regression. Make two submissions, one for the cost function and one
for the gradient.
2.3.1 Learning parameters using fminunc
Similar to the previous parts, you will use fminunc to learn the optimal
parameters ?. If you have completed the cost and gradient for regularized
logistic regression (costFunctionReg.m) correctly, you should be able to step
through the next part of ex2 reg.m to learn the parameters ? using fminunc.
2.4 Plotting the decision boundary
To help you visualize the model learned by this classi?er, we have pro-
vided the function plotDecisionBoundary.m which plots the (non-linear)
decision boundary that separates the positive and negative examples. In
plotDecisionBoundary.m, we plot the non-linear decision boundary by com-
puting the classi?er's predictions on an evenly spaced grid and then and drew
a contour plot of where the predictions change from y = 0 to y = 1.
After learning the parameters ?, the next step in ex reg.m will plot a
decision boundary similar to Figure 4.
10
2.5 Optional (ungraded) exercises
In this part of the exercise, you will get to try out di?erent regularization
parameters for the dataset to understand how regularization prevents over-
tting.
Notice the changes in the decision boundary as you vary ?. With a small
?, you should ?nd that the classi?er gets almost every training example
correct, but draws a very complicated boundary, thus over?tting the data
(Figure 5). This is not a good decision boundary: for example, it predicts
that a point at x = (􀀀0:25; 1:5) is accepted (y = 1), which seems to be an
incorrect decision given the training set.
With a larger ?, you should see a plot that shows an simpler decision
boundary which still separates the positives and negatives fairly well. How-
ever, if ? is set to too high a value, you will not get a good ?t and the decision
boundary will not follow the data so well, thus under?tting the data (Figure
6).
You do not need to submit any solutions for these optional (ungraded)
exercises.
−1 −0.5 0 0.5 1 1.5
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1
1.2
lambda = 1
Microchip Test 1
Microchip Test 2
y = 1
y = 0
Decision boundary
Figure 4: Training data with decision boundary (? = 1)
11
−1 −0.5 0 0.5 1 1.5
−1
−0.5
0
0.5
1
1.5
lambda = 0
Microchip Test 1
Microchip Test 2
y = 1
y = 0
Decision boundary
Figure 5: No regularization (Over?tting) (? = 0)
−1 −0.5 0 0.5 1 1.5
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
1
1.2
lambda = 100
Microchip Test 1
Microchip Test 2
y = 1
y = 0
Decision boundary
Figure 6: Too much regularization (Under?tting) (? = 100)
12

More products