Logistic Regression is one of many supervised machine learning algorithms, just like Linear Regression.
Instead of predicting a continuous value, it predicts the probability of an event happening, or something is true or false.
So, this algorithms is mostly used for binary classification problems. Here are the use cases of Logistic Regression:
Predict if an email is spam or not.
Predict if a credit card transaction is fraudulent or not.
Predict if a customer will churn or not.
Predict if a patient has cancer or not.
However, it also has the same limitations as Linear Regression, such as:
It is a linear model, so it cannot capture complex non-linear relationships. In other words, it assumes that the data is linearly separable.
It assumes that the data is independent of each other. In other words, it assumes that the data is not correlated with each other.
It is sensitive to outliers.
Mathematics Behind Logistic Regression
First, Logistic Regression still uses Linear Equation used in Linear Regression, and it's expressed as:
y^=θ0+θ1x
Second, we are going to need Sigmoid function.
This funciton's sole job is to predict by converting the output of the linear equation into a probability value between 0 and 1.
σ(z)=1+e−z1
where z=θ0+θ1×x. Then the sigmoid function can be rewritten as:
σ(θ0+θ1x)=P(y∣x)=1+e−(θ0+θ1x)1
After acquiring the probability value from the Sigmoid function, we can use a threshold value to classify into one or another.
To generalize, we can use the following equation to predict the probability of an event occurring:
Once we have the probability of an event occuring, we then use a threshold value to round up the probability value to either 0 or 1.
If P(y∣x)≥0.5, then the data is classified as 1, otherwise it is classified as 0.
Similar to what we did in the Linear Regression post, we need to estimate the best θ0 and θ1 using the Gradient Descent algorithm.
What the Gradient Descent algorithm does is to update the θ0 and θ1 values based on the cost function and the learning rate.
This example is just a simple linear model, we are going to use the following equations to update intercept and coefficient:
First things first, we need to import the necessary libraries.
Say that we only have a feature x, the sepal length, and we want to determine the instance is a Virginica or not.
Let's prepare the data.
You might be wondering why we have is_virginica_dict.
I need that variable to separate the data so that some data sit at the bottom of the plot and some sit at the top of the plot.
Data points are either sitting at 0 or 1
So let's add a line like we did in the Linear Regression post with −1.2 as the intercept and 0.28 as the coefficient.
A graph with a Linear Regression line
It's clear that our data do not follow the pattern that the straight line is showing in the graph.
Thus, we need to use Sigmoid function to bend the line, so that the line would look like this:
A graph with a Logistic Regression line
Let's train our model for 100,000 times.
Not bad, the accuracy is 80%, with −13.00 as the intercept and 2.05 as the coefficient.
Logistic Regression is a supervised machine learning algorithm that is used for binary classification problems.
It uses the Sigmoid function to calculate the probability of an event occurring.
It uses a threshold value to roundup the probability value to either 0 or 1. If δ(x)>0.5, then the data is classified as 1, otherwise it is classified as 0.