Skip to content

Commit

Permalink
Avoid numerical instability
Browse files Browse the repository at this point in the history
This avoids basically doing 1 - 1, for example:

>>> from math import exp
>>> margin = -40
>>> 1 - 1 / (1 + exp(margin))
0.0
>>> exp(margin) / (1 + exp(margin))
4.248354255291589e-18
>>>
  • Loading branch information
naftaliharris committed Jul 30, 2014
1 parent 077f633 commit 0d55a9f
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion python/pyspark/mllib/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def predict(self, x):
if margin > 0:
prob = 1 / (1 + exp(-margin))
else:
prob = 1 - 1 / (1 + exp(margin))
exp_margin = exp(margin)
prob = exp_margin / (1 + exp_margin)
return 1 if prob > 0.5 else 0


Expand Down

0 comments on commit 0d55a9f

Please sign in to comment.