Skip to content

scipy/log-of-scipy-det

Rule log-of-scipy-det
Pack SciPy
Severity warning
Fix unsafe, with xpd check --fix --unsafe-fixes
Library versions every version

linalg.det overflows or underflows for a large matrix, so its log is inf or -inf. Use np.linalg.slogdet(a)[1].

logdet = np.log(linalg.det(cov))
logdet = np.linalg.slogdet(cov)[1]

The rule reports np.log(linalg.det(a)), np.log(np.abs(linalg.det(a))) and np.log(abs(linalg.det(a))), where linalg is scipy.linalg or det was imported from it. On two covariance matrices the old form gave -inf and inf, where slogdet lost less than one digit, as the table shows. np.log(np.linalg.det(a)) belongs to the NumPy rule log-of-det.

Covariance matrix np.log(linalg.det(a)) Digits that slogdet lost
120 features at scale 1e-3 -inf 0.54
80 features at scale 1e4 inf 0.27

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.linalg.slogdet(a)[1].

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

$D($A) overflows to inf or underflows to 0 for a large matrix, so its log is inf or -inf. On covariance matrices of 80 and 120 features that lost all 16 digits where slogdet lost less than 1. Write $NP.linalg.slogdet($A)[1].

Browse the other rules of the SciPy pack.