Logistic Regression
What Is Logistic Regression?
Logistic regression is a supervised classification algorithm, used mostly for binary classification. It predicts the probability that an outcome belongs to a certain class.
For example, suppose we know whether a person smokes, their age, and their blood pressure and cholesterol. From this information, we want to predict whether the person could have heart disease. This is a binary classification problem.
Logistic regression can be used for many other classification problems:
- Medical diagnosis: predicting whether a patient has a disease, such as diabetes, from symptoms, test results, or genetic data
- Credit scoring: predicting whether a person will default on a loan, based on prior data
- Customer behavior: predicting whether a customer will buy a product from their age, income, and past purchases, or whether they will click on it
The Problem
In this lesson, we predict heart disease from age, blood pressure, cholesterol, and whether someone is a smoker. Our labeled training dataset has samples:
| Age | Blood Pressure | Cholesterol | Smoker | Heart Disease |
|---|---|---|---|---|
| 45 | 130 | 230 | 1 | 1 |
| 54 | 140 | 250 | 0 | 1 |
| 62 | 150 | 240 | 1 | 1 |
| 50 | 120 | 210 | 0 | 0 |
| 38 | 110 | 190 | 0 | 0 |
| 65 | 155 | 260 | 1 | 1 |
| 59 | 145 | 255 | 1 | 1 |
| 48 | 125 | 200 | 0 | 0 |
| 53 | 135 | 245 | 1 | 1 |
| 70 | 160 | 270 | 1 | 1 |
Each is the input, and each is the output label. The input is -dimensional. We have four attributes here, so :
We want a model that takes any input and predicts the output , which is either 1 (heart disease) or 0 (no heart disease).
The Bernoulli Distribution
Since the output is a yes or no answer, we need a Bernoulli distribution. It is used when there are exactly two possible outcomes, success or failure. We usually call success 1 and failure 0.
Here are some examples:
- Flipping a coin, where heads is 1 and tails is 0
- Whether an email is spam or not
- Whether a customer buys a product or not
- Whether something is present or absent
Let be the probability of success, which is always between 0 and 1. The probability of failure is then :
We can write both cases in a single formula:
This works because can only be 0 or 1:
- If , the formula becomes .
- If , the formula becomes .
From Linear Regression to Classification
Can we adapt linear regression to give us a class instead of a continuous value? This is the idea behind logistic regression:
- Use a linear regression model to predict a continuous value.
- Transform that continuous value into a probability of success, between 0 and 1.
- Treat a value below 0.5 as failure (0), and a value above 0.5 as success (1).
In linear regression, the predicted output was:
This can be any continuous value, but has to lie between 0 and 1. To convert it, we use the sigmoid function:
The Sigmoid Function
For any continuous value , the sigmoid function is:
Let's see how it behaves, using the fact that and :
- As , .
- As , .
So all sigmoid values lie between 0 and 1, with 0.5 as the midpoint. Half of the curve is below 0.5 and half is above it. This is why the sigmoid can convert any continuous value into a value between 0 and 1.
Prediction
For any input , the predicted probability of class 1 is , and the probability of class 0 is what's left over:
At test time, we get a class by thresholding:
- If is below 0.5, we predict class 0, the failure class.
- If is above 0.5, we predict class 1, the success class.
The Likelihood
The likelihood is the probability of getting the actual output for a given input. It is when , and when .
Using the same formula as the Bernoulli distribution, we can write both cases together:
As before, this works because is only 0 or 1:
- If , the first factor becomes 1, leaving .
The Loss Function
We want to maximize the likelihood. As in the maximum likelihood lesson, we take the log, which gives us this for sample :
Instead of maximizing the log-likelihood, we can minimize the negative log-likelihood:
This is the loss function for logistic regression, and it is also called the binary cross-entropy.
Why "Binary Cross-Entropy"?
For a binary random variable with probability , the entropy measures the randomness of the variable:
Now look at our loss function again, and let and . The loss then becomes:
This looks similar to entropy, but with two different terms, and , which come from separate Bernoulli distributions. That's why it is called binary cross-entropy.
The Derivative of the Sigmoid
To train the model, we use gradient descent, with the update rule from the gradient descent lesson:
We need the gradient of the loss, and the loss contains sigmoids. So first, let's find the derivative of the sigmoid:
We can split this into two fractions:
The first fraction is , and the second is . So:
The Gradient of the Loss
Over all samples, the loss to minimize is the average:
To shorten the next few lines, we'll write for .
Taking the gradient with respect to , we use the derivative of the log, the sigmoid derivative above, and the fact that the gradient of is :
The terms cancel in the first part, and the terms cancel in the second:
Expanding the brackets gives:
The two middle terms cancel, which leaves:
Moving the minus sign inside reverses the order. Since is the computed output, we can write it as :
So the gradient is built from the difference between the computed output and the actual output . We use this gradient in gradient descent to optimize the weights.
Summary
The key ideas are:
- Logistic regression predicts the probability of a class, and is mostly used for binary classification.
- It applies the sigmoid function to a linear model's output, giving a value between 0 and 1.
- A threshold of 0.5 turns that probability into a class.
- The likelihood comes from the Bernoulli distribution, and its negative log is the binary cross-entropy loss.
- The gradient is , and we use it to train the model with gradient descent.