绘制回归模型

Python 中的广义线性模型

Ita Cirovic Donev

Data Science Consultant

导入库

import seaborn as sns
import matplotlib.pyplot as plt
  • 蟹模型 'sat ~ width' 已保存为 model
Python 中的广义线性模型

绘制数据点

# Adjust figure size
plt.subplots(figsize = (8, 5))
# Plot data points
sns.regplot('width', 'sat', 
            data = crab,
            fit_reg = False)

蟹数据集中宽度与卫星数的散点图。

Python 中的广义线性模型

添加抖动

sns.regplot('width', 'sat', 
            data = crab,
            fit_reg = False,
            y_jitter = 0.3)

在蟹数据集中为宽度与卫星数的散点图添加抖动。

Python 中的广义线性模型

添加线性拟合

sns.regplot('width', 'sat', 
            data = crab,
            y_jitter = 0.3,
            fit_reg = True,
            line_kws = {'color':'green', 
                        'label':'LM fit'})

线性拟合(含置信区间)与蟹数据集中宽度和卫星数的散点图。

Python 中的广义线性模型

添加 Poisson GLM 估计值

crab['fit_values'] = model.fittedvalues
sns.scatterplot('width','fit_values', 
                data = crab,
                color = 'red', 
                label = 'Poisson')

蟹数据集中宽度与卫星数的散点图,以及线性与 Poisson 拟合。

Python 中的广义线性模型

预测

Poisson 模型拟合叠加在宽度与卫星数散点图上

Python 中的广义线性模型

预测

new_data = pd.DataFrame({'width':[24, 28, 32]})
model.predict(new_data)
0    1.881981

基于拟合的 Poisson 回归,在宽度 24cm 处读取预测值。

Python 中的广义线性模型

预测

new_data = pd.DataFrame({'width':[24, 28, 32]})
model.predict(new_data)
0    1.881981
1    3.627360

基于拟合的 Poisson 回归,在宽度 28cm 处读取预测值。

Python 中的广义线性模型

预测

new_data = pd.DataFrame({'width':[24, 28, 32]})
model.predict(new_data)
0    1.881981
1    3.627360
2    6.991433

基于拟合的 Poisson 回归,在宽度 32cm 处读取预测值。

Python 中的广义线性模型

Passons à la pratique !

Python 中的广义线性模型

Preparing Video For Download...