jax-ml/jax · error · TypeError

expected non-empty vector for x

Error message

expected non-empty vector for x

What it means

A least-squares fit with zero data points is underdetermined/meaningless: the Vandermonde matrix would have zero rows and the normal equations singular. jnp.polyfit therefore rejects empty x with a TypeError before doing any linear algebra.

Source

Thrown at jax/_src/numpy/polynomial.py:241

    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:
    w_arr, = promote_dtypes_inexact(w)
    if w_arr.ndim != 1:
      raise TypeError("expected a 1-d array for weights")
    if w_arr.shape[0] != y_arr.shape[0]:

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Skip the fit when x.size == 0 (return NaNs or previous coefficients).
  2. Assert non-empty input before calling: if x.size == 0: raise/return early.
  3. Fix upstream filtering so at least deg+1 points remain.

Example fix

// before
c = jnp.polyfit(x[mask], y[mask], deg)  # mask all-False
// after
if mask.sum() > deg:
    c = jnp.polyfit(x[mask], y[mask], deg)
else:
    c = last_known_coeffs  # or NaN placeholder
Defensive patterns

Strategy: validation

Validate before calling

if x.size == 0:
    raise ValueError('no samples to fit')  # or return NaN
c = jnp.polyfit(x, y, deg)

Type guard

def has_samples(x) -> bool:
    return getattr(x, 'size', 0) > 0

Prevention

When it happens

Trigger: jnp.polyfit(jnp.array([]), y, deg); x filtered by a mask that removed all points; empty minibatches or empty time windows fed to a fitter.

Common situations: Runtime edge cases where a filter/window selects zero samples (market data gaps, empty sensor buffers); test code iterating over groups where some group is empty.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/114a05e1d346ee20. Report an issue: GitHub.