def _get_payoff(self, actual, predicted):
pred_factor = numpy.abs(0.5 - predicted)
payoff_selector = 2*numpy.isclose(actual, 1) + (predicted < 0.5)
payoff = numpy.choose(payoff_selector,
[
self.fp_payoff,
self.tn_payoff,
self.tp_payoff,
self.fn_payoff,
])
return numpy.sum(payoff * pred_factor)
def get_total_payoff(self):
return self._get_payoff(self.target, predictions)
We use numpy.choose
to generate an array of payoff selections and multiply that with an array of absolute differences between 0.5
and the prediction values, then sum. numpy.isclose
is used to test whether the actual
values are close to 1. We can ignore the predicted == 0.5
case, since multiplying by numpy.abs(0.5 - predicted)
gives the correct result of 0 anyway. If self.target
and predictions
are guaranteed to be 1D, numpy.dot
is likely to perform better than separately multiplying and summing.