regression_metric.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from scipy.stats import stats
  2. from sklearn.metrics import explained_variance_score
  3. from sklearn.metrics import mean_absolute_error
  4. from sklearn.metrics import mean_squared_error
  5. from sklearn.metrics import median_absolute_error
  6. from sklearn.metrics import r2_score
  7. import numpy as np
  8. class RMSE(object):
  9. @staticmethod
  10. def compute(labels, pred_scores):
  11. return np.sqrt(mean_squared_error(labels, pred_scores))
  12. class MAE(object):
  13. @staticmethod
  14. def compute(labels, pred_scores):
  15. return mean_absolute_error(labels, pred_scores)
  16. class R2Score(object):
  17. @staticmethod
  18. def compute(labels, pred_scores):
  19. return r2_score(labels, pred_scores)
  20. class MSE(object):
  21. @staticmethod
  22. def compute(labels, pred_scores):
  23. return mean_squared_error(labels, pred_scores)
  24. class ExplainedVariance(object):
  25. @staticmethod
  26. def compute(labels, pred_scores):
  27. return explained_variance_score(labels, pred_scores)
  28. class MedianAbsoluteError(object):
  29. @staticmethod
  30. def compute(labels, pred_scores):
  31. return median_absolute_error(labels, pred_scores)
  32. class IC(object):
  33. """
  34. Compute Information Criterion with a given dTable and loss
  35. When k = 2, result is genuine AIC;
  36. when k = log(n), results is BIC, also called SBC, SIC, SBIC.
  37. """
  38. def compute(self, k, n, dfe, loss):
  39. aic_score = k * dfe + 2 * n * loss
  40. return aic_score
  41. class IC_Approx(object):
  42. """
  43. Compute Information Criterion value with a given dTable and loss
  44. When k = 2, result is genuine AIC;
  45. when k = log(n), results is BIC, also called SBC, SIC, SBIC.
  46. Note that this formula for linear regression dismisses the constant term n * np.log(2 * np.pi) for sake of simplicity, so the absolute value of result will be small.
  47. """
  48. def compute(self, k, n, dfe, loss):
  49. aic_score = k * dfe + n * np.log(loss * 2)
  50. return aic_score
  51. class Describe(object):
  52. @staticmethod
  53. def compute(pred_scores):
  54. describe = stats.describe(pred_scores)
  55. metrics = {"min": describe.minmax[0], "max": describe.minmax[1], "mean": describe.mean,
  56. "variance": describe.variance, "skewness": describe.skewness, "kurtosis": describe.kurtosis}
  57. return metrics