Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion python/pyspark/mllib/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ class LogisticRegressionModel(LinearModel):
def predict(self, x):
_linear_predictor_typecheck(x, self._coeff)
margin = _dot(x, self._coeff) + self._intercept
prob = 1/(1 + exp(-margin))
if margin > 0:
prob = 1 / (1 + exp(-margin))
else:
prob = 1 - 1 / (1 + exp(margin))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better would be

prob = exp(margin)/(1 + exp(margin))

because for very small probabilities this computation is essentially 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
>>>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is definitely better. Could you submit a PR? We don't need a JIRA for small changes. Btw, please cache exp(margin) instead of computing it twice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, pull request here! #1652

Thanks a lot! :-)

return 1 if prob > 0.5 else 0


Expand Down