How to Plot a Logistic Regression Curve in Python

Logistic regression is a statistical model used for binary classification problems, where the outcome can only take two possible values, such as 0 or 1. It is a type of regression analysis that uses a logistic function to model the relationship between the independent variables and the probability of the dependent variable being 1. In this article, we will discuss the underlying statistical concept of logistic regression and learn how to plot the logistic regression curve in Python using the scikit-learn library.

Plotting the Logistic Regression Curve using Scikit-Learn in Python

Scikit-learn is a popular machine learning library in Python. It provides a simple and easy-to-use interface for logistic regression modeling and visualization.

Let’s see how to plot the logistic regression curve using scikit-learn.

Step 1: Import the Required Libraries

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

Step 2: Create a Design Matrix

The design matrix, also known as the predictor matrix, is a matrix of input values. In our case, we will create a simple design matrix with one input variable.

x = np.linspace(-5, 5, 1000)
X = np.column_stack((np.ones(len(x)), x))

Step 3: Fit the Logistic Regression Model

Fit the logistic regression model to the design matrix using scikit-learn.

clf = LogisticRegression(max_iter=10000, random_state=0).fit(X, np.zeros(len(x)))

Step 4: Generate the Probabilities

Use the fitted model to generate the probabilities for the input values.

y_prob = clf.predict_proba(X)

Step 5: Plot the Logistic Regression Curve

Finally, plot the logistic regression curve using matplotlib.

plt.plot(x, y_prob[:, 1], 'r-')
plt.axhline(y=0.5, color='k', linestyle='--')
plt.xlabel('Input values')
plt.ylabel('Probability of positive class')
plt.title('Logistic Regression Curve')
plt.show()

The resulting plot shows the logistic regression curve, also known as the sigmoid curve. The curve shows how the probability of the positive class changes as the input values change.

References

1. Logistic Regression in Scikit-Learn

2. Logistic Regression on Wikipedia

Leave a Reply

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