计算并描述预测

Python 中的广义线性模型

Ita Cirovic Donev

Data Science Consultant

计算预测

在获得模型拟合后

  1. 原始 $x$ 的拟合值

模型拟合流程图。

Python 中的广义线性模型

计算预测

在获得模型拟合后

  1. 原始 $x$ 的拟合值

  2. 用于预测值 $x$ 值

模型拟合与预测流程图。

Python 中的广义线性模型

计算预测

  • 马蹄蟹模型 y ~ weight $$ \mu = \frac{\exp(-3.6947+1.8151 \times weight)}{1+\exp(-3.6947+1.8151 \times weight)} $$

  • 新测量:weight = 2.85

$$ \mu = \frac{\exp(-3.6947+1.8151 \times \color{blue}{2.85})}{1+\exp(-3.6947+1.8151 \times \color{blue}{2.85})} = 0.814 $$

Python 中的广义线性模型

Python 中的预测

  • 计算数据集 new_data 的模型预测
    # Compute model predictions
    model_GLM.predict(exog = new_data)
    
Python 中的广义线性模型

从概率到类别

给定概率阈值后模型输出的划分。

Python 中的广义线性模型

计算类别预测

# Extract fitted probabilities from model
crab['fitted'] = model.fittedvalues.values
# Define cut-off value
cut_off = 0.4
# Compute class predictions
crab['pred_class'] = np.where(crab['fitted'] > cut_off, 1, 0)
Python 中的广义线性模型

计算类别预测

# Count occurences for each class
crab['pred_class'].value_counts()
1    151
0     22
阈值 $\hat y=1$ $\hat y=0$
$\mu = 0.4$ 151 22
$\mu = 0.5$ 126 47
Python 中的广义线性模型

混淆矩阵

空的混淆矩阵。

Python 中的广义线性模型

混淆矩阵 - 真负类

混淆矩阵中的真负类。

Python 中的广义线性模型

混淆矩阵 - 真正类

混淆矩阵中的真负类和真正类。

Python 中的广义线性模型

混淆矩阵 - 假正类

混淆矩阵中的真负类、真正类与假正类。

Python 中的广义线性模型

混淆矩阵 - 假负类

混淆矩阵中的真负类、真正类、假正类与假负类。

Python 中的广义线性模型

Python 中的混淆矩阵

print(pd.crosstab(y_actual, y_predicted, 
            rownames=['Actual'], colnames=['Predicted'], 
            margins = True))
Predicted   0    1  All
Actual                 
0          15   47   62
1           7  104  111
All        22  151  173
Python 中的广义线性模型

Passons à la pratique !

Python 中的广义线性模型

Preparing Video For Download...