[통계] statsmodel로 선형회귀 모델링

·2025년 1월 17일

통계

목록 보기
31/34

statsmodel

  • sklearn의 linear_model이 머신러닝의 관점이라면 statsmodel은 좀 더 statistical
  • statsmodels.api : statsmodels를 편리하게 불러오는 모듈
  • sm.add_constant : β0\beta_0 추가
  • model = sm.OLS : 모델 생성
  • model.fit() : 학습

tips 데이터로 실습

import statsmodels.api as sm
import pandas as pd

# 예제 데이터 로드 (Seaborn의 tips 데이터셋 사용)
import seaborn as sns
tips = sns.load_dataset('tips')

# 독립변수(x)와 종속변수(tip) 설정
x = tips['total_bill']  # 예시로 total_bill을 독립변수로 사용
y = tips['tip']

# 독립변수에 상수 추가
x_with_constant = sm.add_constant(x)

# 회귀 분석 모델 생성 및 적합
model = sm.OLS(y, x_with_constant)  # OLS: Ordinary Least Squares
results = model.fit()

# 회귀 분석 결과 출력
print(results.summary())

profile
To Dare is To Do

0개의 댓글