Visualize Regression Models with Seaborn

Related Articles

In this article, I will explain how to illustrate models of regression with Seaborn. First, let’s first understand regression models.

Regression models

In order to find the relationship between two variables, we create a regression model. Basically, regression analysis or regression model is a predictive modeling technique in which we have an independent variable and a dependent variable. Regression analysis tells us how the dependent variable takes its values ​​according to the independent variable. When we plot the values ​​that the two variables assume, we get a regression line. In other words, we get the best match line that passes through the plotted data points of two variables so that the distance between the line and points is minimized.

Seaborn Library of Python

In particular, the Seaborn library offers various drawing functions that work on data frames.

Python program to illustrate regression models with Seaborn

The following python program demonstrates two regression plots. First, we need to import the Sea Born library. Next, we read the data file. In addition, we remove the rows with missing values ​​using the dropna () function. While the regplot () function plots the regression model. It takes the variables x, and y, and the data frame as input. also, Order = 2, Indicates a polynomial regression. Similarly, logistic = true represents logistic regression.

Furthermore, you can download the stroke_data.csv data file from here.

import seaborn as sb
from matplotlib import pyplot as plt
import pandas as pd



#Handling Missing values

sb.regplot(x="age", y="bmi", data=df2,
                 order=2, ci=None, scatter=None)
plt.title("Polynomial Regression")

sb.regplot(x="bmi", y="stroke", data=df2,
                 logistic=True, n_boot=500, y_jitter=.03)
plt.title("Logistic Regression")


The output of the Python program to illustrate regression models with Seaborn

Drawing the polynomial regression between bmi and age

In fact, the polynomial regression is a variation of the linear regression in which a polynomial of nGod’ A degree describes the relationship between the independent variable and the dependent variable rather than a straight line.

Drawing the polynomial regression
Drawing the polynomial regression

As can be seen in the figure above, BMI (body mass index) increases with age. However, after reaching its maximum value in the range [40-50], He begins to descend again. Therefore, we can use a polynomial regression chart to represent this relationship.

Planning the logistic regression between stroke and BMI

When we have a dependent variable that takes discrete values, we can use logistic regression. The following illustration shows an example of logistical regression. In fact, the bmi variable takes continuous values. While the variable stroke is discrete.

    Logistics regression planning
Logistics regression planning



Please enter your comment!
Please enter your name here

Popular Articles