Logistic regression is a simple classification method which is widely used in the field of machine learning. Today we’re going to talk about how to train our own logistic regression model in Python to build a a binary classifier. We’ll use NumPy for matrix operations, SciPy for cost minimization, Matplotlib for data visualization and no machine learning tools or libraries whatsoever.

#### Problem & Dataset

We’ll be looking at another assignment from the machine learning course taught by Andrew Ng. Our objective in this problem is to estimate an applicant’s probability of admission into a university based on his/her results on two exams. Our dataset contains some historical data from previous applicants, which we’ll use as a training sample. Let’s read the dataset file and take a look at the first few examples:

[[ 34.62365962  78.02469282   0.        ]
[ 30.28671077  43.89499752   0.        ]
[ 35.84740877  72.90219803   0.        ]
[ 60.18259939  86.3085521    1.        ]
[ 79.03273605  75.34437644   1.        ]]


The first two columns correspond to the exam scores and the third column indicates whether the applicant has been admitted to the university. We can visualize this data using a scatter plot:

#### Hypothesis Function

Logistic regression is a linear model, which means that the decision boundary has to be a straight line. This can be achieved with a a simple hypothesis function in the following form:

$h_\theta(x) = g(\theta^Tx)$

where $g$ is the sigmoid function which is defined as:

$g(z) = \dfrac{1}{1 + e^{-z}}$

Here’s the Python version of the sigmoid function:

The numeric output of the hypothesis function $h_\theta(x)$ corresponds to the model’s confidence in labeling the input:

• If the output is $0.5$, both classes are equally probable as far as the classifier is concerned.
• If the output is $1$, the classifier is 100% confident about class 1.
• If the output is $0$, the classifier is 100% confident about class 0.

In other words, the classifier labels the input based on whether $\theta^Tx$ is positive or negative. Of course this is based on the assumption that the treshold is selected as $0.5$.

#### Cost Function

In the training stage, we’ll try to minimize the cost function below:

$J(\theta) = \dfrac{1}{m}\sum\limits_{i=1}^{m}[-y^{(i)}log(h_\theta(x^{(i)})) - (1 - y^{(i)})log(1 - h_\theta(x^{(i)}))]$

Notice that this cost function penalizes the hypothesis according to its probability estimation error, as demonstrated below:

We can use the following Python function to compute this cost in our script:

#### Optimization Algorithm

We’re going to use the fmin_cg function from scipy.optimize to minimize our cost. fmin_cg needs the gradient of the cost function as well as the cost function itself. Here’s the formula for the cost gradient:

$\dfrac{\partial{J(\theta)}}{\partial{\theta_j}} = \dfrac{1}{m}\sum\limits_{i=1}^{m}(h_\theta(x^{(i)}) - y^{(i)})x^{(i)}_j$

where $m$ is the number of training examples.

And the Python version is:

If you’re not familiar with fmin_cg, it might be a good idea to check out my previous post and/or the official docs before proceeding to the next section.

#### Training Stage

The final thing we need to do before training our model is to add an additional first column to $x$, in order to account for the intercept term $\theta_0$:

Finally we can train our logistic regression model:

Optimization terminated successfully.
Current function value: 0.203498
Iterations: 51
Function evaluations: 122


Now let’s plot the decision boundary of our optimized model:

Looks pretty good. Let’s also measure the training accuracy:

Training Accuracy = 89.0%


Not bad at all. We would probably need something little more flexible than logistic regression if we wanted to come up with a more accurate classifier.

#### Prediction

Finally, here’s how we can predict the probability of admission for a student with arbitrary exam scores:

For a student with scores 45 and 85, we predict an admission probability of 0.776195474168


If you’re still here, you should subscribe to get updates on my future articles. Also feel free to leave a comment below and let me know what you think!