Path: blob/main/C1 - Supervised Machine Learning - Regression and Classification/week3/Optional Labs/C1_W3_Lab05_Cost_Function_Soln.ipynb
3748 views
Optional Lab: Cost Function for Logistic Regression
Goals
In this lab, you will:
examine the implementation and utilize the cost function for logistic regression.
Dataset
Let's start with the same dataset as was used in the decision boundary lab.
We will use a helper function to plot this data. The data points with label are shown as red crosses, while the data points with label are shown as blue circles.
Cost function
In a previous lab, you developed the logistic loss function. Recall, loss is defined to apply to one example. Here you combine the losses to form the cost, which includes all the examples.
Recall that for logistic regression, the cost function is of the form
where
is the cost for a single data point, which is:
where m is the number of training examples in the data set and:
Code Description
The algorithm for compute_cost_logistic
loops over all the examples calculating the loss for each example and accumulating the total.
Note that the variables X and y are not scalar values but matrices of shape () and (,) respectively, where is the number of features and is the number of training examples.
Check the implementation of the cost function using the cell below.
Expected output: 0.3668667864055175
Example
Now, let's see what the cost function output is for a different value of .
In a previous lab, you plotted the decision boundary for . That is, you had
b = -3, w = np.array([1,1])
.Let's say you want to see if , or
b = -4, w = np.array([1,1])
provides a better model.
Let's first plot the decision boundary for these two different values to see which one fits the data better.
For , we'll plot (shown in blue)
For , we'll plot (shown in magenta)
You can see from this plot that b = -4, w = np.array([1,1])
is a worse model for the training data. Let's see if the cost function implementation reflects this.
Expected output
Cost for b = -3 : 0.3668667864055175
Cost for b = -4 : 0.5036808636748461
You can see the cost function behaves as expected and the cost for b = -4, w = np.array([1,1])
is indeed higher than the cost for b = -3, w = np.array([1,1])
Congratulations!
In this lab you examined and utilized the cost function for logistic regression.