Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 5: Logistic Regression

In this chapter, we will learn how to predict categories using the logistic regression model.

In logistic regression (classification), the output variable y can take on only one of a small handful of possible values instead of any number in an infinite range of numbers.

Therefore, it turns out that Linear Regression is not a good algorithm for classification problems.

Classification Problem

QuestionAnswer “y”
Is this email spam?no or yes
Is this transaction fraudulent?no or yes
Is the tumor malignant?no or yes

This type of classification problem where there are only two possible outputs is called binary classification.

In these problems, the terms class and category are used relatively interchangeably.

Binary Classification

NOTE:

Use the numbers zero “0” and one “1” to represent the answer y (target output).

Example of classifying if a tumor is malignant

Here’s an example of a training set for classifying if the tumor is malignant.

FIG

Classification with Linear Regression!

For this example of a training set for classifying if the tumor is malignant, one thing you could try on this training set is to apply the algorithm you already know.

FIG

If you do that, maybe the straight line looks like this, right?

FIG
FIG
FIG
A single artificial neuron

Logistic Regression

FIG

Sigmoid Function (Logistic Function)

FIG

Let’s build: Logistic Regression

Interpretation of Logistic Regression Output

Example

x is “tumor size” y is 0 (not malignant) or 1 (malignant)

fw⃗,b(X⃗)=0.7f_{\vec{w},b}(\vec{X}) = 0.7

Adds to 1

P(y=0)+P(y=1)=1P(y = 0) + P (y = 1) = 1

In general, P(y=0)+P(y=1)+P(y=2)+...+P(y=n)=1P(y=0) + P(y = 1) + P(y = 2) + ... + P(y = n) = 1

Logistic Notation

fw⃗,b(X⃗)=P(y=1∣X⃗;w⃗,b)f_{\vec{w}, b} (\vec{X}) = P(y = 1 | \vec{X}; \vec{w}, b)

Real world usage

For a long time, a lot of Internet advertising was actually driven by basically a slight variation of logistic regression. This was very lucrative for some large companies, and this is basically the algorithm that decided what ad was shown to you and many others on some large websites.

Decision Boundary

let’s take a look at the decision boundary to get a better sense of how logistic regression is computing these predictions.

Recap & Intuition

To recap,

fw⃗,b(X⃗)=g(z)=g(w⃗.X⃗+b)=11+e−(w⃗.X⃗+b)=P(y=1∣x⃗;w⃗,b)f_{\vec{w}, b} (\vec{X}) = g(z) = g(\vec{w}.\vec{X} + b) = \frac{1}{1 + e^{-(\vec{w}.\vec{X} + b)}} = P(y =1|\vec{x};\vec{w}, b)

where,

z=w⃗.X⃗+bz = \vec{w}.\vec{X} + b

This is going to be a number like maybe a 0.7 or 0.3, etc...

what if you want the algorithm to predict whether y is going to be zero or one?

So,

FIG

When is fw⃗,b(X⃗)≥0.5?f_{\vec{w}, b}(\vec{X}) \ge 0.5?

We recall,

fw⃗,b(X⃗)=g(z)=g(w⃗.X⃗+b)=11+e−(w⃗.X⃗+b)=P(y=1∣x⃗;w⃗,b)f_{\vec{w}, b} (\vec{X}) = g(z) = g(\vec{w}.\vec{X} + b) = \frac{1}{1 + e^{-(\vec{w}.\vec{X} + b)}} = P(y =1|\vec{x};\vec{w}, b)

so,

  1. fw⃗,b(X⃗)≥0.5f_{\vec{w}, b} (\vec{X}) \ge 0.5 when g(z)≥0.5g(z) \ge 0.5

  2. when g(z)≥0.5g(z) \ge 0.5 ? Based on above sigmoid graph, when z≥0z \ge 0

  3. When is z≥0z \ge 0 ? Well z=w⃗.X⃗+bz = \vec{w}.\vec{X} + b, so z≥0z \ge 0 whenever w⃗.X⃗+b≥0\vec{w} . \vec{X} + b \ge 0

    • whenever w⃗.X⃗+b>0\vec{w}.\vec{X} + b \gt 0, y^=1\hat{y} = 1

    • whenever w⃗.X⃗+b<0\vec{w} . \vec{X} + b \lt 0, y^=0\hat{y} = 0

    • what about w⃗.X⃗+b==0\vec{w}.\vec{X} + b == 0 ? well, that depends on the problem we are solving!!!

Visualize how the model makes predictions

Problem 1 - Linear Decision Boundary

FIG

take an example of a classification problem where you have two features, x1x_1 and x2x_2 instead of just one feature. Here’s a training set where the little red diamonds denote the positive examples and the little blue circles denote negative examples.

The logistic regression model will make predictions using this function:

fw⃗,b(X⃗)=g(z)=g(w1x1+w2x2+b)f_{\vec{w}, b} (\vec{X}) = g(z) = g(w_1x_1 + w_2x_2 + b)

because we have two features x1x_1 and x2x_2.

Let’s now take a look at how logistic regression makes predictions.

To figure that out, there’s a very interesting line to look at, which is when w⃗.x⃗+b=0\vec{w}.\vec{x} + b = 0. THIS LINE IS ALSO CALLED THE DECISION BOUNDARY, because that’s the line where you’re just almost neutral about whether y is 0 or y is 1.

FIG

Problem 2 - Non-Linear Decision Boundary

Now let’s look at a more complex example where the decision boundary is no longer a straight line.

FIG

As before, diamonds denote the class y equals 1, and the little circles denote the class y equals 0.

We saw how to use polynomials in linear regression, and we can do the same in logistic regression.

Let’s use function:

z=w1x12+w2x22+bz = w_{1}x_{1}^2 + w_{2}x_{2}^2 + b

therefore,

fw⃗,b(X⃗)=g(z)=g(w1x12+w2x22+b)f_{\vec{w}, b}(\vec{X}) = g(z) = g(w_{1}x_{1}^2 + w_{2}x_{2}^2 + b)

Let’s say, we ended up using w1=1,w2=1w_1 = 1, w_2 = 1 and b=−1b = -1

So,

z=x12+x22−1z = x_{1}^2 + x_{2}^2 - 1

The decision boundary, as before, will correspond to when z=0z = 0

z=x12+x22−1=0z = x_{1}^2 + x_{2}^2 - 1 = 0

x12+x22=1x_{1}^2 + x_{2}^2 = 1

That’s the decision boundary, and it looks like this:

FIG

Problem X - More Complex?

Can we come up with even more complex decision boundaries than these?

Ellipse
FIG
More Complex?

Yes!

FIG

You can even get more complex decision boundaries, which can look like functions that look like this.

and even more complex!!!

This implementation of logistic regression will predict y equals 1 inside this shape and y equals 0 outside it.

With these polynomial features, you can get very complex decision boundaries. In other words, logistic regression can learn to fit pretty complex data.

Note: If you don’t include any higher-order polynomials, so the only features you use are x1,x2,x3x_1, x_2, x_3, and so on, then the decision boundary will always be linear.

Cost Function

Why we can’t use mean squared error for classification?

J(w⃗,b)=1m∑i=1m12(fw⃗,b(X⃗(i))−y(i))2J(\vec{w}, b) = \frac{1}{m} \sum_{i=1}^{m} \frac{1}{2} (f_{\vec{w},b}(\vec{X}^{(i)}) - y^{(i)})^2
FIG

Different cost function!

Instead, there will be a different cost function that can make the cost function convex again. so gradient descent is guaranteed to converge to the global minimum.

NOTE: The only thing I’ve changed is that I put the one half inside the summation instead of outside the summation. This will make the math a little bit simpler.

Generalize Cost Function

I am going to generalize the cost function first:

J(w⃗,b)=1m∑i=1mL(fw⃗,b(X⃗(i)),y(i))J(\vec{w}, b) = \frac{1}{m} \sum_{i = 1}^{m} L(f_{\vec{w}, b} (\vec{X}^{(i)}), y^{(i)})

where L = Loss Function.

Logistic Loss Function

L(fw⃗,b(X⃗(i)),y(i))={−log⁡(fw⃗,b(X⃗(i))),if y(i)=1,−log⁡(1−fw⃗,b(X⃗(i))),if y(i)=0.L(f_{\vec{w}, b}(\vec{X}^{(i)}), y^{(i)}) = \begin{cases} -\log\left(f_{\vec{w}, b}(\vec{X}^{(i)})\right), & \text{if } y^{(i)} = 1, \\ -\log\left(1 - f_{\vec{w}, b}(\vec{X}^{(i)})\right), & \text{if } y^{(i)} = 0. \end{cases}

By summing up the losses on all of the training examples, you get the cost function, which measures how well you’re doing on the entire training set.

y = 1

Let’s first consider the case of y equals 1 and plot what this function looks like to gain some intuition about what this loss function is doing.

FIG
y = 0

On this slide, let’s look at the second part of the loss function corresponding to when y is equal to 0.

FIG
Coming back to our tumor dataset.

Going back to the tumor example: if the model predicts a 99.9% chance of malignancy, but it turns out not to be malignant (y = 0), then we penalize the model with a very high loss.

In this case of y equals 0, just as in the case of y equals 1 on the previous slide, the further the prediction f of x is away from the true value of y, the higher the loss.

***The Further the prediction fw⃗,b(X⃗(i))f_{\vec{w}, b} (\vec{X}^{(i)}) is from target y(i)y^{(i)}, the higher the loss. ***

When the true label is 1, the algorithm is strongly incentivized not to predict something too close to 0.

the overall cost function will be convex and thus you can reliably use gradient descent to take you to the global minimum.

Convex is better

the overall cost function will be convex and thus you can reliably use gradient descent to take you to the global minimum.

Learn to prove if cost function is convex

L(fw⃗,b(X⃗(i)),y(i))={−log⁡(fw⃗,b(X⃗(i))),if y(i)=1,−log⁡(1−fw⃗,b(X⃗(i))),if y(i)=0.L(f_{\vec{w}, b}(\vec{X}^{(i)}), y^{(i)}) = \begin{cases} -\log\left(f_{\vec{w}, b}(\vec{X}^{(i)})\right), & \text{if } y^{(i)} = 1, \\ -\log\left(1 - f_{\vec{w}, b}(\vec{X}^{(i)})\right), & \text{if } y^{(i)} = 0. \end{cases}

find w, b that minimize cost J.

Simplified Cost Function for Logistic Regression

Here is the Loss Function for Logistic Regression:

L(fw⃗,b(X⃗(i)),y(i))={−log⁡(fw⃗,b(X⃗(i))),if y(i)=1,−log⁡(1−fw⃗,b(X⃗(i))),if y(i)=0.L(f_{\vec{w}, b}(\vec{X}^{(i)}), y^{(i)}) = \begin{cases} -\log\left(f_{\vec{w}, b}(\vec{X}^{(i)})\right), & \text{if } y^{(i)} = 1, \\ -\log\left(1 - f_{\vec{w}, b}(\vec{X}^{(i)})\right), & \text{if } y^{(i)} = 0. \end{cases}

Because we’re still working on a binary classification problem, y is either zero or one. Because y is either zero or one and cannot take on any value other than zero or one, we’ll be able to come up with a simpler way to write this loss function. You can write the loss function as follows:

L(fw⃗,b(X⃗(i)),y(i))=−y(i)log⁡(fw⃗,b(X⃗(i)))−(1−y(i))log⁡(1−fw⃗,b(X⃗(i)))L(f_{\vec{w}, b} (\vec{X}^{(i)}),y^{(i)}) = -y^{(i)} \log(f_{\vec{w}, b}(\vec{X}^{(i)})) - (1-y^{(i)}) \log (1 - f_{\vec{w}, b} (\vec{X}^{(i)}))

Test for y = 1

if y(i)=1y^{(i)} = 1:

L(fw⃗,b(X⃗(i)),y(i))=−y(i)log⁡(fw⃗,b(X⃗(i)))L(f_{\vec{w}, b} (\vec{X}^{(i)}),y^{(i)}) = -y^{(i)} \log(f_{\vec{w}, b}(\vec{X}^{(i)}))

Test for y = 0

if y(i)=0y^{(i)} = 0:

L(fw⃗,b(X⃗(i)),y(i))=−(1−y(i))log⁡(1−fw⃗,b(X⃗(i)))L(f_{\vec{w}, b} (\vec{X}^{(i)}),y^{(i)}) = - (1-y^{(i)}) \log (1 - f_{\vec{w}, b} (\vec{X}^{(i)}))

What you see is that whether y is one or zero, this single expression here is equivalent to the more complex expression up here, which is why this gives us a simpler way to write the loss with just one equation without separating out these two cases, like we did on top.

Let’s Write Cost Function

Loss Function:

L(fw⃗,b(X⃗(i)),y(i))=−y(i)log⁡(fw⃗,b(X⃗(i)))−(1−y(i))log⁡(1−fw⃗,b(X⃗(i)))L(f_{\vec{w}, b} (\vec{X}^{(i)}),y^{(i)}) = -y^{(i)} \log(f_{\vec{w}, b}(\vec{X}^{(i)})) - (1-y^{(i)}) \log (1 - f_{\vec{w}, b} (\vec{X}^{(i)}))

Cost Function:

J(w⃗,b)=1m∑i=1m[L(fw⃗,b(X⃗(i)),y(i))]J(\vec{w}, b) = \frac{1}{m} \sum_{i = 1}^{m} [L(f_{\vec{w}, b} (\vec{X}^{(i)}), y^{(i)})]

Therefore, the Cost Function for Logistic Regression is:

J(w⃗,b)=−1m∑i=1my(i)log⁡(fw⃗,b(X⃗(i)))+(1−y(i))log⁡(1−fw⃗,b(X⃗(i)))J(\vec{w}, b) = -\frac{1}{m} \sum_{i = 1}^{m} y^{(i)} \log(f_{\vec{w}, b}(\vec{X}^{(i)})) + (1-y^{(i)}) \log (1 - f_{\vec{w}, b} (\vec{X}^{(i)}))

This is the cost function that pretty much everyone uses.

I’d just like to mention that this particular cost function is derived from statistics using a statistical principle called maximum likelihood estimation, which is an idea from statistics on how to efficiently find parameters for different models.

NOTE:

  1. Loss is calculated per training example

  2. Cost is calculated over all training examples

Gradient Descent for Logistic Regression

To fit the parameters of a logistic regression model, we’re going to try to find the values of the parameters w and b that minimize the cost function J of w and b, and we’ll again apply gradient descent to do this.

Here is the Cost Function:

J(w⃗,b)=−1m∑i=1my(i)log⁡(fw⃗,b(X⃗(i)))+(1−y(i))log⁡(1−fw⃗,b(X⃗(i)))J(\vec{w}, b) = -\frac{1}{m} \sum_{i = 1}^{m} y^{(i)} \log(f_{\vec{w}, b}(\vec{X}^{(i)})) + (1-y^{(i)}) \log (1 - f_{\vec{w}, b} (\vec{X}^{(i)}))

To minimize the cost function, we use gradient descent:

repeat {wj=wj−α∂∂wjJ(w⃗,b)b=b−α∂∂bJ(w⃗,b)} Simultaneous Updates\begin{aligned} &\text{repeat \{} \\ &w_{j} = w_{j} - \alpha \frac{\partial}{\partial w_{j}} J(\vec{w}, b) \\ &b = b - \alpha \frac{\partial}{\partial b} J(\vec{w}, b) \\ &\} \text{ Simultaneous Updates} \end{aligned}

for j=1...nj = 1 ... n; where nn is the number of features.

if we solve the partial derivatives:

∂∂wjJ(w⃗,b)=1m∑i=1m(fw⃗,b(X⃗(i))−y(i))xj(i)\frac{\partial}{\partial w_{j}} J(\vec{w}, b) = \frac{1}{m} \sum_{i = 1}^{m} (f_{\vec{w}, b} (\vec{X}^{(i)}) -y^{(i)}) x_{j}^{(i)}

and

∂∂bJ(w⃗,b)=1m∑i=1m(fw⃗,b(X⃗(i))−y(i))\frac{\partial}{\partial b} J(\vec{w}, b) = \frac{1}{m} \sum_{i = 1}^{m} (f_{\vec{w}, b} (\vec{X}^{(i)}) -y^{(i)})

NOTE: use simultaneous updates, meaning that you first compute the right-hand side for all of these updates and then simultaneously overwrite all the values on the left at the same time.

Gradient Descent For Logistic Regression

repeat {wj=wj−α[1m∑i=1m(fw⃗,b(X⃗(i))−y(i))xj(i)]b=b−α[1m∑i=1m(fw⃗,b(X⃗(i))−y(i))]} Simultaneous Updates\begin{aligned} &\text{repeat \{} \\ &w_{j} = w_{j} - \alpha \left[ \frac{1}{m} \sum_{i = 1}^{m} (f_{\vec{w}, b} (\vec{X}^{(i)}) - y^{(i)}) x_{j}^{(i)} \right] \\ &b = b - \alpha \left[ \frac{1}{m} \sum_{i = 1}^{m} (f_{\vec{w}, b} (\vec{X}^{(i)}) - y^{(i)}) \right] \\ &\} \text{ Simultaneous Updates} \end{aligned}

Now, one funny thing you might be wondering is, that’s weird. These two equations look exactly like the ones we had come up with previously for linear regression so you might be wondering, is linear regression actually secretly the same as logistic regression?

Same Concepts as Linear Regression

  1. Monitor Gradient Descent (Learning Curve)

  2. Vectorized Implementation

  3. Feature Scaling

    • Feature scaling applied the same way to scale the different features to take on similar ranges of values can also speed up gradient descent for logistic regression.