Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. class SteveObjective(object):
  2. def calc_ders_range(self, approxes, targets, weights):
  3. # approxes, targets, weights are indexed containers of floats
  4. # (containers with only __len__ and __getitem__ defined).
  5. # weights parameter can be None.
  6. # Returns list of pairs (der1, der2)
  7. assert len(approxes) == len(targets)
  8. if weights is not None:
  9. assert len(weights) == len(approxes)
  10.  
  11. result = []
  12. for index in range(len(targets)):
  13. coef = 2 if targets[index] < 4 <= approxes[index] else 1
  14. der1 = 2 * coef * (targets[index] - approxes[index])
  15. der2 = - 2 * coef
  16. result.append((der1, der2))
  17.  
  18. return result
  19.  
  20. class SteveMetric(object):
  21. def get_final_error(self, error, weight):
  22. return error / (weight + 1e-38)
  23.  
  24. def is_max_optimal(self):
  25. return True
  26.  
  27. def _rmse_asymmetric(self, rating, prediction):
  28. bad_border = 4
  29.  
  30. if rating < bad_border <= prediction:
  31. penalty_coefficient = 2.0
  32. else:
  33. penalty_coefficient = 1.0
  34.  
  35. return penalty_coefficient*(rating - prediction)**2
  36.  
  37. def evaluate(self, approxes, target, weight):
  38. # approxes is list of indexed containers
  39. # (containers with only __len__ and __getitem__ defined), one container
  40. # per approx dimension. Each container contains floats.
  41. # weight is one dimensional indexed container.
  42. # target is float.
  43. # weight parameter can be None.
  44. # Returns pair (error, weights sum)
  45.  
  46. assert len(approxes) == 1
  47. assert len(target) == len(approxes[0])
  48.  
  49. approx = approxes[0]
  50.  
  51. error_sum = 0.0
  52. weight_sum = 0.0
  53.  
  54. for i in range(len(approx)):
  55. weight_sum += 1
  56. error_sum += self._rmse_asymmetric(target[i], approxes[0][i])
  57.  
  58. return error_sum, weight_sum
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement