ML 과제

Suhyeon Lee·2024년 11월 19일
0

tips 데이터를 이용하여 다양한 변수를 넣고 빼면서, 가장 높은 r2 score를 만들어봅시다.

범주형 데이터 수치형으로 변환하기

# 레이블/라벨 인코딩(Label Encoding): 범주형 데이터에 숫자 레이블/라벨 할당 → 월요일은 1, 화요일은 2, …

# 인코딩 결과를 새로운 컬럼으로 추가
# factorize는 인코딩 결과를 튜플로 반환하므로 아래와 같이 형태를 바꿔 추가
tips_df['day_label'] = pd.factorize(tips_df['day'])[0].reshape(-1,1)

# 모델설계도 가져오기
model_lr_day = LinearRegression()
X = tips_df[['total_bill', 'day_label']]
y = tips_df[['tip']]

# 학습
model_lr_day.fit(X,y)

# 예측
y_pred_tip_day = model_lr_day.predict(X)

print('단순선형회귀 mse', mean_squared_error(y_true_tip, y_pred_tip))
print('다중선형회귀 mse', mean_squared_error(y_true_tip, y_pred_tip_day))

print('단순선형회귀 R Square', r2_score(y_true_tip, y_pred_tip))
print('다중선형회귀 R Square', r2_score(y_true_tip, y_pred_tip_day))

단순선형회귀 mse 1.036019442011377
다중선형회귀 mse 1.0351941662575157
단순선형회귀 R Square 0.45661658635167657
다중선형회귀 R Square 0.45704943648763907

profile
2 B R 0 2 B

0개의 댓글

관련 채용 정보