jax-ml/jax · error · ValueError
`dataset` input should have multiple elements.
Error message
`dataset` input should have multiple elements.
What it means
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.
Source
Thrown at jax/_src/scipy/stats/kde.py:60
dataset: arraylike, real-valued. Data from which to estimate the distribution.
If 1D, shape is (n_data,). If 2D, shape is (n_dimensions, n_data).
bw_method: string, scalar, or callable. Either "scott", "silverman", a scalar
value, or a callable function which takes ``self`` as a parameter.
weights: arraylike, optional. Weights of the same shape as the dataset.
"""
neff: Any
dataset: Any
weights: Any
covariance: Any
inv_cov: Any
def __init__(self, dataset, bw_method: BwMethod = None, weights=None):
check_arraylike("gaussian_kde", dataset)
dataset = jnp.atleast_2d(dataset)
if dtypes.issubdtype(lax.dtype(dataset), np.complexfloating):
raise NotImplementedError("gaussian_kde does not support complex data")
if not dataset.size > 1:
raise ValueError("`dataset` input should have multiple elements.")
d, n = dataset.shape
if weights is not None:
check_arraylike("gaussian_kde", weights)
dataset, weights = promote_dtypes_inexact(dataset, weights)
weights = jnp.atleast_1d(weights)
weights /= jnp.sum(weights)
if weights.ndim != 1:
raise ValueError("`weights` input should be one-dimensional.")
if len(weights) != n:
raise ValueError("`weights` input should be of length n")
else:
dataset, = promote_dtypes_inexact(dataset)
weights = jnp.full(n, 1.0 / n, dtype=dataset.dtype)
self._setattr("dataset", dataset)
self._setattr("weights", weights)
neff = self._setattr("neff", 1 / jnp.sum(weights**2))View on GitHub (pinned to 1e1c6a8fc0)
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
Example fix
// before kde = gaussian_kde(group) # group may have 1 element // after kde = gaussian_kde(group) if group.size > 1 else None
Defensive patterns
Strategy: validation
Validate before calling
if jnp.asarray(dataset).size < 2:
raise ValueError('need >= 2 points for KDE') Type guard
def has_kde_enough_data(dataset) -> bool:
return jnp.asarray(dataset).size > 1 Prevention
- 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
When it happens
Trigger: Calling gaussian_kde(jnp.array([1.0])) or gaussian_kde(3.0); passing an empty array also fails the size check.
Common situations: Running KDE in a loop/over groups where some group has one sample; feeding a placeholder or dummy scalar during prototyping.
Related errors
- n must be a non-negative integer.
- gaussian_kde does not support complex data
- `weights` input should be one-dimensional.
- `weights` input should be of length n
- `bw_method` should be 'scott', 'silverman', a scalar, or a c
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/af86becb6280c89d.
Report an issue: GitHub.