You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by me...@apache.org on 2014/07/30 18:57:03 UTC

git commit: Avoid numerical instability

Repository: spark
Updated Branches:
  refs/heads/master 3bc3f1801 -> e3d85b7e4


Avoid numerical instability

This avoids basically doing 1 - 1, for example:

```python
>>> from math import exp
>>> margin = -40
>>> 1 - 1 / (1 + exp(margin))
0.0
>>> exp(margin) / (1 + exp(margin))
4.248354255291589e-18
>>>
```

Author: Naftali Harris <na...@gmail.com>

Closes #1652 from naftaliharris/patch-2 and squashes the following commits:

0d55a9f [Naftali Harris] Avoid numerical instability


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/e3d85b7e
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/e3d85b7e
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/e3d85b7e

Branch: refs/heads/master
Commit: e3d85b7e40073b05e2588583e9d8db11366c2f7b
Parents: 3bc3f18
Author: Naftali Harris <na...@gmail.com>
Authored: Wed Jul 30 09:56:59 2014 -0700
Committer: Xiangrui Meng <me...@databricks.com>
Committed: Wed Jul 30 09:56:59 2014 -0700

----------------------------------------------------------------------
 python/pyspark/mllib/classification.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/e3d85b7e/python/pyspark/mllib/classification.py
----------------------------------------------------------------------
diff --git a/python/pyspark/mllib/classification.py b/python/pyspark/mllib/classification.py
index 9e28dfb..2bbb9c3 100644
--- a/python/pyspark/mllib/classification.py
+++ b/python/pyspark/mllib/classification.py
@@ -66,7 +66,8 @@ class LogisticRegressionModel(LinearModel):
         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