jax-ml/jax · error · ValueError
the number of data points must exceed order to scale the cov
Error message
the number of data points must exceed order to scale the covariance matrix
What it means
Thrown by jnp.polyfit when a covariance estimate is requested (cov=True or 'unscaled' handling) but the number of sample points len(x) is not greater than the polynomial order. The covariance of the fit residuals requires a positive number of degrees of freedom (len(x) - order) to be well defined.
Source
Thrown at jax/_src/numpy/polynomial.py:292
# For multi-dimensional output, make scale (1, order) to divide
# across the c.T of shape (num_rhs, order)
c = (c.T / scale[np.newaxis, :]).T
else:
# Simple case for 1D output
c = c / scale
if full:
assert rcond is not None
return c, resids, rank, s, lax.asarray(rcond)
elif cov:
Vbase = linalg.inv(dot(lhs.T, lhs))
Vbase /= outer(scale, scale)
if cov == "unscaled":
fac = array(1.0)
else:
if len(x_arr) <= order:
raise ValueError("the number of data points must exceed order"
" to scale the covariance matrix")
fac = resids / (len(x_arr) - order)
if y_arr.ndim == 1:
fac = atleast_1d(fac)[np.newaxis]
# For 1D output, simple scalar multiplication
return c, Vbase * fac
else:
# For multiple rhs, broadcast fac to match shape
return c, Vbase[:, :, np.newaxis] * atleast_1d(fac)[np.newaxis, np.newaxis, :]
else:
return c
@export
@api.jit
def poly(seq_of_zeros: ArrayLike) -> Array:
r"""Returns the coefficients of a polynomial for the given sequence of roots.
View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Increase the number of data points so len(x) > deg + 1
- Lower the polynomial degree deg
- Pass cov=False (or omit cov) if you don't need the covariance matrix
- Validate len(x) and deg before calling polyfit
Example fix
// before jnp.polyfit(x, y, deg=3, cov=True) # x has 3 points // after assert len(x) > deg + 1, "need more points than degree+1 for covariance" jnp.polyfit(x, y, deg=min(deg, len(x) - 2), cov=True)
Defensive patterns
Strategy: validation
Validate before calling
if cov and len(x) <= deg + 1: raise ValueError(f'need len(x) > deg+1, got {len(x)} points, deg={deg}') Prevention
- Check len(x) > deg + 1 whenever cov=True
- Cap degree from data size: deg = min(deg, len(x) - 2)
- Log dataset size before fitting
When it happens
Trigger: Calling jnp.polyfit(x, y, deg, cov=True) where deg >= len(x), e.g. fitting a degree-3 polynomial to 3 data points. Note the check is len(x) <= order, where order = deg + 1 internally in numpy semantics.
Common situations: Overfitting small datasets: fitting high-degree polynomials to few samples; degenerate input where x has fewer elements than expected (e.g. wrong axis or accidental scalar input); porting numpy code that also raises here but users missed it.
Related errors
- y has more than 2 dimensions
- m has more than 2 dimensions
- cov: dtype must be a subclass of float or complex; got {dtyp
- cannot handle multidimensional fweights
- incompatible numbers of samples and fweights
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/ce96304518297017.
Report an issue: GitHub.