Visualize Graph of Logistic Regression in Python

A graph of logistic regression is known as Sigmoid Curve which is simply an S-shaped graph. Logistic regression is a supervised algorithm that is mostly used for classification purposes. The logistic model is best when we have a binary classification dataset. A binary classification dataset is a dataset where we have only two possible output values. In this short article, we will learn how to visualize a graph of logistic regression and how to understand it.

You may also like to visualize heatmaps and 3d graphs in Python.

Logistic Regression in Python

Before going to learn about the visualization of the graph of logistic regression, let us first understand what is logistic regression and how the model is trained. Logistic regression is a classification model that is used to classify an object based on a sigmoid function.


A sigmoid function is a mathematical function that is used to classify objects in the logistic regression model. The range of sigmoid function can be from 0 to 1.

We can visualize the sigmoid function or the graph of logistic regression in various ways. Here we will use the Matplotlib and Seaborn modules to visualize the graph.

Visualize Sigmoid Function in Python

We are now already familiar with the sigmoid function. First, we will import the required modules and then will create a function for the sigmoid and then will visualize it.

# Importing the modules
import numpy as np
import matplotlib.pyplot as plt

Once we imported the modules, the next step is to create a function that will help us to draw the sigmoid function.

# Generate x values
x = np.linspace(-10, 10, 100)

# Compute sigmoid values
y = sigmoid(x)

As we have the dataset ready, we will use the Matplotlib module to visualize the sigmoid function using the line chart.

plt.plot(x, y)
plt.title('Sigmoid Function')
plot sigmoid graph in python

Visualize Graph of Logistic Regression Using Seaborn

Another module that is used to visualize the graph of the logistic regression model is Seaborn. Here we will assume that you already have the dataset ready and you want to plot the graph. In the Seaborn module, the regplot() graph is used to plot the logistic regression graph as shown below:

# import the seaborn module
import seaborn as sns

#plot logistic regression graph
sns.regplot(x=x, y=y, data=data, logistic=True, ci=None)

Here, the x and y represent the input and output values of the dataset. After running the code, you will get a graph similar to this one:


Visualize Decision Boundry of Logistic Regression

Well, a decision boundary of the logistic regression is the threshold value that helps to classify the object into another category. For example, if the threshold value of the trained model is 0.4, then any value above 0.4 will be classified as 1. Sometimes, the decision boundary of logistic regression really helps us to see the trained model visually.

Let us first import all the required modules for the visualization and training of the logistic regression.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression

We will use the NumPy module to create the dataset, the Matplotlib module to visualize the decision boundary, and the sklearn module to train the logistic regression model.

Let us now create a very simple dataset for the training of the model. In your case, you can use the dataset that you already have or the trained model.

# Generate synthetic data
X = np.random.randn(200, 2)
y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0)

Once we have our dataset, we will use the Sklearn module to import the logistic regression model and then train the model on this dataset.

# Train logistic regression model
model = LogisticRegression(), y)

After the training process, it is time to specify the decision boundary.

# Plot the decision boundary
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
h = 0.02  
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

Now, we are almost done. We will now visualize the model based on the decision boundary that we just created.

plt.contourf(xx, yy, Z,, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=y,, edgecolors='k')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('Logistic Regression Decision Boundary')
decision boundary in logistic regression

As you can see, we were successful to visualize the decision boundary of the logistic regression.

Final Thoughts

In this short article, we discussed how to visualize the graph of logistic regression in Python using Matplotlib and Seabron modules. We learned to visualize graphs of logistic regression, sigmoid function, and the decision boundary of the trained model.

Leave a Comment

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

Scroll to Top