Logistic regression in python

Tzung-Chien Hsieh
3 min readApr 10, 2022

--

In this article, I will introduce how to use logistic regression in python. As we know, logistic regression can be used for classification problems. For the binary classification, we will get the probabilities to class ‘0’ and to class ‘1’. Here I will introduce it by using the iris dataset from the scikit-learn library.

The source code used in this article can be found at https://github.com/la60312/machine_learning_tutorial/blob/main/logistic_regresion.ipynb

Load the iris dataset

We first load the iris dataset, and print out the features and target names. Here we have four features and three classes.

Filter data to one dimension feature

To visualize the logistic regression probability in a 2D figure with the binary classification, we only keep one feature ‘petal width’, and filter out the third class.

Train logistic regression

Train logistic regression with one-dimensional feature to classify ‘setosa’ and ‘versicolor’.

Plot probability

Here we can see that the decision boundary is around 0.7.

Classification with two features

Here we get two features from iris dataset, and perform the logistic regression. We further get the model’s parameter for drawing the decision boundary.

Plot decision boundary in two-dimensional space

We plot the data points in two-dimensional space (petal length and petal width). Here we can see that the two classes are perfectly separated by the decision boundary.

Summary

In this article, we can use logistic regression to perform the classification problem. We could also get the probability for each class and find out the decision boundary.

The source code used in this article can be found at https://github.com/la60312/machine_learning_tutorial/blob/main/logistic_regresion.ipynb

--

--

Tzung-Chien Hsieh

Computer science PhD student, I will share what I learned in Programming, Machine Learning, and Rare Disorders.