{"record":{"id":"2c2182e8693658a6","repo":"jax-ml/jax","slug":"expected-w-and-y-to-have-the-same-length","errorCode":null,"errorMessage":"expected w and y to have the same length","messagePattern":"expected w and y to have the same length","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/polynomial.py","lineNumber":260,"sourceCode":"  if y_arr.ndim < 1 or y_arr.ndim > 2:\n    raise TypeError(\"expected 1D or 2D array for y\")\n  if x_arr.shape[0] != y_arr.shape[0]:\n    raise TypeError(\"expected x and y to have same length\")\n\n  if rcond is None:\n    rcond = len(x_arr) * float(finfo(x_arr.dtype).eps)\n  rcond = core.concrete_or_error(float, rcond, \"rcond must be float\")\n  # set up least squares equation for powers of x\n  lhs = vander(x_arr, order)\n  rhs = y_arr\n\n  # apply weighting\n  if w is not None:\n    w_arr, = promote_dtypes_inexact(w)\n    if w_arr.ndim != 1:\n      raise TypeError(\"expected a 1-d array for weights\")\n    if w_arr.shape[0] != y_arr.shape[0]:\n      raise TypeError(\"expected w and y to have the same length\")\n    lhs *= w_arr[:, np.newaxis]\n    if rhs.ndim == 2:\n      rhs *= w_arr[:, np.newaxis]\n    else:\n      rhs *= w_arr\n\n  # scale lhs to improve condition number and solve\n  scale = sqrt((lhs*lhs).sum(axis=0))\n  lhs /= scale[np.newaxis, :]\n  c, resids, rank, s = linalg.lstsq(lhs, rhs, rcond)\n\n  # Broadcasting scale coefficients\n  if c.ndim > 1:\n    # For multi-dimensional output, make scale (1, order) to divide\n    # across the c.T of shape (num_rhs, order)\n    c = (c.T / scale[np.newaxis, :]).T\n  else:\n    # Simple case for 1D output","sourceCodeStart":242,"sourceCodeEnd":278,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/polynomial.py#L242-L278","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nc = jnp.polyfit(x[mask], y[mask], 3, w=w)  # w unfiltered\n// after\nc = jnp.polyfit(x[mask], y[mask], 3, w=w[mask])","handlingStrategy":"validation","validationCode":"if w is not None:\n    assert w.shape[0] == y.shape[0], (w.shape, y.shape)\nc = jnp.polyfit(x, y, deg, w=w)","typeGuard":"def weights_match(w, y) -> bool:\n    return w is None or w.shape[0] == y.shape[0]","tryCatchPattern":null,"preventionTips":["Filter weights with the same mask as x and y","Regenerate weights after data-length changes"],"tags":["jax","numpy","polynomial","polyfit","weights","length-mismatch"],"backgroundTag":"length-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}