평행 기울기 선형 회귀

Python의 statsmodels로 배우는 중급 회귀

Maarten Van den Broeck

Content Developer at DataCamp

이전 강의

이 강의는 Introduction to Regression with statsmodels in Python의 내용을 전제로 합니다

Python의 statsmodels로 배우는 중급 회귀

단순 회귀에서 복수 회귀로

복수 회귀는 둘 이상 설명 변수를 갖는 회귀 모델입니다.

설명 변수가 늘면 통찰이 늘고 예측이 더 좋아집니다.

Python의 statsmodels로 배우는 중급 회귀

강의 구성

1장

  • "평행 기울기" 회귀

2장

  • 상호작용
  • 심프슨의 역설

3장

  • 더 많은 설명 변수
  • 선형 회귀의 동작 원리

4장

  • 다중 로지스틱 회귀
  • 로지스틱 분포
  • 로지스틱 회귀의 동작 원리
Python의 statsmodels로 배우는 중급 회귀

물고기 데이터셋

mass_g length_cm species
242.0 23.2 Bream
5.9 7.5 Perch
200.0 30.0 Pike
40.0 12.9 Roach
  • 각 행은 물고기 한 마리를 나타냅니다
  • mass_g는 반응 변수입니다
  • 수치형 1개, 범주형 설명 변수 1개
Python의 statsmodels로 배우는 중급 회귀

설명 변수를 하나씩 보기

from statsmodels.formula.api import ols

mdl_mass_vs_length = ols("mass_g ~ length_cm",
                         data=fish).fit()
print(mdl_mass_vs_length.params)
Intercept   -536.223947
length_cm     34.899245
dtype: float64
  • 절편 계수 1개
  • 기울기 계수 1개
mdl_mass_vs_species = ols("mass_g ~ species + 0",
                          data=fish).fit()

print(mdl_mass_vs_species.params)
species[Bream]    617.828571
species[Perch]    382.239286
species[Pike]     718.705882
species[Roach]    152.050000
dtype: float64
  • 각 범주에 대한 절편 계수 1개
Python의 statsmodels로 배우는 중급 회귀

두 변수를 동시에 보기

mdl_mass_vs_both = ols("mass_g ~ length_cm + species + 0",
                       data=fish).fit()
print(mdl_mass_vs_both.params)
species[Bream]    -672.241866
species[Perch]    -713.292859
species[Pike]    -1089.456053
species[Roach]    -726.777799
length_cm           42.568554
dtype: float64
  • 기울기 계수 1개
  • 각 범주에 대한 절편 계수 1개
Python의 statsmodels로 배우는 중급 회귀

계수 비교

print(mdl_mass_vs_length.params)
Intercept   -536.223947
length_cm     34.899245
print(mdl_mass_vs_both.params)
species[Bream]    -672.241866
species[Perch]    -713.292859
species[Pike]    -1089.456053
species[Roach]    -726.777799
length_cm           42.568554
print(mdl_mass_vs_species.params)
species[Bream]    617.828571
species[Perch]    382.239286
species[Pike]     718.705882
species[Roach]    152.050000
Python의 statsmodels로 배우는 중급 회귀

시각화: 수치형 설명 변수 1개

import matplotlib.pyplot as plt
import seaborn as sns

sns.regplot(x="length_cm",
            y="mass_g",
            data=fish,
            ci=None)

plt.show()

물고기 질량 대 길이 산점도와 선형 추세선

Python의 statsmodels로 배우는 중급 회귀

시각화: 범주형 설명 변수 1개

sns.boxplot(x="species",
            y="mass_g",
            data=fish,
            showmeans=True)

종별 물고기 질량의 상자그림

Python의 statsmodels로 배우는 중급 회귀

시각화: 두 설명 변수

coeffs = mdl_mass_vs_both.params
print(coeffs)
species[Bream]    -672.241866
species[Perch]    -713.292859
species[Pike]    -1089.456053
species[Roach]    -726.777799
length_cm           42.568554
ic_bream, ic_perch, ic_pike, ic_roach, sl = coeffs
sns.scatterplot(x="length_cm",
                y="mass_g",
                hue="species",
                data=fish)
plt.axline(xy1=(0, ic_bream), slope=sl, color="blue")
plt.axline(xy1=(0, ic_perch), slope=sl, color="green")
plt.axline(xy1=(0, ic_pike), slope=sl, color="red")
plt.axline(xy1=(0, ic_roach), slope=sl, color="orange")

종별로 구분된 물고기 질량 대 길이의 평행 기울기 모델

Python의 statsmodels로 배우는 중급 회귀

연습해 봅시다!

Python의 statsmodels로 배우는 중급 회귀

Preparing Video For Download...