Skip to content

FIX RuntimeWarning by dividing by zero in test_kernel_gradient #19396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Feb 11, 2021
6 changes: 3 additions & 3 deletions sklearn/gaussian_process/kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -1686,9 +1686,9 @@ def __call__(self, X, Y=None, eval_gradient=False):
D = squareform(dists**2)[:, :, np.newaxis]

if self.nu == 0.5:
K_gradient = K[..., np.newaxis] * D \
/ np.sqrt(D.sum(2))[:, :, np.newaxis]
K_gradient[~np.isfinite(K_gradient)] = 0
denominator = np.sqrt(D.sum(axis=2))[:, :, np.newaxis]
K_gradient = K[..., np.newaxis] * \
np.divide(D, denominator, where=denominator != 0)
elif self.nu == 1.5:
K_gradient = \
3 * D * np.exp(-np.sqrt(3 * D.sum(-1)))[..., np.newaxis]
Expand Down