Machine Learning Series: Logistic Regression


Image by S. Hermann & F. Richter from Pixabay


Hello Reader,

 

In this post I will describe an algorithm used to estimate the probability that an instance belongs to one of a discrete number of classes (e.g. An email is spam or not).

 

How logistic regression estimates the probabilities? As linear regression calculated the weighted sum of the feature vector x with the parameter vector θ but instead of output the value directly it passes it through a function called the logistic function.



The character above p is called "hat". p hat represents the probability predicted by the model.

 

The logistic function is also known as sigmoid. Below the formula of the logistic function and its graph.




The simplest classifier is when the model needs to chose between two classes, in this case we are considering a binary classifier. The output "y hat" of the model will be based on a function that takes the predicted probability and compares it with a threshold to decide if an instance belongs or not to the class the model is looking for. The default threshold is 50% probability.



As for linear regression to allow our algorithm to learn during the training phase, we need to have a way to give feedbacks on how bad or good a prediction is, in the case of logistic regression the cost function should estimate high probability when y=1 and low probability when y=0. Below the cost function for logistic regression





The cost function can be applied to a single instance, once we apply it to the entire training set we can write it in one formula called log loss




For the log loss, we do not have a closed-form to minimize the parameters θ ( as the normal form), in order to learn the algorithm we need to use another technique called gradient descent, as we will see in the next post.

 

Now that we saw the math behind logistic regression let's see it in practice. First a couple of words on the data, we are going to use a dataset part of the Sklearn library, the dataset contains 150 samples of three species of flower: Iris Setosa, Iris Versicolor, and Iris Virgica. For each flower, we have four features sepal and petal width and length. 



As long as we want to train a binary classifier we need to consider just two classes and to be able to plot the instances on a 2D graph we keep two features 



Let's plot the dataset, In the scatter plot below each dot represent a flower the position on the plot depends by two feature sepal length and sepal width, the dots colored in green are labeled as Iris Virginica and are the ones our classifier will be trained to predict



Now we can train our logistic regression classifier. The algorithm will optimize the vector parameter θ to minimize the log loss based on the training labels.




Once trained the algorithm can predict the probability that an instance is an Iris Virginica or not. The decision boundary is set at 50% probability, if the probability is greater or equal to 50% the model predicts that the instance is an Iris Veronica. The below plot shows the contours of the predicted probability, the more we enter the area where the Iris Virginica is denser the more the probability increases. The black dotted line in the middle represents the decision boundary (50% probability)


The model accuracy on the dataset is 95%. Let's train a multi-class classifier



We have two ways to predict the class, the default behavior of the Sklearn library is the "one vs all" (OvA) approach. In the OvA approach, the algorithm predicts the probability of each class as a binary classifier, compares the prediction of each class, and choose the one with the highest probability. Another way to predict multiple classes is a generalization of logistic regression called SoftMax regression.

 

The first step is to calculate the score for each class



he equation below represents the score for a class k. In SoftMax Regression each class has its parameter vector. 

The second step is to calculate the probability of the class k using the SoftMax function


The function calculates the exponential of the score of the class k and divides it by the sum of the exponential of all classes.

 

The model prediction is the class with the highest probability. Note that Softmax is used to predict one class at time and it should be used when the classes are mutually exclusive.

 

The cost used in the SoftMax problem is called Cross-Entropy


  • y(k) is the expected probability for the class k, it is 1 in case the training sample represents the class that we are trying to predict
  • p(k) hat is the predicted probability

 

Let's train the model to predict multiple classes




below a chart that highlights the probability contours for the Iris Versicolor, the probability 0.5 is the decision boundary



Recommended resources 

Notebook:

Course:  

Books:


Comments