-
회귀 (연속형 변수) 측정하는법 MSE, RMSE, MAE, MPE, MAPE in pythondata-science-summary/summary 2020. 9. 18. 00:10반응형
<엔지니어는 구현을 못하면 모르는것이다>
회귀 (연속형 변수) 측정
MSE, RMSE, MAE, MPE, MAPE 사용하여 계산
모두 Mean Error 가 들어간다. => 즉 평균을 계산할수 있는 연속형 변수 측정에 사용한다.
import pandas as pd import numpy as np from sklearn import datasets from sklearn.model_selection import train_test_split datas = datasets.load_diabetes() datas.target df = pd.DataFrame(datas.data, columns = datas.feature_names) y = pd.DataFrame(datas.target, columns = ['y']) df = pd.concat([df, y], axis=1) train, test = train_test_split(df, test_size=0.3, random_state=123) from sklearn.linear_model import LinearRegression lr = LinearRegression() lr.fit(train.drop(columns='y'), train.y) y_pred = lr.predict(test.drop(columns='y')) y_true = test.y # 평균 제곱 오차 mean squared error from sklearn.metrics import mean_squared_error mse = mean_squared_error(y_true, y_pred) def MSE(y, pred): return np.mean(np.square(y-pred)) print(MSE(y_true, y_pred), mse) # 평균 제곱근 오차 root mean squared error from sklearn.metrics import mean_squared_error rmse = np.sqrt(mean_squared_error(y_true, y_pred)) def RMSE(y, pred): return np.sqrt(np.mean(np.square(y-pred))) print(RMSE(y_true, y_pred), rmse) # 평균 절대 오차 mean absolute error from sklearn.metrics import mean_absolute_error mae = mean_absolute_error(y_true, y_pred) def MAE(y, pred): return np.mean(np.abs(y-pred)) print(MAE(y_true, y_pred), mae) # 평균 백분율 오차 mean absolute percentage error def MPE(y, pred): return np.mean((y-pred)/y)*100 print(MPE(y_true, y_pred)) # 평균 절대 백분율 오차 mean percentage error def MAPE(y, pred): return np.mean(np.abs((y-pred)/y)*100) print(MAPE(y_true, y_pred))
반응형'data-science-summary > summary' 카테고리의 다른 글
Logistic regression 에서 통계적으로유의한 독립변수 찾는법 in python (0) 2020.09.19 로지스틱 회귀(logistic regression) in python (0) 2020.09.19 VIF(Variance Inflation Factor) 분산팽창지수 in python (0) 2020.09.18 결정 계수 in python (0) 2020.09.17 선형회귀(LinearRegression) in python (0) 2020.09.17