metrics.py 991 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """
  2. Contains all metrics used for model results comparisons
  3. """
  4. # main imports
  5. import numpy as np
  6. def coefficient_of_determination(y, predicted):
  7. """Returns the coefficient of determination between `y` and `y_predicted` values
  8. Args:
  9. y: array of reference values
  10. predicted: array of predicted values
  11. Returns:
  12. coefficient of determination
  13. Example:
  14. >>> from ipfml import metrics
  15. >>> import numpy as np
  16. >>> y = np.arange(10)
  17. >>> predicted = np.arange(10) * 1.05
  18. >>> coeff = metrics.coefficient_of_determination(y, predicted)
  19. >>> int(coeff)
  20. 1
  21. """
  22. y = np.asarray(y)
  23. predicted = np.asarray(predicted)
  24. y_mean = y.mean()
  25. numerator_sum = 0
  26. denominator_sum = 0
  27. for id_val, val in enumerate(y):
  28. numerator_sum += (predicted[id_val] - y_mean) * (
  29. predicted[id_val] - y_mean)
  30. denominator_sum += (val - y_mean) * (val - y_mean)
  31. return numerator_sum / denominator_sum