jax-ml/jax · error · TypeError
expected w and y to have the same length
Error message
expected w and y to have the same length
What it means
Weights in polyfit are applied elementwise per observation, so w must have exactly y.shape[0] entries — one weight per data point. A length mismatch would make the row-wise multiplication (w[:, None] * lhs) shape-invalid, and polyfit surfaces it as an explicit TypeError up front.
Source
Thrown at jax/_src/numpy/polynomial.py:260
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]:
raise TypeError("expected w and y to have the same length")
lhs *= w_arr[:, np.newaxis]
if rhs.ndim == 2:
rhs *= w_arr[:, np.newaxis]
else:
rhs *= w_arr
# scale lhs to improve condition number and solve
scale = sqrt((lhs*lhs).sum(axis=0))
lhs /= scale[np.newaxis, :]
c, resids, rank, s = linalg.lstsq(lhs, rhs, rcond)
# Broadcasting scale coefficients
if c.ndim > 1:
# 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 outputView on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Recompute/ravel weights on the same slice as y: w=w[mask] alongside y[mask].
- Assert w.shape[0] == y.shape[0] before calling.
- Regenerate weights whenever the underlying data length changes.
Example fix
// before c = jnp.polyfit(x[mask], y[mask], 3, w=w) # w unfiltered // after c = jnp.polyfit(x[mask], y[mask], 3, w=w[mask])
Defensive patterns
Strategy: validation
Validate before calling
if w is not None:
assert w.shape[0] == y.shape[0], (w.shape, y.shape)
c = jnp.polyfit(x, y, deg, w=w) Type guard
def weights_match(w, y) -> bool:
return w is None or w.shape[0] == y.shape[0] Prevention
- Filter weights with the same mask as x and y
- Regenerate weights after data-length changes
When it happens
Trigger: jnp.polyfit(x, y, deg, w=w) with len(w) != y.shape[0]; weights computed for the unfiltered dataset while y was filtered; weights from a different time window than y.
Common situations: Filtering x and y by a mask but computing weights before filtering (or vice versa); resampling one of the arrays independently; stale cached weights after data length changes.
Related errors
- expected a 1-d array for weights
- expected deg >= 0
- expected 1D vector for x
- expected non-empty vector for x
- expected 1D or 2D array for y
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/2c2182e8693658a6.
Report an issue: GitHub.