jax-ml/jax · error · RuntimeError
incompatible numbers of samples and fweights
Error message
incompatible numbers of samples and fweights
What it means
When frequency weights are given to jnp.cov, their length must equal the number of observations (X.shape[1]). If np.shape(fweights)[0] != X.shape[1], RuntimeError('incompatible numbers of samples and fweights') is raised.
Source
Thrown at jax/_src/numpy/lax_numpy.py:9214
if y is not None:
y_arr = atleast_2d(y)
if not rowvar and y_arr.shape[0] != 1:
y_arr = y_arr.T
X = concatenate((X, y_arr), axis=0)
if X.shape[1] == 0:
cov_shape = () if X.shape[0] == 1 else (X.shape[0], X.shape[0])
return array_creation.full(cov_shape, np.nan, dtype=X.dtype)
if ddof is None:
ddof = 1 if bias == 0 else 0
w: Array | None = None
if fweights is not None:
fweights = util.ensure_arraylike("cov", fweights)
if np.ndim(fweights) > 1:
raise RuntimeError("cannot handle multidimensional fweights")
if np.shape(fweights)[0] != X.shape[1]:
raise RuntimeError("incompatible numbers of samples and fweights")
if not issubdtype(fweights.dtype, np.integer):
raise TypeError("fweights must be integer.")
# Ensure positive fweights; note that numpy raises an error on negative fweights.
w = abs(fweights)
if aweights is not None:
aweights = util.ensure_arraylike("cov", aweights)
if np.ndim(aweights) > 1:
raise RuntimeError("cannot handle multidimensional aweights")
if np.shape(aweights)[0] != X.shape[1]:
raise RuntimeError("incompatible numbers of samples and aweights")
# Ensure positive aweights: note that numpy raises an error for negative aweights.
aweights = abs(aweights)
w = asarray(aweights if w is None else w * aweights)
if dtype is not None:
X = X.astype(dtype)
w = w.astype(dtype) if w is not None else w
View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Resize weights to match the observation count: fweights = fweights[:m.shape[1]]
- Transpose m or set rowvar=False if the orientation is flipped
- Verify fweights.shape[0] == m.shape[-1] before calling
Example fix
// before jnp.cov(m, fweights=w) # len(w) != m.shape[1] // after assert w.shape[0] == m.shape[-1] jnp.cov(m, fweights=w)
Defensive patterns
Strategy: validation
Validate before calling
n_obs = m.shape[-1]
assert jnp.shape(fweights)[0] == n_obs, f'fweights len {jnp.shape(fweights)[0]} != {n_obs} samples'
jnp.cov(m, fweights=fweights) Prevention
- Size weights to observations, not variables
- Mind rowvar orientation
- Recompute weights whenever data is filtered
When it happens
Trigger: jnp.cov(m, fweights=np.arange(5)) when m has 3 observations (m.shape[1] == 3), or weights sized to the number of variables instead of samples.
Common situations: Confusing variables x observations orientation (rowvar semantics); computing weights over a filtered subset of the data while passing the full m.
Related errors
- incompatible numbers of samples and aweights
- cannot handle multidimensional fweights
- fweights must be integer.
- cannot handle multidimensional aweights
- expected w and y to have the same length
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/7376920f509c8b5c.
Report an issue: GitHub.