🏆 자격증, 어학

[빅데이터 분석기사] 실기 6회 - 3유형 predict

데이터팍스 2024. 8. 21. 18:04

 

문제

**age가 55, Cholesterol가 72.6일때 위 모델을 기반으로 weight값을 예측하라.**

 

import pandas as pd
import numpy as np
import statsmodels.api as sm

x=df[['age','Cholesterol']]
y=df['weight']

x=sm.add_constant(x)
model=sm.OLS(y,x).fit()
pred = model.predict([1,55,72.6]) # const , age,Cholesterol
print(pred)
 
 

statsmodel 방식으로 풀었을 때는 model.preict() 또는 model.pvalues()가 된다

값은 78.8577101134459

x=df[['age','Cholesterol']]
y=df['weight']
from sklearn.linear_model import LinearRegression
model=LinearRegression()
re=model.fit(x,y)
pred = re.predict([1,55,72.6]) # const , age,Cholesterol
model.coef_
 

근데 sklearn으로 풀었을 때는 저게 된다 1이 const를 의미하는 건가봄..

직접 회귀식 구해서도 넣어봤는데

sum=74.8953-0.0361*55+0.0819*72.6
print(sum)
 

78.85574가 나왔다...

 

결국엔 sklearn과 statsmodels를 전부 외워야함 ㅠㅠ