{"record":{"id":"ef8fa628f263c794","repo":"jax-ml/jax","slug":"y-has-more-than-2-dimensions","errorCode":null,"errorMessage":"y has more than 2 dimensions","messagePattern":"y has more than 2 dimensions","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/lax_numpy.py","lineNumber":9180,"sourceCode":"    Array([[ 1., -1.],\n           [-1.,  1.]], dtype=float32)\n\n    In general, the entries of the covariance matrix may be any positive\n    or negative real value. For example, here is the covariance of 100\n    points drawn from a 3-dimensional standard normal distribution:\n\n    >>> key = jax.random.key(0)\n    >>> x = jax.random.normal(key, shape=(3, 100))\n    >>> with jnp.printoptions(precision=2):\n    ...   print(jnp.cov(x))\n    [[0.9  0.03 0.1 ]\n     [0.03 1.   0.01]\n     [0.1  0.01 0.85]]\n  \"\"\"\n  if y is not None:\n    m, y = util.promote_args_inexact(\"cov\", m, y)\n    if y.ndim > 2:\n      raise ValueError(\"y has more than 2 dimensions\")\n  else:\n    m, = util.promote_args_inexact(\"cov\", m)\n\n  if m.ndim > 2:\n    raise ValueError(\"m has more than 2 dimensions\")  # same as numpy error\n\n  if dtype is not None and not dtypes.issubdtype(dtype, np.inexact):\n    raise ValueError(f\"cov: dtype must be a subclass of float or complex; got {dtype=}\")\n\n  X = atleast_2d(m)\n  if not rowvar and m.ndim != 1:\n    X = X.T\n  if X.shape[0] == 0:\n    return array([]).reshape(0, 0)\n\n  if y is not None:\n    y_arr = atleast_2d(y)\n    if not rowvar and y_arr.shape[0] != 1:","sourceCodeStart":9162,"sourceCodeEnd":9198,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/lax_numpy.py#L9162-L9198","documentation":"jnp.cov computes covariance and, when an optional second variable array y is supplied, promotes m and y to inexact dtypes and requires y to have at most 2 dimensions. A y with ndim > 2 raises ValueError('y has more than 2 dimensions'), matching NumPy's error.","triggerScenarios":"jnp.cov(m, y) where y is 3D or higher, e.g. an image batch of shape (N, H, W) passed as y.","commonSituations":"Feeding stacked/multidimensional tensors (e.g. batches of images or time-series windows) as the y argument without reshaping to 1D/2D.","solutions":["Reshape y to at most 2D before calling: y.reshape(-1, y.shape[-1])","If y is redundant, pass y=None","Compute covariance per-slice with vmap over the extra dimensions"],"exampleFix":"// before\njnp.cov(m, y_3d)  # ValueError\n// after\njnp.cov(m, y_3d.reshape(-1, y_3d.shape[-1]))","handlingStrategy":"validation","validationCode":"if y is not None:\n    y = jnp.asarray(y)\n    assert y.ndim <= 2, 'y must be 1D or 2D'\njnp.cov(m, y)","typeGuard":"def cov_ready_y(y) -> bool:\n    return y is None or jnp.asarray(y).ndim <= 2","tryCatchPattern":null,"preventionTips":["Reshape extra tensor dims before cov","Use vmap for batched covariance","Keep observation data 2D"],"tags":["jax","statistics","covariance","shape-validation"],"backgroundTag":"shape-validation-failed","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}