로지스틱 회귀가 필요한 이유

Python에서 statsmodels로 살펴보는 회귀 소개

Maarten Van den Broeck

Content Developer at DataCamp

은행 이탈 데이터셋

has_churned time_since_first_purchase time_since_last_purchase
0 0.3993247 -0.5158691
1 -0.4297957 0.6780654
0 3.7383122 0.4082544
0 0.6032289 -0.6990435
... ... ...
반응 변수 관계 기간 최근 활동 시점
1 https://www.rdocumentation.org/packages/bayesQR/topics/Churn
Python에서 statsmodels로 살펴보는 회귀 소개

이탈 vs. 최근성: 선형 모델

mdl_churn_vs_recency_lm = ols("has_churned ~ time_since_last_purchase",
                              data=churn).fit()

print(mdl_churn_vs_recency_lm.params)
Intercept                   0.490780
time_since_last_purchase    0.063783
dtype: float64
intercept, slope = mdl_churn_vs_recency_lm.params
Python에서 statsmodels로 살펴보는 회귀 소개

선형 모델 시각화

sns.scatterplot(x="time_since_last_purchase",
                y="has_churned",
                data=churn)

plt.axline(xy1=(0, intercept), slope=slope) plt.show()

마지막 구매 이후 경과 시간에 따른 고객 이탈 여부 산점도. 모든 점은 y=0 또는 y=1 선 위에 위치합니다. 선형 추세선은 경과 시간이 늘어날수록 이탈 확률이 증가함을 보여줍니다.

Python에서 statsmodels로 살펴보는 회귀 소개

축 범위 확대

sns.scatterplot(x="time_since_last_purchase",
                y="has_churned",
                data=churn)

plt.axline(xy1=(0,intercept),
           slope=slope)

plt.xlim(-10, 10) plt.ylim(-0.2, 1.2)
plt.show()

마지막 구매 이후 경과 시간에 따른 고객 이탈 여부 산점도. 이전보다 축 범위를 넓혀 추세선이 y=0 아래와 y=1 위로 벗어나는 것을 보여줍니다. 이는 불가능한 값입니다.

Python에서 statsmodels로 살펴보는 회귀 소개

로지스틱 회귀란?

  • 일반화 선형 모델의 또 다른 유형입니다.
  • 반응 변수가 논리형일 때 사용합니다.
  • 반응값은 로지스틱(S자형) 곡선을 따릅니다.
Python에서 statsmodels로 살펴보는 회귀 소개

`logit()`을 이용한 로지스틱 회귀

from statsmodels.formula.api import logit
mdl_churn_vs_recency_logit = logit("has_churned ~ time_since_last_purchase",
                                   data=churn).fit()

print(mdl_churn_vs_recency_logit.params)
Intercept                  -0.035019
time_since_last_purchase    0.269215
dtype: float64
Python에서 statsmodels로 살펴보는 회귀 소개

로지스틱 모델 시각화

sns.regplot(x="time_since_last_purchase",
            y="has_churned",
            data=churn,
            ci=None,
            logistic=True)
plt.axline(xy1=(0,intercept),
           slope=slope,
           color="black")

plt.show()

마지막 구매 이후 경과 시간에 따른 고객 이탈 여부 산점도. 선형 및 로지스틱 추세선이 표시되며, 두 선 모두 경과 시간이 늘어날수록 이탈 확률이 증가함을 보여줍니다. 경과 시간이 매우 클 때를 제외하면 두 추세선은 서로 매우 가깝게 추적합니다.

Python에서 statsmodels로 살펴보는 회귀 소개

축 범위 확대

마지막 구매 이후 경과 시간에 따른 고객 이탈 여부 산점도로, 두 추세선이 모두 표시됩니다. 이전보다 축 범위를 넓혀 로지스틱 추세선이 이탈 확률 0~1 범위를 벗어나지 않음을 보여줍니다.

Python에서 statsmodels로 살펴보는 회귀 소개

연습해 봅시다!

Python에서 statsmodels로 살펴보는 회귀 소개

Preparing Video For Download...