本文整理汇总了Python中sklearn.gaussian_process.GaussianProcessRegressor.predict方法的典型用法代码示例。如果您正苦于以下问题:Python GaussianProcessRegressor.predict方法的具体用法?Python GaussianProcessRegressor.predict怎么用?Python GaussianProcessRegressor.predict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.gaussian_process.GaussianProcessRegressor
的用法示例。
在下文中一共展示了GaussianProcessRegressor.predict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_y_normalization
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessRegressor [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessRegressor import predict [as 别名]
def test_y_normalization():
""" Test normalization of the target values in GP
Fitting non-normalizing GP on normalized y and fitting normalizing GP
on unnormalized y should yield identical results
"""
y_mean = y.mean(0)
y_norm = y - y_mean
for kernel in kernels:
# Fit non-normalizing GP on normalized y
gpr = GaussianProcessRegressor(kernel=kernel)
gpr.fit(X, y_norm)
# Fit normalizing GP on unnormalized y
gpr_norm = GaussianProcessRegressor(kernel=kernel, normalize_y=True)
gpr_norm.fit(X, y)
# Compare predicted mean, std-devs and covariances
y_pred, y_pred_std = gpr.predict(X2, return_std=True)
y_pred = y_mean + y_pred
y_pred_norm, y_pred_std_norm = gpr_norm.predict(X2, return_std=True)
assert_almost_equal(y_pred, y_pred_norm)
assert_almost_equal(y_pred_std, y_pred_std_norm)
_, y_cov = gpr.predict(X2, return_cov=True)
_, y_cov_norm = gpr_norm.predict(X2, return_cov=True)
assert_almost_equal(y_cov, y_cov_norm)
示例2: test_no_fit_default_predict
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessRegressor [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessRegressor import predict [as 别名]
def test_no_fit_default_predict():
# Test that GPR predictions without fit does not break by default.
default_kernel = (C(1.0, constant_value_bounds="fixed") *
RBF(1.0, length_scale_bounds="fixed"))
gpr1 = GaussianProcessRegressor()
_, y_std1 = gpr1.predict(X, return_std=True)
_, y_cov1 = gpr1.predict(X, return_cov=True)
gpr2 = GaussianProcessRegressor(kernel=default_kernel)
_, y_std2 = gpr2.predict(X, return_std=True)
_, y_cov2 = gpr2.predict(X, return_cov=True)
assert_array_almost_equal(y_std1, y_std2)
assert_array_almost_equal(y_cov1, y_cov2)
示例3: plot_gp
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessRegressor [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessRegressor import predict [as 别名]
def plot_gp(x_min, x_max, x, y, train_features, train_labels):
fig = plt.figure(figsize=(16, 10))
fig.suptitle('Gaussian Process and Utility Function After {} Steps'.format(len(train_features)), fontdict={'size':30})
gs = gridspec.GridSpec(2, 1, height_ratios=[3, 1])
axis = plt.subplot(gs[0])
acq = plt.subplot(gs[1])
gp = GaussianProcessRegressor(
kernel=Matern(nu=2.5),
n_restarts_optimizer=25, )
gp.fit(train_features, train_labels)
mu, sigma = gp.predict(x, return_std=True)
axis.plot(x, y, linewidth=3, label='Target')
axis.plot(train_features.flatten(), train_labels, 'D', markersize=8, label=u'Observations', color='r')
axis.plot(x, mu, '--', color='k', label='Prediction')
axis.fill(np.concatenate([x, x[::-1]]),
np.concatenate([mu - 1.9600 * sigma, (mu + 1.9600 * sigma)[::-1]]),
alpha=.6, fc='c', ec='None', label='95% confidence interval')
axis.set_xlim((x_min, x_max))
axis.set_ylim((None, None))
axis.set_ylabel('f(x)', fontdict={'size':20})
axis.set_xlabel('x', fontdict={'size':20})
bounds = np.asarray([[x_min, x_max]])
acquisition_fucntion_kappa = 5
mean, std = gp.predict(x, return_std=True)
acquisition_fucntion_values = mean + acquisition_fucntion_kappa * std
acq.plot(x, acquisition_fucntion_values, label='Utility Function', color='purple')
acq.plot(x[np.argmax(acquisition_fucntion_values)], np.max(acquisition_fucntion_values), '*', markersize=15,
label=u'Next Best Guess', markerfacecolor='gold', markeredgecolor='k', markeredgewidth=1)
acq.set_xlim((x_min, x_max))
acq.set_ylim((0, np.max(acquisition_fucntion_values) + 0.5))
acq.set_ylabel('Utility', fontdict={'size':20})
acq.set_xlabel('x', fontdict={'size':20})
axis.legend(loc=2, bbox_to_anchor=(1.01, 1), borderaxespad=0.)
acq.legend(loc=2, bbox_to_anchor=(1.01, 1), borderaxespad=0.)
示例4: test_predict_cov_vs_std
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessRegressor [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessRegressor import predict [as 别名]
def test_predict_cov_vs_std():
""" Test that predicted std.-dev. is consistent with cov's diagonal."""
for kernel in kernels:
gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y)
y_mean, y_cov = gpr.predict(X2, return_cov=True)
y_mean, y_std = gpr.predict(X2, return_std=True)
assert_almost_equal(np.sqrt(np.diag(y_cov)), y_std)
示例5: test_gpr_interpolation
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessRegressor [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessRegressor import predict [as 别名]
def test_gpr_interpolation(kernel):
# Test the interpolating property for different kernels.
gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y)
y_pred, y_cov = gpr.predict(X, return_cov=True)
assert_almost_equal(y_pred, y)
assert_almost_equal(np.diag(y_cov), 0.)
示例6: test_gpr_interpolation
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessRegressor [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessRegressor import predict [as 别名]
def test_gpr_interpolation():
"""Test the interpolating property for different kernels."""
for kernel in kernels:
gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y)
y_pred, y_cov = gpr.predict(X, return_cov=True)
assert_true(np.allclose(y_pred, y))
assert_true(np.allclose(np.diag(y_cov), 0.))
示例7: test_duplicate_input
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessRegressor [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessRegressor import predict [as 别名]
def test_duplicate_input():
""" Test GPR can handle two different output-values for the same input. """
for kernel in kernels:
gpr_equal_inputs = GaussianProcessRegressor(kernel=kernel, alpha=1e-2)
gpr_similar_inputs = GaussianProcessRegressor(kernel=kernel, alpha=1e-2)
X_ = np.vstack((X, X[0]))
y_ = np.hstack((y, y[0] + 1))
gpr_equal_inputs.fit(X_, y_)
X_ = np.vstack((X, X[0] + 1e-15))
y_ = np.hstack((y, y[0] + 1))
gpr_similar_inputs.fit(X_, y_)
X_test = np.linspace(0, 10, 100)[:, None]
y_pred_equal, y_std_equal = gpr_equal_inputs.predict(X_test, return_std=True)
y_pred_similar, y_std_similar = gpr_similar_inputs.predict(X_test, return_std=True)
assert_almost_equal(y_pred_equal, y_pred_similar)
assert_almost_equal(y_std_equal, y_std_similar)
示例8: test_y_multioutput
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessRegressor [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessRegressor import predict [as 别名]
def test_y_multioutput():
""" Test that GPR can deal with multi-dimensional target values"""
y_2d = np.vstack((y, y*2)).T
# Test for fixed kernel that first dimension of 2d GP equals the output
# of 1d GP and that second dimension is twice as large
kernel = RBF(length_scale=1.0)
gpr = GaussianProcessRegressor(kernel=kernel, optimizer=None,
normalize_y=False)
gpr.fit(X, y)
gpr_2d = GaussianProcessRegressor(kernel=kernel, optimizer=None,
normalize_y=False)
gpr_2d.fit(X, y_2d)
y_pred_1d, y_std_1d = gpr.predict(X2, return_std=True)
y_pred_2d, y_std_2d = gpr_2d.predict(X2, return_std=True)
_, y_cov_1d = gpr.predict(X2, return_cov=True)
_, y_cov_2d = gpr_2d.predict(X2, return_cov=True)
assert_almost_equal(y_pred_1d, y_pred_2d[:, 0])
assert_almost_equal(y_pred_1d, y_pred_2d[:, 1] / 2)
# Standard deviation and covariance do not depend on output
assert_almost_equal(y_std_1d, y_std_2d)
assert_almost_equal(y_cov_1d, y_cov_2d)
y_sample_1d = gpr.sample_y(X2, n_samples=10)
y_sample_2d = gpr_2d.sample_y(X2, n_samples=10)
assert_almost_equal(y_sample_1d, y_sample_2d[:, 0])
# Test hyperparameter optimization
for kernel in kernels:
gpr = GaussianProcessRegressor(kernel=kernel, normalize_y=True)
gpr.fit(X, y)
gpr_2d = GaussianProcessRegressor(kernel=kernel, normalize_y=True)
gpr_2d.fit(X, np.vstack((y, y)).T)
assert_almost_equal(gpr.kernel_.theta, gpr_2d.kernel_.theta, 4)
示例9: test_sample_statistics
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessRegressor [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessRegressor import predict [as 别名]
def test_sample_statistics():
""" Test that statistics of samples drawn from GP are correct."""
for kernel in kernels:
gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y)
y_mean, y_cov = gpr.predict(X2, return_cov=True)
samples = gpr.sample_y(X2, 300000)
# More digits accuracy would require many more samples
assert_almost_equal(y_mean, np.mean(samples, 1), 2)
assert_almost_equal(np.diag(y_cov) / np.diag(y_cov).max(), np.var(samples, 1) / np.diag(y_cov).max(), 1)
示例10: test_prior
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessRegressor [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessRegressor import predict [as 别名]
def test_prior(kernel):
# Test that GP prior has mean 0 and identical variances.
gpr = GaussianProcessRegressor(kernel=kernel)
y_mean, y_cov = gpr.predict(X, return_cov=True)
assert_almost_equal(y_mean, 0, 5)
if len(gpr.kernel.theta) > 1:
# XXX: quite hacky, works only for current kernels
assert_almost_equal(np.diag(y_cov), np.exp(kernel.theta[0]), 5)
else:
assert_almost_equal(np.diag(y_cov), 1, 5)
示例11: fit_GP
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessRegressor [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessRegressor import predict [as 别名]
def fit_GP(x_train):
y_train = gaussian(x_train, mu, sig).ravel()
# Instanciate a Gaussian Process model
kernel = C(1.0, (1e-3, 1e3)) * RBF(1, (1e-2, 1e2))
gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=9)
# Fit to data using Maximum Likelihood Estimation of the parameters
gp.fit(x_train, y_train)
# Make the prediction on the meshed x-axis (ask for MSE as well)
y_pred, sigma = gp.predict(x, return_std=True)
return y_train, y_pred, sigma
示例12: test_GP_brownian_motion
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessRegressor [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessRegressor import predict [as 别名]
def test_GP_brownian_motion(self):
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel as C
# add data
t = np.linspace(0, 10, 100)
#
# Instanciate a Gaussian Process model
# kernel = C(1.0, (1e-3, 1e3)) * RBF(10, (1e-2, 1e2))
# Instanciate a Gaussian Process model
kernel = lambda x, y: 1. * min(x, y)
# kernel = C(1.0, (1e-3, 1e3)) * RBF(10, (1e-2, 1e2))
gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=9)
# gp = GaussianProcessRegressor()
# Fit to data using Maximum Likelihood Estimation of the parameters
X = np.atleast_2d(t).T
gp.fit(X, y)
# gp = GaussianProcessRegressor()
# Fit to data using Maximum Likelihood Estimation of the parameters
# gp.fit(t, y)
# Make the prediction on the meshed x-axis (ask for MSE as well)
# y_star, err_y_star = gp.predict(t, return_std=True)
# Make the prediction on the meshed x-axis (ask for MSE as well)
y_pred, sigma = gp.predict(t, return_std=True)
fig = plt.figure()
ax = fig.add_axes((0.1, 0.3, 0.8, 0.65))
ax.invert_yaxis()
ax.plot(t, y, color='blue', label='L bol', lw=2.5)
ax.errorbar(t, y, yerr=yerr, fmt='o', color='blue', label='%s obs.')
#
# ax.plot(t, y_star, color='red', ls='--', lw=1.5, label='GP')
ax.plot(t, y_pred, '-', color='gray')
# ax.fill_between(t, y_star - 2 * err_y_star, y_star + 2 * err_y_star, color='gray', alpha=0.3)
ax.fill(np.concatenate([t, t[::-1]]),
np.concatenate([y_pred - 1.9600 * sigma,
(y_pred + 1.9600 * sigma)[::-1]]),
alpha=.5, fc='b', ec='None', label='95% confidence interval')
plt.show()
示例13: plot_gaussian
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessRegressor [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessRegressor import predict [as 别名]
def plot_gaussian(data, col):
'''
Plots the gaussian process regression with a characteristic length scale
of 10 years. Essentially this highlights the 'slow trend' in the data.
Parameters
----------
data: dataframe
pandas dataframe containing 'date', 'linMean' which is the average
runtime and 'linSD' which is the standard deviation.
col: string
the color in which the plot the data
'''
#extract the results from the dataframe
Year = np.array(data[u'date'].tolist())
Mean = np.array(data[u'linMean'].tolist())
SD = np.array(data[u'linSD'].tolist())
#initialize the gaussian process. Note that the process is calculated with a
#length scale of 10years to give the 'slow trend' in the results.
length_scale = 10.
kernel = 1.* RBF(length_scale)
gp = GaussianProcessRegressor(kernel=kernel, sigma_squared_n=(SD) ** 2, \
normalize_y=True)
#now fit the data and get the predicted mean and standard deviation
#Note: for reasons that are unclear, GaussianProcessRegressor won't take 1D
#arrays so the data are converted to 2D and then converted back for plotting
gp.fit(np.atleast_2d(Year).T, np.atleast_2d(Mean).T)
Year_array = np.atleast_2d(np.linspace(min(Year)-2, max(Year)+2, 100)).T
Mean_prediction, SD_prediction = gp.predict(Year_pred, return_std=True)
Year_array=Year_array.ravel()
Mean_prediction=Mean_prediction.ravel()
#plot the predicted best fit
plt.plot(Year_array, Mean_prediction, col, alpha=1)
#plot the 95% confidence interval
plt.fill_between(Year_array, (Mean_prediction - 1.9600 * SD_prediction), \
y2=(Mean_prediction + 1.9600 * SD_prediction), alpha=0.5, \
color=col)
plt.draw()
示例14: test_K_inv_reset
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessRegressor [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessRegressor import predict [as 别名]
def test_K_inv_reset(kernel):
y2 = f(X2).ravel()
# Test that self._K_inv is reset after a new fit
gpr = GaussianProcessRegressor(kernel=kernel).fit(X, y)
assert hasattr(gpr, '_K_inv')
assert gpr._K_inv is None
gpr.predict(X, return_std=True)
assert gpr._K_inv is not None
gpr.fit(X2, y2)
assert gpr._K_inv is None
gpr.predict(X2, return_std=True)
gpr2 = GaussianProcessRegressor(kernel=kernel).fit(X2, y2)
gpr2.predict(X2, return_std=True)
# the value of K_inv should be independent of the first fit
assert_array_equal(gpr._K_inv, gpr2._K_inv)
示例15: GP
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessRegressor [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessRegressor import predict [as 别名]
class GP(BaseTuner):
def __init__(self, tunables, gridding=0, r_minimum=2):
"""
Extra args:
r_minimum: the minimum number of past results this selector needs in
order to use gaussian process for prediction. If not enough
results are present during a fit(), subsequent calls to
propose() will revert to uniform selection.
"""
super(GP, self).__init__(tunables, gridding=gridding)
self.r_minimum = r_minimum
def fit(self, X, y):
""" Use X and y to train a Gaussian process. """
super(GP, self).fit(X, y)
# skip training the process if there aren't enough samples
if X.shape[0] < self.r_minimum:
return
self.gp = GaussianProcessRegressor(normalize_y=True)
self.gp.fit(X, y)
def predict(self, X):
if self.X.shape[0] < self.r_minimum:
# we probably don't have enough
logger.warn('GP: not enough data, falling back to uniform sampler')
return Uniform(self.tunables).predict(X)
y, stdev = self.gp.predict(X, return_std=True)
return np.array(list(zip(y, stdev)))
def _acquire(self, predictions):
"""
Predictions from the GP will be in the form (prediction, error).
The default acquisition function returns the index with the highest
predicted value, not factoring in error.
"""
return np.argmax(predictions[:, 0])