모형 공식

Python으로 배우는 Generalized Linear Models

Ita Cirovic Donev

Data Science Consultant

공식과 모형 행렬

데이터 소스 X와 Y로 시작하는 다이어그램.

Python으로 배우는 Generalized Linear Models

공식과 모형 행렬

공식 다이어그램

Python으로 배우는 Generalized Linear Models

공식과 모형 행렬

모형 행렬 다이어그램

Python으로 배우는 Generalized Linear Models

공식과 모형 행렬

glm 클래스 입력 다이어그램.

Python으로 배우는 Generalized Linear Models

모형 행렬

  • 모형 행렬: $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으로 배우는 Generalized Linear Models

변수 변환

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으로 배우는 Generalized Linear Models

중심화와 표준화

  • 상태 보존 변환
'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으로 배우는 Generalized Linear Models

사용자 정의 변환 만들기

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으로 배우는 Generalized Linear Models

산술 연산

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으로 배우는 Generalized Linear Models

범주형 데이터 코딩

색상 유형: 빨강, 초록, 파랑

Python으로 배우는 Generalized Linear Models

범주형 데이터 코딩

색상 유형과 데이터의 색상 관측 다이어그램

Python으로 배우는 Generalized Linear Models

범주형 데이터 코딩

빨강/초록/파랑으로 원-핫 인코딩 다이어그램.

Python으로 배우는 Generalized Linear Models

Patsy 코딩

  • 문자열과 불리는 자동 코딩
  • 수치형 → 범주형
    • C() 함수
  • 기준 그룹
    • 기본: 첫 번째 그룹
    • Treatment
    • levels
Python으로 배우는 Generalized Linear Models

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으로 배우는 Generalized Linear Models

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으로 배우는 Generalized Linear Models

기준 그룹 변경

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으로 배우는 Generalized Linear Models

기준 그룹 변경

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으로 배우는 Generalized Linear Models

다중 절편

'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으로 배우는 Generalized Linear Models

Vamos praticar!

Python으로 배우는 Generalized Linear Models

Preparing Video For Download...