jax-ml/jax · error · TypeError

fweights must be integer.

Error message

fweights must be integer.

What it means

Frequency weights in jnp.cov represent integer replication counts, so their dtype must be a subclass of np.integer. If fweights is float or another type, TypeError('fweights must be integer.') is raised, matching NumPy.

Source

Thrown at jax/_src/numpy/lax_numpy.py:9216

    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

  avg, w_sum = reductions.average(X, axis=1, weights=w, returned=True)
  w_sum = w_sum[0]

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Cast to integer: fweights=w.astype(int)
  2. Use aweights for fractional/float weights instead
  3. Generate counts with integer ops (//, sum of ints) upstream

Example fix

// before
jnp.cov(m, fweights=jnp.array([1.0, 2.0]))  # TypeError
// after
jnp.cov(m, fweights=jnp.array([1, 2]))
// or for fractional weights:
jnp.cov(m, aweights=jnp.array([0.5, 1.5]))
Defensive patterns

Strategy: type-guard

Validate before calling

if fweights is not None:
    fweights = jnp.asarray(fweights)
    if not jnp.issubdtype(fweights.dtype, jnp.integer):
        fweights = fweights.astype(jnp.int32)  # or move to aweights
jnp.cov(m, fweights=fweights)

Type guard

def integer_weights(w) -> bool:
    return jnp.issubdtype(jnp.asarray(w).dtype, jnp.integer)

Prevention

When it happens

Trigger: jnp.cov(m, fweights=np.array([1.0, 2.0, 1.0])) — float weights; or weights produced by count/normalization math that yields floats.

Common situations: Normalized or fractional weights (e.g. inverse-frequency weights) passed as fweights when they belong in aweights; reading weights from float-typed files.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/3577a14b086d506b. Report an issue: GitHub.