선형 회귀의 작동 방식

Python의 statsmodels로 배우는 중급 회귀

Maarten Van den Broeck

Content Developer at DataCamp

표준 단순 선형 회귀 플롯

선형 회귀 추세선을 가진 산점도.

Python의 statsmodels로 배우는 중급 회귀

잔차 시각화

선형 회귀 추세선과 각 점에서 추세선까지의 선분(잔차)을 표시한 산점도.

Python의 statsmodels로 배우는 중급 회귀

최적 적합의 척도

가장 단순한 생각(실패)

  • 모든 잔차를 합합니다.
  • 일부 잔차는 음수입니다.

그다음 단순한 생각(성공)

  • 각 잔차를 제곱해 합합니다.
  • 이를 제곱합(sum of squares)이라 합니다.
Python의 statsmodels로 배우는 중급 회귀

수치 최적화로의 짧은 우회

이차함수 선 그래프

x = np.arange(-4, 5, 0.1)
y = x ** 2 - x + 10

xy_data = pd.DataFrame({"x": x,
                        "y": y})

sns.lineplot(x="x",
             y="y",
             data=xy_data)

이차함수 y = x ** 2 - x + 10

Python의 statsmodels로 배우는 중급 회귀

미적분으로 방정식 풀기

$y = x ^ 2 - x + 10$

$\frac{\partial y}{\partial x} = 2 x - 1$

$0 = 2 x - 1$

$x = 0.5$

$y = 0.5 ^ 2 - 0.5 + 10 = 9.75$

  • 모든 방정식이 이렇게 풀리지는 않습니다.
  • Python에 맡길 수 있습니다.

이해되지 않아도 걱정하지 마세요. 연습에는 필요하지 않습니다.

앞의 이차함수를 최소값까지 풀이한 그림

Python의 statsmodels로 배우는 중급 회귀

minimize()

from scipy.optimize import minimize
def calc_quadratic(x):
  y = x ** 2 - x + 10
  return y
minimize(fun=calc_quadratic,
         x0=3)
      fun: 9.75
 hess_inv: array([[0.5]])
      jac: array([0.])
  message: 'Optimization terminated successfully.'
     nfev: 6
      nit: 2
     njev: 3
   status: 0
  success: True
        x: array([0.49999998])
Python의 statsmodels로 배우는 중급 회귀

선형 회귀 알고리즘

제곱합(metric)을 계산하는 함수를 정의합니다.

 

minimize()로 이 함수를 최소화하는 계수를 구합니다.

def calc_sum_of_squares(coeffs):
  intercept, slope = coeffs
  # More calculation!
minimize(
  fun=calc_sum_of_squares,
  x0=0
)
Python의 statsmodels로 배우는 중급 회귀

연습해 봅시다!

Python의 statsmodels로 배우는 중급 회귀

Preparing Video For Download...