회귀 분석

Python으로 설문 데이터 분석하기

EbunOluwa Andrew

Data Scientist

회귀 분석

  • 변수 간 관계 파악
  • 구체적 결과 예측에 활용
  • 독립 변수들의 종속 변수 영향 평가
  • 미래 기회와 위험 예측
  • 방대한 원시 데이터를 실행 가능한 정보로 축약
  • 근거 기반 의사결정 지원

하락 화살표를 막는 사람들

Python으로 설문 데이터 분석하기

최소제곱법(OLS)을 이용한 선형 회귀

  • 선형 회귀 모형
    • x와 y 사이 선형 관계 가정
    • y = m * x + b
    • 최소제곱법(OLS)
    • Sum((예측−관측)^2) 최소화

https://seeing-theory.brown.edu/regression-analysis/index.html

1 https://seeing-theory.brown.edu/regression-analysis/index.html
Python으로 설문 데이터 분석하기

데이터 불러오기

import pandas as pd

import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
exercise_data = pd.read_csv('workout_survey_data.csv') print(exercise_data.head())
| workout_minutes | calories_burned |
|-----------------|-----------------|
| 77              | 79.775152       |
| 21              | 23.177279       |
| 22              | 25.609262       |
| 20              | 17.857388       |
Python으로 설문 데이터 분석하기

변수 정의

x = 독립 변수 y = 종속 변수

x = exercise_data.minutes.tolist()
y = exercise_data.calories.tolist() 
print(x,'\n',y)
| [77, 21, 22, 20, 36...           |
|----------------------------------|
| [79.7, 23.1, 25.6, 17.8, 41.8... |

설문 데이터

workout_minutes calories_burned
77 79.775152
21 23.177279
22 25.609262
20 17.857388
36 41.849864
Python으로 설문 데이터 분석하기

상수항 추가

x = sm.add_constant(x)
print (x)
  • 모형에 b(절편) 추정을 지시합니다

Python으로 설문 데이터 분석하기

회귀 수행 및 적합

result = sm.OLS(y,x).fit()
print(result.summary())

Python으로 설문 데이터 분석하기

m과 b 가져오기

Python으로 설문 데이터 분석하기

원자료 산점도 그리기

x = exercise_data.minutes.tolist()
y = exercise_data.calories.tolist()
plt.scatter(x,y)
plt.xlabel('minutes')
plt.ylabel('calories')
plt.show()

Python으로 설문 데이터 분석하기

회귀선 그리기

max_x = exercise_data.minutes.max()
min_x = exercise_data.minutes.min()
x = np.arange(min_x, max_x, 1)

y = 1.0072*x + 0.1552
plt.plot(y, 'r') plt.show()

Python으로 설문 데이터 분석하기

반응 예측

y = 1.0072 * 30 + 0.1552
print(y)
30.3712
Python으로 설문 데이터 분석하기

선형 회귀의 장단점

  • 장점
    • 데이터가 선형 분리 가능할 때 성능 우수
  • 단점
    • 비선형 경우에도 선형 관계를 가정함

Python으로 설문 데이터 분석하기

연습해 봅시다!

Python으로 설문 데이터 분석하기

Preparing Video For Download...