AI

머신러닝 - 성능평가

0_hailey_0 2024. 3. 13. 20:12

머신러닝 모델링을 수행했으면 모델 성능을 평가해보자!

 

1. 회귀 성능 평가

 

1) MAE(Mean Absolute Error)

# 모듈 불러오기
from sklearn.metrics import mean_absolute_error

# 성능 평가
print('MAE:', mean_absolute_error(y_test, y_pred))

 

2)  MSE(Mean Squared Error)

# 모듈 불러오기
from sklearn.metrics import mean_squared_error

# 성능 평가
print('MSE:', mean_squared_error(y_test, y_pred))

 

3) RMSE(Root Mean Squared Error)

# 모듈 불러오기
from sklearn.metrics import mean_squared_error

# 성능 평가
print('RMSE:', mean_squared_error(y_test, y_pred)** 0.5)
print('RMSE:', mean_squared_error(y_test, y_pred, squared=False))

 

4) MAPE(Mean Absolute Percentage Error)

# 모듈 불러오기
from sklearn.metrics import mean_absolute_percentage_error

# 성능 평가
print('MAPE:', mean_absolute_percentage_error(y_test, y_pred))

 

5) R2-Score

# 모듈 불러오기
from sklearn.metrics import r2_score

# 성능 평가
print('R2:', r2_score(y_test, y_pred))

 


 

2. 분류 성능 평가

 

1) Confusion Matrix

# 모듈 불러오기
from sklearn.metrics import confusion_matrix

# 성능 평가
print(confusion_matrix(y_test, y_pred))

 

# 혼동행렬 시각화
plt.figure(figsize=(4,3))
sns.heatmap(confusion_matrix(y_test, y_pred),
           annot = True,
           cmap ='Blues',
           cbar=False,
           annot_kws={'size' : 15})
plt.show()

 

2) Accuracy

# 모듈 불러오기
from sklearn.metrics import accuracy_score

# 성능 평가
print("정확도:", accuracy_score(y_test, y_pred))
# 참고: 평가 성능(정확도)
model.score(x_test, y_test)

# 참고: 학습 성능(정확도)
model.score(x_train, y_train)

 

3) Precision

# 모듈 불러오기
from sklearn.metrics import precision_score

# 성능 평가
print('정밀도:', precision_score(y_test, y_pred))
print('정밀도:', precision_score(y_test, y_pred, average = 'binary'))
print('정밀도:', precision_score(y_test, y_pred, average =None))
print('정밀도:', precision_score(y_test, y_pred, average ='macro'))
print('정밀도:', precision_score(y_test, y_pred, average ='weighted'))

 

 

4) Recall

 

# 모듈 불러오기
from sklearn.metrics import recall_score

# 성능 평가
print('재현율:', recall_score(y_test, y_pred, average=None))

 

5) F1-Score

# 모듈 불러오기
from sklearn.metrics import f1_score

# 성능 평가
print('F1:', f1_score(y_test, y_pred, average=None))

 

6) Classification Report

# 모듈 불러오기
from sklearn.metrics import classification_report

# 성능 평가
print(classification_report(y_test, y_pred))

 

[4주차 02]