numpy/log-of-product
| Rule | log-of-product |
|---|---|
| Pack | NumPy |
| Severity | warning |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
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)).
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
The product can overflow to inf or underflow to 0 before the log is taken. Write $NP.sum($NP.log($X)).Browse the other rules of the NumPy pack.