|
| 1 | +import numpy |
| 2 | + |
| 3 | +""" Here I implemented the scoring functions. |
| 4 | + MAE, MSE, RMSE, RMSLE are included. |
| 5 | +
|
| 6 | + Those are used for calculating differences between |
| 7 | + predicted values and actual values. |
| 8 | +
|
| 9 | + Metrics are slightly differentiated. Sometimes squared, rooted, |
| 10 | + even log is used. |
| 11 | +
|
| 12 | + Using log and roots can be perceived as tools for penalizing big |
| 13 | + erors. However, using appropriate metrics depends on the situations, |
| 14 | + and types of data |
| 15 | +""" |
| 16 | + |
| 17 | +#Mean Absolute Error |
| 18 | +def mae(predict, actual): |
| 19 | + predict = np.array(predict) |
| 20 | + actual = np.array(actual) |
| 21 | + |
| 22 | + difference = abs(predict - actual) |
| 23 | + score = difference.mean() |
| 24 | + |
| 25 | + return score |
| 26 | + |
| 27 | +#Mean Squared Error |
| 28 | +def mse(predict, actual): |
| 29 | + predict = np.array(predict) |
| 30 | + actual = np.array(actual) |
| 31 | + |
| 32 | + difference = predict - actual |
| 33 | + square_diff = np.square(difference) |
| 34 | + |
| 35 | + score = square_diff.mean() |
| 36 | + return score |
| 37 | + |
| 38 | +#Root Mean Squared Error |
| 39 | +def rmse(predict, actual): |
| 40 | + predict = np.array(predict) |
| 41 | + actual = np.array(actual) |
| 42 | + |
| 43 | + difference = predict - actual |
| 44 | + square_diff = np.square(dfference) |
| 45 | + mean_square_diff = square_diff.mean() |
| 46 | + score = np.sqrt(mean_square_diff) |
| 47 | + return score |
| 48 | + |
| 49 | +#Root Mean Square Logarithmic Error |
| 50 | +def rmsle(predict, actual): |
| 51 | + predict = np.array(predict) |
| 52 | + actual = np.array(actual) |
| 53 | + |
| 54 | + log_predict = np.log(predict+1) |
| 55 | + log_actual = np.log(actual+1) |
| 56 | + |
| 57 | + difference = log_predict - log_actual |
| 58 | + square_diff = np.square(difference) |
| 59 | + mean_square_diff = square_diff.mean() |
| 60 | + |
| 61 | + score = np.sqrt(mean_square_diff) |
| 62 | + |
| 63 | + return score |
0 commit comments