numpy/log-of-product
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| NumPy | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
A product can overflow to inf or underflow to 0 before the log is taken. Sum the logs instead.
log_likelihood = np.log(np.prod(probabilities))log_likelihood = np.sum(np.log(probabilities))What the rule reports
Section titled “What the rule reports”The rule reports np.log(np.prod(x)).
How to fix it
Section titled “How to fix it”xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.
The unsafe fix writes np.sum(np.log(x)).
The last bits can change. An even number of negative inputs can yield a positive product with a real logarithm, while taking the individual logs yields NaN.
Diagnostic message
Section titled “Diagnostic message”The product can overflow to inf or underflow to 0 before the log is taken. Write $NP.sum($NP.log($X)).See all rules in the NumPy pack.