From 37ee08dc9c7cbe0cf8a671c8cc73877d1a142ee1 Mon Sep 17 00:00:00 2001 From: David Schaich Date: Tue, 24 May 2016 19:31:51 -0400 Subject: BUG: Make sure we don't divide by zero in np.polyfit. This should fix the issue discussed at https://mail.scipy.org/pipermail/numpy-discussion/2013-July/067076.html Without the ValueError added here, polyfit can (and does) return negative or nan variances with no warning. --- numpy/lib/polynomial.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'numpy/lib/polynomial.py') diff --git a/numpy/lib/polynomial.py b/numpy/lib/polynomial.py index c0a1cdaed..741cbfa6a 100644 --- a/numpy/lib/polynomial.py +++ b/numpy/lib/polynomial.py @@ -599,6 +599,9 @@ def polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False): # it is included here because the covariance of Multivariate Student-T # (which is implied by a Bayesian uncertainty analysis) includes it. # Plus, it gives a slightly more conservative estimate of uncertainty. + if (len(x) - order - 2.0) < 1: + raise ValueError("the number of data points must exceed " + "the degree + 3 for covariance matrix") fac = resids / (len(x) - order - 2.0) if y.ndim == 1: return c, Vbase * fac -- cgit v1.2.1