{"record":{"id":"d7fba7338f04fa87","repo":"jax-ml/jax","slug":"expected-1d-vector-for-x","errorCode":null,"errorMessage":"expected 1D vector for x","messagePattern":"expected 1D vector for x","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/polynomial.py","lineNumber":239,"sourceCode":"\n    If ``cov=True`` and ``full=False``, returns a tuple of arrays having\n    polynomial coefficients and covariance matrix.\n\n    >>> 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:","sourceCodeStart":221,"sourceCodeEnd":257,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/polynomial.py#L221-L257","documentation":"polyfit fits a polynomial along a 1-D independent variable: it builds vander(x, order), which is only defined for a 1-D x. Passing multi-dimensional x (2-D grid, batched samples) is a TypeError because the Vandermonde construction and the least-squares system would be ambiguous.","triggerScenarios":"jnp.polyfit(X, y, deg) where X has shape (m, n) (e.g. a meshgrid output); passing paired (x, y) points as a 2-D array to the first argument.","commonSituations":"Confusing polyfit's signature with sklearn-style fit(features, target); using the X output of np.meshgrid directly instead of a flattened coordinate array.","solutions":["Flatten x (and correspondingly y) so both are 1-D and length-matched: jnp.polyfit(X.ravel(), Y.ravel(), deg).","For multivariate fitting, use jax.scipy or a custom linear solve on stacked basis features, not polyfit.","Check x.ndim == 1 before the call."],"exampleFix":"// before\nc = jnp.polyfit(X, Z, 2)  # X from meshgrid, shape (m, n)\n// after\nc = jnp.polyfit(X.ravel(), Z.ravel(), 2)","handlingStrategy":"validation","validationCode":"x = jnp.asarray(x)\nif x.ndim != 1:\n    x = x.ravel()\ny = y.reshape(-1) if x.size == y.size else y\nc = jnp.polyfit(x, y, deg)","typeGuard":"def is_1d(x) -> bool:\n    return getattr(x, 'ndim', 0) == 1","tryCatchPattern":null,"preventionTips":["Flatten meshgrid outputs before fitting","polyfit is 1-D only; use custom least squares for surfaces"],"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"}