이진 데이터와 로지스틱 회귀

Python으로 배우는 Generalized Linear Models

Ita Cirovic Donev

Data Science Consultant

이진 반응 데이터

  • 이진 반응 $\rightarrow \large{\texttt{\color{#079EA1}{0},\color{#ED715F}{1}}}$

예:

  • 신용평가 $\rightarrow \texttt{\color{#ED715F}{"연체"}/\color{#079EA1}{"비연체"}}$
  • 시험 합격 여부 $\rightarrow \texttt{\color{#079EA1}{"합격"}/\color{#ED715F}{"불합격"}}$
  • 이상 거래 탐지 $\rightarrow \texttt{\color{#ED715F}{"사기"}/\color{#079EA1}{"비사기"}}$
  • 제품 선택 $\rightarrow \texttt{\color{#2485F2}{"Product ABC"}/\color{#F2AC30}{"Product XYZ"}}$
Python으로 배우는 Generalized Linear Models

이진 데이터

비집계(UNGROUPED)

  • 단일 사건
  • 동전 1회 던지기
  • 가능한 결과 두 가지: 0/1
  • $Bernoulli(p)$ 또는
  • $Binomial(n=1,p)$

집계(GROUPED)

  • 복수 사건
  • 동전 여러 번 던지기
  • 주어진 시행 횟수 $n$에서 성공 횟수
  • $Binomial(n,p)$
Python으로 배우는 Generalized Linear Models

로지스틱 함수

공부 시간과 시험 합격/불합격(0/1)의 산점도

Python으로 배우는 Generalized Linear Models

로지스틱 함수

공부 시간과 시험 합격/불합격(0/1)의 산점도

  • 시험 결과: $PASS=1$ 또는 $FAIL=0$

  • 다음을 모형화

$P(y=1)=\beta_0 + \beta_1x_1$

$P(\text{Pass})=\beta_0 + \beta_1 \times \text{공부 시간}$

Python으로 배우는 Generalized Linear Models

로지스틱 함수

공부 시간과 시험 합격/불합격(0/1)의 로지스틱 적합

  • 시험 결과: $PASS=1$, $FAIL=0$

  • 다음을 모형화:

$P(y=1)=\beta_0 + \beta_1x_1$

$P(\text{Pass})=\beta_0 + \beta_1 \times \text{공부 시간}$

  • 로지스틱 함수 사용

$f(z) = \frac{1}{(1+\exp(-z))}$

Python으로 배우는 Generalized Linear Models

오즈와 오즈비

       

$$ ODDS = \frac{\text{사건 발생}}{\text{사건 미발생}} $$

       

$$ \text{ODDS RATIO} = \frac{odds 1}{odds 2} $$

Python으로 배우는 Generalized Linear Models

오즈 예시

  • 4경기 연속 결과: 3승 1패

  • 오즈는 3 대 1 오즈의 시각적 계산: 분자에 3개의 승, 분모에 1개의 패 상자

Python으로 배우는 Generalized Linear Models

오즈와 확률

  $$ \text{odds} \neq \text{probability} $$

  $$ \text{odds} = \frac{\text{probability}}{1-\text{probability}} $$

  $$ \text{probability} = \frac{\text{odds}}{1+\text{odds}} $$

Python으로 배우는 Generalized Linear Models

확률 모형에서 로지스틱 회귀로

 

1단계. 확률 모형

$E(y)=\mu=P(y=1)=\beta_0 + \beta_1x_1$

 

2단계. 로지스틱 함수

$f(z) = \large{\frac{1}{(1+\exp(-z))}}$

 

3단계. 로지스틱 함수 적용 $\rightarrow$ INVERSE-LOGIT

$\mu = \large{\frac{1}{1+\exp(-(\beta_0+\beta_1x_1))}} = \large{\frac{\exp(\beta_0+\beta_1x_1)}{1+\exp(\beta_0+\beta_1x_1)}}$

$1-\mu = \large{\frac{1}{1+\exp(\beta_0+\beta_1x_1)}}$

Python으로 배우는 Generalized Linear Models

확률 모형에서 로지스틱 회귀로

 

  • 확률 $\rightarrow$ 오즈 $$ ODDS=\frac{\mu}{1-\mu} = exp{(\beta_0+\beta_1x_1)} $$  
  • 로그 변환 $\rightarrow \color{#CF5383}{\text{로지스틱 회귀}}$

  $$ LOGIT(\mu)=log(\frac{\mu}{1-\mu}) = \beta_0+\beta_1x_1 $$

Python으로 배우는 Generalized Linear Models

Python에서의 로지스틱 회귀

함수 - glm()

model_GLM = glm(formula = 'y ~ x',                        
                data = my_data, 
                family = sm.families.Binomial()).fit

입력

y = [0,1,1,0,...]
y = ['No','Yes','Yes',...]
y = ['Fail','Pass','Pass',...]
Python으로 배우는 Generalized Linear Models

Ayo berlatih!

Python으로 배우는 Generalized Linear Models

Preparing Video For Download...