模型公式

Python 中的广义线性模型

Ita Cirovic Donev

Data Science Consultant

公式与模型矩阵

图示起点:数据源 X 和 Y。

Python 中的广义线性模型

公式与模型矩阵

公式示意图

Python 中的广义线性模型

公式与模型矩阵

模型矩阵示意图

Python 中的广义线性模型

公式与模型矩阵

glm 类输入的示意图。

Python 中的广义线性模型

模型矩阵

  • 模型矩阵:$y \sim \bold{X}$

  • 模型公式

    'y ~ x1 + x2'
    
  • 检查模型矩阵结构
    from patsy import dmatrix
    dmatrix('x1 + x2')
    
  Intercept  x1  x2
          1   1   4
          1   2   5
          1   3   6
Python 中的广义线性模型

变量变换

import numpy as np
'y ~ x1 + np.log(x2)'
dmatrix('x1 + np.log(x2)')
DesignMatrix with shape (3, 3)
  Intercept  x1  np.log(x2)
          1   1     1.38629
          1   2     1.60944
          1   3     1.79176
Python 中的广义线性模型

中心化与标准化

  • 有状态变换
'y ~ center(x1) + standardize(x2)'
dmatrix('center(x1) + standardize(x2)')
DesignMatrix with shape (3, 3)
  Intercept  center(x1)  standardize(x2)
          1          -1         -1.22474
          1           0          0.00000
          1           1          1.22474
Python 中的广义线性模型

自定义变换

def my_transformation(x):
  return 4 * x
dmatrix('x1 + x2 + my_transformation(x2)')
DesignMatrix with shape (3, 4)
  Intercept  x1  x2  my_transformation(x2)
          1   1   4                     16
          1   2   5                     20
          1   3   6                     24
Python 中的广义线性模型

算术运算

x1 = np.array([1, 2, 3])
x2 = np.array([4,5,6])

dmatrix('I(x1 + x2'))
DesignMatrix with shape (3, 2)
  Intercept  I(x1 + x2)
          1           5
          1           7
          1           9
x1 = [1, 2, 3]
x2 = [4,5,6]

dmatrix('I(x1 + x2)')
DesignMatrix with shape (6, 2)
  Intercept  I(x1 + x2)
          1           1
          1           2
          1           3
          1           4
          1           5
          1           6
Python 中的广义线性模型

类别型数据编码

颜色类型:red、green、blue

Python 中的广义线性模型

类别型数据编码

颜色类型与数据中的颜色观测示意图

Python 中的广义线性模型

类别型数据编码

使用 red、green、blue 进行独热编码的示意图。

Python 中的广义线性模型

Patsy 编码

  • 字符串和布尔值会自动编码
  • 数值 → 类别
    • 使用 C() 函数
  • 参考组
    • 默认:第一组
    • Treatment
    • levels
Python 中的广义线性模型

C() 函数

  • 数值变量
    dmatrix('color', data = crab)
    
DesignMatrix with shape (173, 2)
  Intercept  color
          1      2
          1      3
          1      1
  [... rows omitted]
  • 有多少水平?
    crab['color'].value_counts()
    
2    95
3    44
4    22
1    12
Python 中的广义线性模型

C() 函数

  • 类别变量
    dmatrix('C(color)', data = crab)
    
DesignMatrix with shape (173, 4)
  Intercept  C(color)[T.2]  C(color)[T.3]  C(color)[T.4]
          1              1              0              0
          1              0              1              0
          1              0              0              0
  [... rows omitted]
Python 中的广义线性模型

更改参考组

dmatrix('C(color, Treatment(4))', data = crab)
DesignMatrix with shape (173, 4)
  Intercept  C(color)[T.1]  C(color)[T.2]  C(color)[T.3]  
          1              0              1              0 
          1              0              0              1 
          1              1              0              0 
  [... rows omitted]
Python 中的广义线性模型

更改参考组

l = [1, 2, 3,4]
dmatrix('C(color, levels = l)', data = crab)
DesignMatrix with shape (173, 4)
  Intercept  C(color)[T.2]  C(color)[T.3]   C(color)[T.4] 
          1               1            0               0     
          1               0            1               0
          1               0            0               0 
  [... rows omitted]
Python 中的广义线性模型

多个截距

'y ~ C(color)-1'
dmatrix('C(color)-1', data = crab)
DesignMatrix with shape (173, 4)
  C(color)[1]  C(color)[2]  C(color)[3]  C(color)[4]
            0            1            0            0
            0            0            1            0
            1            0            0            0
  [... rows omitted]
Python 中的广义线性模型

Vamos praticar!

Python 中的广义线性模型

Preparing Video For Download...