Skip to content

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))

The rule reports np.log(np.prod(x)).

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)).

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.