Path: blob/main/C1 - Supervised Machine Learning - Regression and Classification/week3/Optional Labs/C1_W3_Lab02_Sigmoid_function_Soln.ipynb
3748 views
Optional Lab: Logistic Regression
In this ungraded lab, you will
explore the sigmoid function (also known as the logistic function)
explore logistic regression; which uses the sigmoid function
Sigmoid or Logistic Function
As discussed in the lecture videos, for a classification task, we can start by using our linear regression model, , to predict given .
However, we would like the predictions of our classification model to be between 0 and 1 since our output variable is either 0 or 1.
This can be accomplished by using a "sigmoid function" which maps all input values to values between 0 and 1.
Let's implement the sigmoid function and see this for ourselves.
Formula for Sigmoid function
The formula for a sigmoid function is as follows -
ParseError: KaTeX parse error: \tag works only in display equations
In the case of logistic regression, z (the input to the sigmoid function), is the output of a linear regression model.
In the case of a single example, is scalar.
in the case of multiple examples, may be a vector consisting of values, one for each example.
The implementation of the sigmoid function should cover both of these potential input formats. Let's implement this in Python.
NumPy has a function called exp()
, which offers a convenient way to calculate the exponential ( ) of all elements in the input array (z
).
It also works with a single number as an input, as shown below.
The sigmoid
function is implemented in python as shown in the cell below.
Let's see what the output of this function is for various value of z
The values in the left column are z
, and the values in the right column are sigmoid(z)
. As you can see, the input values to the sigmoid range from -10 to 10, and the output values range from 0 to 1.
Now, let's try to plot this function using the matplotlib
library.
As you can see, the sigmoid function approaches 0
as z
goes to large negative values and approaches 1
as z
goes to large positive values.
Logistic Regression
A logistic regression model applies the sigmoid to the familiar linear regression model as shown below:
where
ParseError: KaTeX parse error: \tag works only in display equations
Let's apply logistic regression to the categorical data example of tumor classification. First, load the examples and initial values for the parameters.
Try the following steps:
Click on 'Run Logistic Regression' to find the best logistic regression model for the given training data
Note the resulting model fits the data quite well.
Note, the orange line is '' or above. It does not match the line in a linear regression model. Further improve these results by applying a threshold.
Tick the box on the 'Toggle 0.5 threshold' to show the predictions if a threshold is applied.
These predictions look good. The predictions match the data
Now, add further data points in the large tumor size range (near 10), and re-run logistic regression.
unlike the linear regression model, this model continues to make correct predictions
Congratulations!
You have explored the use of the sigmoid function in logistic regression.