Python/machine_learning/scoring_functions.py

83 lines
1.9 KiB
Python
Raw Normal View History

import numpy as np
2017-08-19 05:23:00 +00:00
""" Here I implemented the scoring functions.
MAE, MSE, RMSE, RMSLE are included.
Those are used for calculating differences between
predicted values and actual values.
Metrics are slightly differentiated. Sometimes squared, rooted,
even log is used.
Using log and roots can be perceived as tools for penalizing big
erors. However, using appropriate metrics depends on the situations,
and types of data
"""
2019-10-05 05:14:13 +00:00
# Mean Absolute Error
2017-08-19 05:23:00 +00:00
def mae(predict, actual):
predict = np.array(predict)
actual = np.array(actual)
difference = abs(predict - actual)
score = difference.mean()
return score
2019-10-05 05:14:13 +00:00
# Mean Squared Error
2017-08-19 05:23:00 +00:00
def mse(predict, actual):
predict = np.array(predict)
actual = np.array(actual)
difference = predict - actual
square_diff = np.square(difference)
score = square_diff.mean()
return score
2019-10-05 05:14:13 +00:00
# Root Mean Squared Error
2017-08-19 05:23:00 +00:00
def rmse(predict, actual):
predict = np.array(predict)
actual = np.array(actual)
difference = predict - actual
square_diff = np.square(difference)
2017-08-19 05:23:00 +00:00
mean_square_diff = square_diff.mean()
score = np.sqrt(mean_square_diff)
return score
2019-10-05 05:14:13 +00:00
# Root Mean Square Logarithmic Error
2017-08-19 05:23:00 +00:00
def rmsle(predict, actual):
predict = np.array(predict)
actual = np.array(actual)
2019-10-05 05:14:13 +00:00
log_predict = np.log(predict + 1)
log_actual = np.log(actual + 1)
2017-08-19 05:23:00 +00:00
difference = log_predict - log_actual
square_diff = np.square(difference)
mean_square_diff = square_diff.mean()
score = np.sqrt(mean_square_diff)
return score
2019-10-05 05:14:13 +00:00
# Mean Bias Deviation
def mbd(predict, actual):
predict = np.array(predict)
actual = np.array(actual)
difference = predict - actual
2019-10-05 05:14:13 +00:00
numerator = np.sum(difference) / len(predict)
denumerator = np.sum(actual) / len(predict)
print(numerator)
print(denumerator)
score = float(numerator) / denumerator * 100
return score