'returning cov and std from sklearn gaussian process?

I can return the covariance or the standard deviation from a GP using sklearn, like:

y, cov = gp.predict(Xpredict,return_cov=True)
y, std = gp.predict(Xpredict,return_std=True)

but how can I return both without calling gp.predict twice?

This

y, cov, std = gp.predict(Xpredict, return_cov=True, return_std=True)

doesn't work



Solution 1:[1]

According to scikit-learn documentation, you cannot do it in one call using predict()

Note that at most one of the two can be requested.

Solution 2:[2]

You can return covariance and then extract standard deviation as follow:

import numpy as np

y_mean, y_cov = gp.predict(X, return_cov=True)
y_std = np.sqrt(np.diag(y_cov))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sentence
Solution 2 Yasser Sami