Skip to content
Snippets Groups Projects
Commit e3d85b7e authored by Naftali Harris's avatar Naftali Harris Committed by Xiangrui Meng
Browse files

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 <naftaliharris@gmail.com>

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

0d55a9f [Naftali Harris] Avoid numerical instability
parent 3bc3f180
No related branches found
No related tags found
No related merge requests found
...@@ -66,7 +66,8 @@ class LogisticRegressionModel(LinearModel): ...@@ -66,7 +66,8 @@ class LogisticRegressionModel(LinearModel):
if margin > 0: if margin > 0:
prob = 1 / (1 + exp(-margin)) prob = 1 / (1 + exp(-margin))
else: 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 return 1 if prob > 0.5 else 0
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment