Path: blob/main/C1 - Supervised Machine Learning - Regression and Classification/week3/Optional Labs/C1_W3_Lab03_Decision_Boundary_Soln.ipynb
3748 views
Optional Lab: Logistic Regression, Decision Boundary
Goals
In this lab, you will:
Plot the decision boundary for a logistic regression model. This will give you a better sense of what the model is predicting.
Dataset
Let's suppose you have following training dataset
The input variable
X
is a numpy array which has 6 training examples, each with two featuresThe output variable
y
is also a numpy array with 6 examples, andy
is either0
or1
Plot data
Let's 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.
Logistic regression model
Suppose you'd like to train a logistic regression model on this data which has the form
where , which is the sigmoid function
Let's say that you trained the model and get the parameters as . That is,
(You'll learn how to fit these parameters to the data further in the course)
Let's try to understand what this trained model is predicting by plotting its decision boundary
Refresher on logistic regression and decision boundary
Recall that for logistic regression, the model is represented as
where is known as the sigmoid function and it maps all input values to values between 0 and 1:
ParseError: KaTeX parse error: \tag works only in display equations and is the vector dot product:
We interpret the output of the model () as the probability that given and parameterized by and .
Therefore, to get a final prediction ( or ) from the logistic regression model, we can use the following heuristic -
if , predict
if , predict
Let's plot the sigmoid function to see where
As you can see, for
For a logistic regression model, . Therefore,
if , the model predicts
if , the model predicts
Plotting decision boundary
Now, let's go back to our example to understand how the logistic regression model is making predictions.
Our logistic regression model has the form
From what you've learnt above, you can see that this model predicts if
Let's see what this looks like graphically. We'll start by plotting , which is equivalent to .
In the plot above, the blue line represents the line and it should intersect the x1 axis at 3 (if we set = 3, = 0) and the x0 axis at 3 (if we set = 0, = 3).
The shaded region represents . The region above the line is .
Any point in the shaded region (under the line) is classified as . Any point on or above the line is classified as . This line is known as the "decision boundary".
As we've seen in the lectures, by using higher order polynomial terms (eg: , we can come up with more complex non-linear boundaries.
Congratulations!
You have explored the decision boundary in the context of logistic regression.