{"record":{"id":"c9fd6efd396a5e56","repo":"jax-ml/jax","slug":"expected-a-1-d-array-for-weights","errorCode":null,"errorMessage":"expected a 1-d array for weights","messagePattern":"expected a 1-d array for weights","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/polynomial.py","lineNumber":258,"sourceCode":"  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]\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","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/polynomial.py#L240-L276","documentation":"In polyfit, sample weights w must be a 1-D array with one weight per observation, because they are applied row-wise to the design matrix and responses (lhs *= w[:, None]). A multi-dimensional or scalar-broadcast weight array is rejected with a TypeError.","triggerScenarios":"jnp.polyfit(x, y, deg, w=W) where W.ndim != 1 (e.g. shape (n, 1) column matrix or a per-target (n, k) weight grid); passing a scalar weight instead of a vector of ones.","commonSituations":"Weights loaded from data pipelines as column vectors; reusing per-channel weight matrices from other libraries (sklearn sample_weight is 1-D, but intermediate processing may add axes).","solutions":["Flatten weights: w=W.ravel().","Use None or jnp.ones(len(x)) for unweighted fits instead of a scalar.","Check w.ndim == 1 and w.shape[0] == y.shape[0] before the call."],"exampleFix":"// before\nc = jnp.polyfit(x, y, 3, w=W)  # W.shape == (n, 1)\n// after\nc = jnp.polyfit(x, y, 3, w=W.ravel())","handlingStrategy":"validation","validationCode":"if w is not None:\n    w = jnp.ravel(jnp.asarray(w))\n    assert w.ndim == 1\nc = jnp.polyfit(x, y, deg, w=w)","typeGuard":"def weights_ok(w) -> bool:\n    return w is None or (getattr(w, 'ndim', 0) == 1)","tryCatchPattern":null,"preventionTips":["Ravel column-vector weights","Use None for unweighted fits"],"tags":["jax","numpy","polynomial","polyfit","weights","shape-validation"],"backgroundTag":"invalid-shape-argument","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}