{"record":{"id":"a1380bdc5e5a15e3","repo":"jax-ml/jax","slug":"mean-does-not-have-dimension-self-d","errorCode":null,"errorMessage":"mean does not have dimension {self.d}","messagePattern":"mean does not have dimension (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/scipy/stats/kde.py","lineNumber":148,"sourceCode":"\n  def evaluate(self, points):\n    \"\"\"Evaluate the Gaussian KDE on the given points.\"\"\"\n    check_arraylike(\"evaluate\", points)\n    points = self._reshape_points(points)\n    result = _gaussian_kernel_eval(False, self.dataset.T, self.weights[:, None],\n                                   points.T, self.inv_cov)\n    return result[:, 0]\n\n  def __call__(self, points):\n    return self.evaluate(points)\n\n  def integrate_gaussian(self, mean, cov):\n    \"\"\"Integrate the distribution weighted by a Gaussian.\"\"\"\n    mean = jnp.atleast_1d(jnp.squeeze(mean))\n    cov = jnp.atleast_2d(cov)\n\n    if mean.shape != (self.d,):\n      raise ValueError(f\"mean does not have dimension {self.d}\")\n    if cov.shape != (self.d, self.d):\n      raise ValueError(f\"covariance does not have dimension {self.d}\")\n\n    chol = linalg.cho_factor(self.covariance + cov)\n    norm = jnp.sqrt(2 * np.pi)**self.d * jnp.prod(jnp.diag(chol[0]))\n    norm = 1.0 / norm\n    return _gaussian_kernel_convolve(chol, norm, self.dataset, self.weights,\n                                     mean)\n\n  @api.jit\n  def integrate_box_1d(self, low, high):\n    \"\"\"Integrate the distribution over the given limits.\"\"\"\n    if self.d != 1:\n      raise ValueError(\"integrate_box_1d() only handles 1D pdfs\")\n    if np.ndim(low) != 0 or np.ndim(high) != 0:\n      raise ValueError(\n          \"the limits of integration in integrate_box_1d must be scalars\")\n    sigma = jnp.squeeze(jnp.sqrt(self.covariance))","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/scipy/stats/kde.py#L130-L166","documentation":"In gaussian_kde.integrate_gaussian, mean is squeezed then atleast_1d'd and must end up with shape exactly (self.d,). Passing a mean with extra/missing components, or one that squeezes to a scalar when d == 1 but was passed with a spurious dimension (or vice versa), triggers this error.","triggerScenarios":"integrate_gaussian(mean, cov) with mean of shape (d+1,), (), (1, 1) when d == 1 after squeeze, or a batched mean of shape (B, d).","commonSituations":"Reusing a mean vector computed for a different dataset dimensionality; passing nested single-element arrays from upstream math that squeeze unexpectedly.","solutions":["Reshape mean explicitly to (kde.d,): mean = jnp.reshape(mean, (kde.d,))","Check kde.d via kde.dataset.shape[0] and build mean to match","Avoid batching here; use vmap over single calls if multiple means are needed"],"exampleFix":"// before\nkde.integrate_gaussian(mu, Sigma)  # mu.shape == (1, d)\n// after\nkde.integrate_gaussian(jnp.squeeze(mu, axis=0), Sigma)","handlingStrategy":"validation","validationCode":"mean = jnp.reshape(jnp.asarray(mean), (kde.d,))","typeGuard":"def mean_shape_ok(mean, kde) -> bool:\n    return jnp.asarray(mean).shape == (kde.d,) or jnp.squeeze(mean).shape == (kde.d,)","tryCatchPattern":null,"preventionTips":["Reshape mean against kde.d explicitly","Avoid batching integrate_gaussian; use vmap","Keep dimensionality metadata alongside KDE objects"],"tags":["jax","scipy","kde","shape-validation"],"backgroundTag":"invalid-array-shape","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}