模型公式

Generalized Linear Models in Python

Ita Cirovic Donev

Data Science Consultant

公式與模型矩陣

圖示開頭,資料來源 X 與 Y。

Generalized Linear Models in Python

公式與模型矩陣

公式示意圖

Generalized Linear Models in Python

公式與模型矩陣

模型矩陣示意圖

Generalized Linear Models in Python

公式與模型矩陣

glm 類別輸入的示意圖。

Generalized Linear Models in 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
Generalized Linear Models in 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
Generalized Linear Models in 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
Generalized Linear Models in 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
Generalized Linear Models in 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
Generalized Linear Models in Python

類別資料編碼

顏色型別:red、green、blue

Generalized Linear Models in Python

類別資料編碼

顏色型別 red、green、blue 與資料中的觀測示意圖

Generalized Linear Models in Python

類別資料編碼

以 red、green、blue 做 one-hot 編碼的示意圖。

Generalized Linear Models in Python

Patsy 編碼

  • 字串與布林自動編碼
  • 數值 $\rightarrow$ 類別
    • C() 函式
  • 參考組別
    • 預設:第 1 組
    • Treatment
    • levels
Generalized Linear Models in 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
Generalized Linear Models in 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]
Generalized Linear Models in 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]
Generalized Linear Models in 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]
Generalized Linear Models in 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]
Generalized Linear Models in Python

一起來練習吧!

Generalized Linear Models in Python

Preparing Video For Download...