Statsmodels 단순회귀분석

1 개요[ | ]

statsmodels 단순회귀분석
statsmodels 단순선형회귀분석

2 예시 1: Y ~ X[ | ]

import statsmodels.api as sm
Y = [1,3,4,5,2,3,4]
X = [1,2,3,4,5,6,7]
X = sm.add_constant(X)
model = sm.OLS(Y,X)
results = model.fit()

print( results.params )
print( "R²=", results.rsquared )
→ 회귀식 [math]\displaystyle{ y=0.25x+2.14285714 }[/math]
→ 결정계수 [math]\displaystyle{ R^2=0.16118421052631593 }[/math]

3 예시 2: 키와 몸무게[ | ]

import pandas as pd
df = pd.DataFrame({
'height': [1.47, 1.50, 1.52, 1.55, 1.57, 1.60, 1.63, 1.65, 1.68, 1.70, 1.73, 1.75, 1.78, 1.80, 1.83],
'mass': [52.21, 53.12, 54.48, 55.84, 57.20, 58.57, 59.93, 61.29, 63.11, 64.47, 66.28, 68.10, 69.92, 72.19, 74.46],
})

Y = df['mass']
X = df['height']
import statsmodels.api as sm
X = sm.add_constant(X)
model = sm.OLS(Y,X)
results = model.fit()
results.params
→ 회귀식 [math]\displaystyle{ y=61.272187x-39.061956 }[/math]
results.rsquared
→ 결정계수 [math]\displaystyle{ R^2=0.9891969224457968 }[/math]

4 예시 3: 아이스티 주문[ | ]

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/jmnote/zdata/master/simple-regression/iced-tea-orders.csv')
print( df )

Y = df['order']
X = df['high_temperature']

import statsmodels.api as sm
X = sm.add_constant(X)
model = sm.OLS(Y,X)
results = model.fit()

print( results.params )
print( "R²=", results.rsquared )
→ 회귀식 [math]\displaystyle{ y=3.737885x-36.361233 }[/math]
→ 결정계수 [math]\displaystyle{ R^2=0.8225092881166943 }[/math]

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}