{"record":{"id":"af86becb6280c89d","repo":"jax-ml/jax","slug":"dataset-input-should-have-multiple-elements","errorCode":null,"errorMessage":"`dataset` input should have multiple elements.","messagePattern":"`dataset` input should have multiple elements\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/stats/kde.py","lineNumber":60,"sourceCode":"    dataset: arraylike, real-valued. Data from which to estimate the distribution.\n      If 1D, shape is (n_data,). If 2D, shape is (n_dimensions, n_data).\n    bw_method: string, scalar, or callable. Either \"scott\", \"silverman\", a scalar\n      value, or a callable function which takes ``self`` as a parameter.\n    weights: arraylike, optional. Weights of the same shape as the dataset.\n  \"\"\"\n  neff: Any\n  dataset: Any\n  weights: Any\n  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))","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/stats/kde.py#L42-L78","documentation":"gaussian_kde requires the dataset to contain more than one element (dataset.size > 1) because a covariance/bandwidth estimate is undefined for a single point. Note atleast_2d is applied first, so even a scalar becomes a 1-element matrix and still fails.","triggerScenarios":"Calling gaussian_kde(jnp.array([1.0])) or gaussian_kde(3.0); passing an empty array also fails the size check.","commonSituations":"Running KDE in a loop/over groups where some group has one sample; feeding a placeholder or dummy scalar during prototyping.","solutions":["Ensure at least 2 data points per KDE fit","Guard group-wise fits: only fit a KDE when len(group) >= 2, else fall back to a parametric density or skip","Check for empty/single-element slices before constructing the object"],"exampleFix":"// before\nkde = gaussian_kde(group)  # group may have 1 element\n// after\nkde = gaussian_kde(group) if group.size > 1 else None","handlingStrategy":"validation","validationCode":"if jnp.asarray(dataset).size < 2:\n    raise ValueError('need >= 2 points for KDE')","typeGuard":"def has_kde_enough_data(dataset) -> bool:\n    return jnp.asarray(dataset).size > 1","tryCatchPattern":null,"preventionTips":["Guard group-wise KDE fits with a minimum-count threshold","Fall back to a parametric density for tiny groups","Log group sizes when iterating over splits"],"tags":["jax","scipy","kde","input-validation"],"backgroundTag":"insufficient-data","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}