{"record":{"id":"b6d36831f877e5ee","repo":"jax-ml/jax","slug":"weights-input-should-be-one-dimensional","errorCode":null,"errorMessage":"`weights` input should be one-dimensional.","messagePattern":"`weights` input should be one-dimensional\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/stats/kde.py","lineNumber":69,"sourceCode":"  covariance: Any\n  inv_cov: Any\n\n  def __init__(self, dataset, bw_method: BwMethod = None, weights=None):\n    check_arraylike(\"gaussian_kde\", dataset)\n    dataset = jnp.atleast_2d(dataset)\n    if dtypes.issubdtype(lax.dtype(dataset), np.complexfloating):\n      raise NotImplementedError(\"gaussian_kde does not support complex data\")\n    if not dataset.size > 1:\n      raise ValueError(\"`dataset` input should have multiple elements.\")\n\n    d, n = dataset.shape\n    if weights is not None:\n      check_arraylike(\"gaussian_kde\", weights)\n      dataset, weights = promote_dtypes_inexact(dataset, weights)\n      weights = jnp.atleast_1d(weights)\n      weights /= jnp.sum(weights)\n      if weights.ndim != 1:\n        raise ValueError(\"`weights` input should be one-dimensional.\")\n      if len(weights) != n:\n        raise ValueError(\"`weights` input should be of length n\")\n    else:\n      dataset, = promote_dtypes_inexact(dataset)\n      weights = jnp.full(n, 1.0 / n, dtype=dataset.dtype)\n\n    self._setattr(\"dataset\", dataset)\n    self._setattr(\"weights\", weights)\n    neff = self._setattr(\"neff\", 1 / jnp.sum(weights**2))\n\n    bw_method = \"scott\" if bw_method is None else bw_method\n    if bw_method == \"scott\":\n      factor = jnp.power(neff, -1. / (d + 4))\n    elif bw_method == \"silverman\":\n      factor = jnp.power(neff * (d + 2) / 4.0, -1. / (d + 4))\n    elif jnp.isscalar(bw_method) and not isinstance(bw_method, str):\n      factor = cast(Array, bw_method)\n    elif callable(bw_method):","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/stats/kde.py#L51-L87","documentation":"When weights are passed to gaussian_kde they must be 1-D after jnp.atleast_1d, i.e. one weight per sample point along the last axis of the (d, n) dataset. A 2-D weights array raises this error. Note weights are normalized in place before the check, and a 2-D input survives normalization only to be rejected here.","triggerScenarios":"Passing weights shaped (d, n) or (n, 1) instead of (n,) when dataset has shape (d, n).","commonSituations":"Using per-dimension weights (unsupported — KDE weights are per-sample); forgetting to squeeze weights produced by broadcasting or from a column-vector-like array.","solutions":["Flatten/squeeze weights to shape (n,): weights = weights.squeeze() or weights.ravel()","If you truly need per-dimension importance, resample or transform the data instead of the weights","Verify weights length matches dataset.shape[1]"],"exampleFix":"// before\nkde = gaussian_kde(data, weights=w)  # w.shape == (n, 1)\n// after\nkde = gaussian_kde(data, weights=w.ravel())","handlingStrategy":"validation","validationCode":"weights = jnp.ravel(jnp.asarray(weights))\nn = jnp.atleast_2d(jnp.asarray(dataset)).shape[1]\nassert weights.ndim == 1 and weights.shape[0] == n","typeGuard":"def weights_valid(weights, dataset) -> bool:\n    w = jnp.atleast_1d(jnp.asarray(weights))\n    n = jnp.atleast_2d(jnp.asarray(dataset)).shape[1]\n    return w.ndim == 1 and w.shape[0] == n","tryCatchPattern":null,"preventionTips":["Always ravel weights before passing","Remember weights are per-sample, not per-dimension","Compute weights after final data filtering"],"tags":["jax","scipy","kde","weights","shape-validation"],"backgroundTag":"invalid-array-shape","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}