jax-ml/jax · error · ValueError
expected deg >= 0
Error message
expected deg >= 0
What it means
jnp.polyfit(x, y, deg) performs a least-squares polynomial fit and needs deg to be a non-negative Python integer (it is converted via core.concrete_or_error(int, ...)). A negative degree has no meaning — there is no polynomial of degree -1 — so it raises ValueError before building the Vandermonde matrix.
Source
Thrown at jax/_src/numpy/polynomial.py:237
s: [1.67 0.47 0.04]
rcond: 4.7683716e-07
If ``cov=True`` and ``full=False``, returns a tuple of arrays having
polynomial coefficients and covariance matrix.
>>> p, C = jnp.polyfit(x, y, 2, cov=True)
>>> p.shape, C.shape
((3, 3), (3, 3, 3))
"""
if w is None:
x_arr, y_arr = ensure_arraylike("polyfit", x, y)
else:
x_arr, y_arr, w = ensure_arraylike("polyfit", x, y, w)
del x, y
deg = core.concrete_or_error(int, deg, "deg must be int")
order = deg + 1
if deg < 0:
raise ValueError("expected deg >= 0")
if x_arr.ndim != 1:
raise TypeError("expected 1D vector for x")
if x_arr.size == 0:
raise TypeError("expected non-empty vector for x")
if y_arr.ndim < 1 or y_arr.ndim > 2:
raise TypeError("expected 1D or 2D array for y")
if x_arr.shape[0] != y_arr.shape[0]:
raise TypeError("expected x and y to have same length")
if rcond is None:
rcond = len(x_arr) * float(finfo(x_arr.dtype).eps)
rcond = core.concrete_or_error(float, rcond, "rcond must be float")
# set up least squares equation for powers of x
lhs = vander(x_arr, order)
rhs = y_arr
# apply weighting
if w is not None:View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Clamp/validate deg: use max(deg, 0) only if that's actually intended; otherwise fix the source of the negative value.
- Check the order-vs-degree convention: order = deg + 1, so deg = n_coeffs - 1.
- Pass a static Python int, not a traced value, when under jit.
Example fix
// before coeffs = jnp.polyfit(x, y, deg=n_coeffs - 1) # n_coeffs == 0 // after coeffs = jnp.polyfit(x, y, deg=max(n_coeffs - 1, 0)) // or assert n_coeffs >= 1 before the call
Defensive patterns
Strategy: validation
Validate before calling
deg = int(deg)
if deg < 0:
raise ValueError(f'deg must be >= 0, got {deg}')
c = jnp.polyfit(x, y, deg) Type guard
def valid_deg(d) -> bool:
return isinstance(d, int) and not isinstance(d, bool) and d >= 0 Prevention
- Remember deg = n_coeffs - 1
- Pass degree as a static Python int
When it happens
Trigger: jnp.polyfit(x, y, deg=-1); deg computed as order-1 where order=0; deg passed as a tracer under jit (sibling concrete_or_error failure).
Common situations: Off-by-one when converting between 'number of coefficients' (order) and 'degree' (order-1); looping over degrees and including 0 or negatives; config-driven degree values validated only as ints, not as >= 0.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- expected 1D vector for x
- expected non-empty vector for x
- expected 1D or 2D array for y
- expected x and y to have same length
- expected a 1-d array for weights
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/49cb720adfd2da5d.
Report an issue: GitHub.