สูตรโมเดล

Generalized Linear Models ใน Python

Ita Cirovic Donev

Data Science Consultant

สูตรและเมทริกซ์โมเดล

จุดเริ่มต้นของแผนภาพแสดงแหล่งข้อมูล X และ Y

Generalized Linear Models ใน Python

สูตรและเมทริกซ์โมเดล

แผนภาพสูตร

Generalized Linear Models ใน Python

สูตรและเมทริกซ์โมเดล

แผนภาพเมทริกซ์โมเดล

Generalized Linear Models ใน Python

สูตรและเมทริกซ์โมเดล

แผนภาพอินพุตของคลาส glm

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

การ centering และ standardization

  • Stateful transforms
'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 ใน 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 ใน 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 ใน Python

การเข้ารหัสข้อมูลประเภท

ประเภทสี: แดง เขียว น้ำเงิน

Generalized Linear Models ใน Python

การเข้ารหัสข้อมูลประเภท

แผนภาพประเภทสี: แดง เขียว น้ำเงิน และการสังเกตสีในข้อมูล

Generalized Linear Models ใน Python

การเข้ารหัสข้อมูลประเภท

แผนภาพ one-hot encoding โดยใช้สีแดง เขียว และน้ำเงิน

Generalized Linear Models ใน Python

การเข้ารหัสด้วย Patsy

  • String และ boolean ถูกเข้ารหัสอัตโนมัติ
  • ตัวเลข $\rightarrow$ ประเภท (categorical)
    • ฟังก์ชัน C()
  • กลุ่มอ้างอิง
    • ค่าเริ่มต้น: กลุ่มแรก
    • Treatment
    • levels
Generalized Linear Models ใน 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 ใน Python

ฟังก์ชัน C()

  • ตัวแปรประเภท (categorical)
    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 ใน 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 ใน 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 ใน Python

Multiple intercepts

'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 ใน Python

มาฝึกกันเถอะ!

Generalized Linear Models ใน Python

Preparing Video For Download...