求解简单方程

时间:2014-03-10 15:17:32

标签: python math

我有这段代码:

TP = len(set1)
FP = len(set2)
FN = len(set3)

if FP == 0:
    Score = ((2*TP)/((2*TP)+FN))
    print "Warning: FP is equal 0."

elif FN == 0:
     Score = ((2*TP)/((2*TP)+FP))
     print "Warning: FN is equal 0"

elif TP == 0:
    Score = ((2*TP)/((2*TP)+FP+FN))
    print "Warning: TP is equal 0"

else:
    Score = ((2*TP)/((2*TP)+FP+FN))

print " The Score is = ", Score

但由于某种原因,我的分数每次都会返回0。我检查了数值,一切似乎都正确。任何人都可以帮助我??

1 个答案:

答案 0 :(得分:1)

因为您每次都使用整数除法。而不是2常量使用2.0来强制python的除法返回 float

例如:

Score = ((2.0*TP)/((2.0*TP)+FN))

作为旁注,您可以在此问题中详细了解强制浮动部门:How can I force division to be floating point? Division keeps rounding down to 0和文档:Decimals, Floats, and Floating Point Arithmetic