'Return the kernel k(X, Y) and optionally its gradient.
Parameters
X : array, shape (n_samples_X, n_features)
Left argument of the returned kernel k(X, Y)
Y : array, shape (n_samples_Y, n_features), (optional, default=None)
Right argument of the returned kernel k(X, Y). If None, k(X, X)
if evaluated instead.
eval_gradient : bool (optional, default=False)
Determines whether the gradient with respect to the kernel
hyperparameter is determined. Only supported when Y is None.
Returns
K : array, shape (n_samples_X, n_samples_Y)
Kernel k(X, Y)
K_gradient : array (opt.), shape (n_samples_X, n_samples_X, n_dims)
The gradient of the kernel k(X, X) with respect to the
hyperparameter of the kernel. Only returned when eval_gradient
is True.'

def __call__(self, X, Y=None, eval_gradient=False):

    X = np.atleast_2d(X)
    length_scale = _check_length_scale(X, self.length_scale)
    if (Y is None):
        dists = pdist((X / length_scale), metric='sqeuclidean')
        K = np.exp(((-0.5) * dists))
        K = squareform(K)
        np.fill_diagonal(K, 1)
    else:
        if eval_gradient:
            raise ValueError('Gradient    can    only    be    evaluated    when    Y    is    None.')
        dists = cdist((X / length_scale), (Y / length_scale), metric='sqeuclidean')
        K = np.exp(((-0.5) * dists))
    if eval_gradient:
        if self.hyperparameter_length_scale.fixed:
            return (K, np.empty((X.shape[0], X.shape[0], 0)))
        elif ((not self.anisotropic) or (length_scale.shape[0] == 1)):
            K_gradient = (K * squareform(dists))[:, :, np.newaxis]
            return (K, K_gradient)
        elif self.anisotropic:
            K_gradient = (((X[:, np.newaxis, :] - X[np.newaxis, :, :]) ** 2) / (length_scale ** 2))
            K_gradient *= K[..., np.newaxis]
            return (K, K_gradient)
    else:
        return K
