{"record":{"id":"d89b0888e21e10a8","repo":"jax-ml/jax","slug":"expected-1d-or-2d-array-for-y","errorCode":null,"errorMessage":"expected 1D or 2D array for y","messagePattern":"expected 1D or 2D array for y","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/polynomial.py","lineNumber":243,"sourceCode":"    >>> p, C = jnp.polyfit(x, y, 2, cov=True)\n    >>> p.shape, C.shape\n    ((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]","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/polynomial.py#L225-L261","documentation":"polyfit accepts y either as a 1-D array of responses or a 2-D array where columns are separate responses to fit simultaneously. Any other rank (scalar after promotion, or 3-D+) breaks the shape contract with the Vandermonde design matrix and is rejected with a TypeError.","triggerScenarios":"jnp.polyfit(x, Y, deg) with Y.ndim == 3 (e.g. images or batched series); a 0-d y scalar.","commonSituations":"Fitting multiple channels that arrive as (T, C, B) instead of (T, C); passing a batched array of targets from a training pipeline without first squeezing the batch axis.","solutions":["Reshape y to (len(x), n_targets): y.reshape(len(x), -1).","Squeeze accidental extra axes (e.g. y[:, :, 0]).","For higher-rank data, vmap polyfit over the extra axes."],"exampleFix":"// before\nc = jnp.polyfit(x, Y, 3)  # Y.shape == (T, C, B)\n// after\nc = jax.vmap(lambda y: jnp.polyfit(x, y, 3))(Y.reshape(len(x), C * B).reshape(len(x), -1))\n// or simply: c = jnp.polyfit(x, Y.reshape(len(x), -1), 3)","handlingStrategy":"validation","validationCode":"y = jnp.asarray(y)\nif y.ndim not in (1, 2):\n    y = y.reshape(len(x), -1)\nc = jnp.polyfit(x, y, deg)","typeGuard":"def y_rank_ok(y) -> bool:\n    return y.ndim in (1, 2)","tryCatchPattern":null,"preventionTips":["Reshape multi-channel targets to (n_samples, n_targets)","Squeeze accidental batch axes from y"],"tags":["jax","numpy","polynomial","polyfit","shape-validation"],"backgroundTag":"invalid-shape-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}