Adventures in Machine Learning

Plotting Logistic Regression Curves in Python: A Step-by-Step Guide

Logistic regression is a popular statistical technique that is used to model the relationships between predictor variables and a binary response variable. Whether you are analyzing medical data to predict the likelihood of a patient developing a certain condition or predicting credit risk for a loan application, logistic regression can come in handy.

This article will introduce you to plotting a logistic regression curve in Python using the default dataset as an example.

Loading and Viewing the Dataset

The default dataset is a classic dataset in the field of statistical learning that has 10,000 observations and five variables, including default (which is the response variable), student, balance, income, and default payment next month. To load and view the dataset in Python, you can use the pandas library, as follows:

“`

import pandas as pd

default = pd.read_csv(“default.csv”)

print(default.head())

“`

The head() function is used to display the first few rows of the dataset, which will give you an idea of what the data looks like and help you understand the structure. Building a Logistic Regression Model that Uses “Balance” to Predict the Probability of Defaulting

Now that the dataset is loaded and viewed, we can build a logistic regression model to predict the probability of defaulting based on the balance variable.

The balance variable represents the average balance that the individual carries on their credit card. To build the logistic regression model, we will use scikit-learn, which is a popular machine learning library in Python.

Here is the code to build the model:

“`

from sklearn.linear_model import LogisticRegression

from sklearn.model_selection import train_test_split

# Define the predictor and response variables

X = default[[‘balance’]]

y = default[‘default’]

# Split the data into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

# Create a logistic regression object and fit the model to the data

log_reg = LogisticRegression()

log_reg.fit(X_train, y_train)

“`

The code above defines the predictor and response variables (balance and default, respectively), splits the data into training and testing sets, creates a logistic regression object, and then fits the model to the data.

Syntax for Plotting a Logistic Regression Curve in Python

Now that we have created a logistic regression model, we can plot the logistic regression curve to visualize the probability of defaulting as a function of the average balance. To plot the logistic regression curve, we will use the regplot() function from the seaborn data visualization library.

Here is the code to create the plot:

“`

import seaborn as sns

import matplotlib.pyplot as plt

sns.set_style(“whitegrid”)

# Create a new figure and set the size

plt.figure(figsize=(8, 6))

# Plot the logistic regression curve

sns.regplot(x=X[‘balance’], y=y, logistic=True, ci=None, scatter_kws={‘s’: 10})

# Add labels and a title

plt.xlabel(“Average Balance”)

plt.ylabel(“Probability of Defaulting”)

plt.title(“Logistic Regression Curve”)

plt.show()

“`

The code above imports the seaborn and matplotlib.pyplot libraries, sets the style for the plot, creates a new figure, plots the logistic regression curve using the regplot() function, and adds labels and a title.

Conclusion

In this article, we have learned how to plot a logistic regression curve in Python using the default dataset as an example. We loaded and viewed the dataset using the pandas library, built a logistic regression model using scikit-learn, and plotted the logistic regression curve using the regplot() function from the seaborn data visualization library.

By following the syntax and example code provided in this article, you can create your own logistic regression curve and visualize the relationships between predictor variables and a binary response variable. Example: Plotting a Logistic Regression Curve in Python

In the previous section, we learned about the steps to plot a logistic regression curve in Python.

In this section, we will go through an example of creating a plot with balance as the predictor variable and the predicted probability of defaulting on the y-axis using the default dataset from theto Statistical Learning book. We will start by loading the default dataset using the pandas library, as follows:

“`

import pandas as pd

df = pd.read_csv(‘https://raw.githubusercontent.com/mGalarnyk/Python_Tutorials/master/Statistics/creditcard.csv’)

print(df.head())

“`

The first few rows of the dataset are displayed, which show the columns “credit_use”, “student”, “balance”, “income”, and “default”. We will use “balance” as the predictor variable and “default” as the response variable in our logistic regression model.

Next, we will build a logistic regression model using scikit-learn, as follows:

“`

from sklearn.linear_model import LogisticRegression

from sklearn.model_selection import train_test_split

# Define the predictor and response variables

X = df[[‘balance’]]

y = df[‘default’]

# Split the data into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1)

# Create a logistic regression object and fit the model to the data

logreg = LogisticRegression()

logreg.fit(X_train, y_train)

“`

The logistic regression model is built and fitted to the data. Now we can use the model to predict the probability of defaulting for a given balance value.

To create the plot with balance as the predictor variable and the predicted probability of defaulting on the y-axis, we will use the regplot() function from the seaborn data visualization library, as follows:

“`

import seaborn as sns

import matplotlib.pyplot as plt

# Set the style of the plot

sns.set_style(“whitegrid”)

# Create a new figure and set the size

plt.figure(figsize=(8, 6))

# Plot the logistic regression curve

sns.regplot(x=X[‘balance’], y=y, logistic=True, ci=None, scatter_kws={‘s’: 10})

# Add labels and a title

plt.xlabel(“Balance”)

plt.ylabel(“Probability of Defaulting”)

plt.title(“Logistic Regression Curve”)

plt.show()

“`

The plot shows that as balance increases, the probability of defaulting also increases. For example, with a balance of 5000, the predicted probability of defaulting is around 20%, while with a balance of 2000, the predicted probability of defaulting is around 8%.

Customization of the Logistic Regression Curve Plot in Python

We can customize the logistic regression curve plot in Python by modifying the colors of the points and curve. This can be achieved using the scatter_kws and line_kws arguments of the regplot() function.

The scatter_kws argument allows us to modify the properties of the scatter points, such as size and color. For example, to change the color of the points to red and increase their size, we can use the following code:

“`

sns.regplot(x=X[‘balance’], y=y, logistic=True, ci=None,

scatter_kws={‘s’: 50, ‘facecolors’: ‘r’, ‘edgecolors’: ‘none’})

“`

The line_kws argument allows us to modify the properties of the logistic regression curve, such as color and line style.

For example, to change the color of the curve to green and make it dashed, we can use the following code:

“`

sns.regplot(x=X[‘balance’], y=y, logistic=True, ci=None,

scatter_kws={‘s’: 50, ‘facecolors’: ‘r’, ‘edgecolors’: ‘none’},

line_kws={‘color’: ‘g’, ‘linestyle’: ‘–‘, ‘lw’: 2})

“`

Remember, these are just examples, and you should feel free to use whichever colors you prefer to customize the plot to your liking.

Conclusion

In this article, we went through an example of creating a logistic regression curve plot in Python using the default dataset from theto Statistical Learning book. We also learned how to customize the plot by modifying the colors of the points and the curve.

By following the example code provided in this article, you can build your own logistic regression curve plots and customize them to your liking.

Additional Resources

If you are interested in learning more about logistic regression and plotting curves in Python, here are some additional resources that you may find useful. 1.to Statistical Learning: With Applications in R

This book provides an excellent introduction to statistical learning, including logistic regression, and is written for anyone who wants to learn about statistical methods from a practical perspective.

It covers key concepts in data science and machine learning, with examples and exercises in R. The book is available for free online or in print.

2. Python Data Science Handbook

This book is a comprehensive guide to data science with Python, including statistical modeling, machine learning, and data visualization.

It covers logistic regression and its application to classification problems, with examples and code snippets in Python. The book is available for free online or in print.

3. Kaggle

Kaggle is a popular data science platform that hosts competitions, datasets, and tutorials.

It is a great resource for learning about machine learning algorithms and techniques, including logistic regression, and for practicing your skills by participating in competitions or working on real-world projects. 4.

Seaborn Documentation

Seaborn is a popular data visualization library in Python that provides many powerful visualization functions, including the regplot() function for plotting logistic regression curves. The seaborn documentation provides detailed information about the library, including examples and code snippets for various types of plots and customization options.

5. scikit-learn Documentation

Scikit-learn is a popular machine learning library in Python that provides many powerful algorithms, including logistic regression, for data science and machine learning tasks.

The scikit-learn documentation provides detailed information about the library, including examples and code snippets for various types of models and customization options. 6.

YouTube Tutorials

There are many YouTube tutorials available on logistic regression and its implementation in Python. These tutorials are often aimed at beginners and cover the basics of logistic regression, as well as how to implement it in Python using various libraries, including scikit-learn and seaborn.

7. Online Courses

There are many online courses available on platforms such as Coursera, Udacity and Udemy, which cover logistic regression and its implementation in Python.

These courses are usually structured in modules, with examples, exercises, quizzes and a certification after completion.

Conclusion

Logistic regression is a fundamental statistical technique for modeling the relationships between predictor variables and a binary response variable. It is commonly used in many fields like finance, medicine, and marketing for prediction and classification problems.

In this article, we have learned how to plot logistic regression curves in Python using the default dataset as an example, customize the plot with different colors and delved into resources for further exploration. By following the examples and resources provided, you can expand your knowledge of logistic regression and its implementation in Python to become well-versed in this powerful machine learning technique.

In this article, we explored how to plot a logistic regression curve in Python using the default dataset as an example. We learned how to load and view the dataset, build a logistic regression model, and plot the logistic regression curve using the regplot() function from the seaborn library.

We also discussed customizations to the plot using the scatter_kws and line_kws arguments and provided additional resources for further learning and exploration. Logistic regression is a powerful technique used for prediction and classification problems in many fields, and learning how to plot a logistic regression curve in Python is an essential skill for any data scientist or machine learning practitioner.

Popular Posts