{"record":{"id":"a021dfd7dde5c12c","repo":"jax-ml/jax","slug":"expected-x-and-y-to-have-same-length","errorCode":null,"errorMessage":"expected x and y to have same length","messagePattern":"expected x and y to have same length","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/polynomial.py","lineNumber":245,"sourceCode":"    ((3, 3), (3, 3, 3))\n  \"\"\"\n  if w is None:\n    x_arr, y_arr = ensure_arraylike(\"polyfit\", x, y)\n  else:\n    x_arr, y_arr, w = ensure_arraylike(\"polyfit\", x, y, w)\n  del x, y\n  deg = core.concrete_or_error(int, deg, \"deg must be int\")\n  order = deg + 1\n  if deg < 0:\n    raise ValueError(\"expected deg >= 0\")\n  if x_arr.ndim != 1:\n    raise TypeError(\"expected 1D vector for x\")\n  if x_arr.size == 0:\n    raise TypeError(\"expected non-empty vector for x\")\n  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]","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/polynomial.py#L227-L263","documentation":"polyfit builds vander(x, order) with len(x) rows, so y must have the same number of observations: y.shape[0] == x.shape[0]. A length mismatch means the design matrix and responses are inconsistent and the least-squares system cannot be formed.","triggerScenarios":"jnp.polyfit(x, y, deg) with len(y) != len(x); x masked/filtered but y not (or vice versa); x from ravel of a grid paired with an unflattened y.","commonSituations":"Applying the same mask to x but forgetting y (or slicing y's columns only); mixing flattened and unflattened arrays after grid operations; off-by-one trimming of one series.","solutions":["Apply identical filtering/slicing to both: jnp.polyfit(x[mask], y[mask], deg).","Verify shapes first: assert x.shape[0] == y.shape[0].","When using meshgrid outputs, ravel both arrays."],"exampleFix":"// before\nc = jnp.polyfit(x[keep], y, 3)\n// after\nc = jnp.polyfit(x[keep], y[keep], 3)","handlingStrategy":"validation","validationCode":"x, y = jnp.asarray(x), jnp.asarray(y)\nassert x.shape[0] == y.shape[0], (x.shape, y.shape)\nc = jnp.polyfit(x, y, deg)","typeGuard":"def lengths_match(x, y) -> bool:\n    return x.shape[0] == y.shape[0]","tryCatchPattern":null,"preventionTips":["Apply identical masks to x and y","Ravel both arrays from grid operations"],"tags":["jax","numpy","polynomial","polyfit","shape-mismatch"],"backgroundTag":"length-mismatch","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}