Plot decision boundary in Logistic regression in Python

Introduction: Whenever we plot a graph of a machine learning model, we can see there are multiple classes available. The decision boundary divides these classes with a line and that line is the decision boundary. It separates different classes with their labels. We can see them in SVM models, KNN models and also in logistic regression. This blog is defining what is decision boundary and How to plot it in the model.

Decision boundary: Well, whenever we define two classes together in a single plot we can differentiate them by a boundary, this boundary is just a line that defines the existing difference between those classes. We use them in many machine learning algorithms but in this blog, I am explaining it with logistic regression. We use Matplotlib to plot these types of plots and we plot them after everything is done means when we actually get the cost function, accuracy, gradient and all those things which we apply in logistic regression.

Let us see the coding part for this:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

#After train test split and prediction part we can apply this code to see the plotting two diffrent classes in one diagram with a sepration line which is boundary line
x_train = [np.min(X[:, 3] - 4), np.max(X[:, 5] + 6)]
y_train = - (parameters[0] +[1], x_values)) / parameters[2]

plt.plot(x_train, y_train, label='Boundary')
plt.xlabel('Those whowant to stay on app')
plt.ylabel('Those who want to delete the account')


Plot decision boundary in Logistic regression in Python

In this code:

  • After importing the libraries, first I am explaining some particular columns which I am taking for plotting
  • The same thing with y_train but here we are dividing them with the parameter for the decision boundary
  • Then I am plotting the boundary with both the classes.
  • And then I am plotting the graph.


Sometimes multiple classes can be a problem while plotting. But this will be not useful if classes are not properly differentiated. For getting the best results you need clean data and good prediction so you can perform better.

Leave a Reply

Your email address will not be published. Required fields are marked *