## Machine Learning

## A detailed explanation of the algorithm together with useful examples on how to build a model in Python

*Just so you know what you are getting into, this is a **long story** that contains a visual and a mathematical explanation of logistic regression with 4 different Python examples. Please take a look at the **list of topics below** and feel free to jump to the sections that you are most interested in.*

Machine Learning is making huge leaps forward, with an increasing number of algorithms enabling us to solve complex real-world problems.

This story is part of a deep dive series explaining the mechanics of Machine Learning algorithms. In addition to giving you an understanding of how ML algorithms work, it also provides you with Python examples to build your own ML models.

- The
**category**of algorithms logistic regression belongs to - An
**explanation**of how logistic regression works **Python examples**of how to build logistic regression models, including:

– Binary target with 1 independent variable

– Binary target with 2 independent variables

– Multinomial with 3 class labels and 2 independent variables

– Multinomial with 3 class labels and 2 independent variables + oversampling

Looking at the below chart’s supervised learning branch, we can see that we have two main categories of problems: regression and classification.

**Regression**: we use regression algorithms when we have a continuous (numerical) target variable. For example, predicting the price of a house based on its proximity to major amenities.**Classification**: used when the target variable is categorical. For example, predicting a win/loss of a game or customer defaulting/not-defaulting on a loan payment. Note, it does not necessarily have to be a binary outcome.

While logistic regression has a “regression” in its name, it actually belongs to the classification algorithms. However, there are some similarities between linear regression and logistic regression, which we will touch upon in the next section.

Let’s begin the explanation by looking at the following example.

Assume we have a class of 10 pupils where each of them had to take an exam. Their preparation time, final score, and outcome (pass/fail) are displayed below. *Note, the passing score is 40.*