0

I'm running a function in python 3 that's counting values as floats, but is returning a value of 0 when they're divided as if they're integers.

Function:

def precision_recall(predictions, results):

#values initiated as floats for later division
tp, fp, fn, tn, i = 0.0, 0.0, 0.0, 0.0, 0

while i < len(results):

        if predictions[i] == 1 and results[i] == 1:
            tp = tp + 1
        elif predictions[i] == 1 and results[i] == 0:
            fp = fp + 1
        elif predictions[i] == 0 and results[i] == 0:
            tn = tn + 1
        else: 
            fn = fn + 1
        i = i + 1

precision = tp / (tp + fp)
recall = tn / (tn + fn)
f1 = precision*recall / (precision + recall)
print("Precision: %d, Recall: %d, f1: %d" % (precision, recall, f1))
print(tp, fp, fn, tn)

The variables predictions and results are lists filled with values of 0 or 1.

This function returns the following output:

Precision: 0, Recall: 0, f1: 0
(113.0, 34.0, 23.0, 187.0)

Ie, the division is returning 0, even though the values used for it are floats.

Furthermore, if I run the same formula for Precision on the command line everything runs fine:

113.0 / (113.0 + 34.0)
Out[25]: 0.7687074829931972 

I'm writing this script with numpy, pandas, and sklearn, but I don't believe any of these should change the way this operation ought to happen.

I'm also using Python 3, and my understanding is that it automatically processes division as floats.

Furthermore, if I make this modification:

precision = tp / float(tp + fp)

I get the same results.

I've tried

from __future__ import divsion

and this hasn't changed anything either.

Does anybody know what could be wrong?

None of the answers in this response work for me.

Community
  • 1
  • 1
Jonathan Bechtel
  • 3,497
  • 4
  • 43
  • 73
  • 1
    Please format the code (looks wrong regarding indents), and print out some stuff within these logical if-else formulas (to make sure, you are changing from the initial zeros = you are entering the cases!). Imagine, that your predictions are floats, comparing these with integer 1 is calling for trouble! And while you are at it: why not show some parts of predictions and results. – sascha Sep 11 '16 at 19:42
  • 4
    There's nothing wrong with the computation; the problem is in the printing. You're formatting your results with `%d`, which is truncating the fractional part. Use `%g` instead. – Mark Dickinson Sep 11 '16 at 19:43
  • @MarkDickinson You are right. As i'm never using these string-formats i didn't see it. Good catch! – sascha Sep 11 '16 at 19:46
  • That's a nice demonstration why new-style string formatting is your friend. – Lev Levitsky Sep 11 '16 at 19:48
  • @MarkDickinson -- you are right, this solved it. Would you mind posting this as an answer so I can go ahead and mark it as closed? – Jonathan Bechtel Sep 11 '16 at 19:55
  • 1
    Another possible duplicate target: http://stackoverflow.com/questions/31708244/python-integer-from-tuple-divided-yields-integer-instead-of-float-no-matter – Mark Dickinson Sep 11 '16 at 20:00

0 Answers0