本文整理汇总了Python中sklearn.gaussian_process.GaussianProcessClassifier.log_marginal_likelihood方法的典型用法代码示例。如果您正苦于以下问题:Python GaussianProcessClassifier.log_marginal_likelihood方法的具体用法?Python GaussianProcessClassifier.log_marginal_likelihood怎么用?Python GaussianProcessClassifier.log_marginal_likelihood使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.gaussian_process.GaussianProcessClassifier
的用法示例。
在下文中一共展示了GaussianProcessClassifier.log_marginal_likelihood方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_lml_improving
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessClassifier [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessClassifier import log_marginal_likelihood [as 别名]
def test_lml_improving():
""" Test that hyperparameter-tuning improves log-marginal likelihood. """
for kernel in kernels:
if kernel == fixed_kernel: continue
gpc = GaussianProcessClassifier(kernel=kernel).fit(X, y)
assert_greater(gpc.log_marginal_likelihood(gpc.kernel_.theta),
gpc.log_marginal_likelihood(kernel.theta))
示例2: test_lml_gradient
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessClassifier [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessClassifier import log_marginal_likelihood [as 别名]
def test_lml_gradient(kernel):
# Compare analytic and numeric gradient of log marginal likelihood.
gpc = GaussianProcessClassifier(kernel=kernel).fit(X, y)
lml, lml_gradient = gpc.log_marginal_likelihood(kernel.theta, True)
lml_gradient_approx = \
approx_fprime(kernel.theta,
lambda theta: gpc.log_marginal_likelihood(theta,
False),
1e-10)
assert_almost_equal(lml_gradient, lml_gradient_approx, 3)
示例3: test_converged_to_local_maximum
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessClassifier [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessClassifier import log_marginal_likelihood [as 别名]
def test_converged_to_local_maximum(kernel):
# Test that we are in local maximum after hyperparameter-optimization.
gpc = GaussianProcessClassifier(kernel=kernel).fit(X, y)
lml, lml_gradient = \
gpc.log_marginal_likelihood(gpc.kernel_.theta, True)
assert np.all((np.abs(lml_gradient) < 1e-4) |
(gpc.kernel_.theta == gpc.kernel_.bounds[:, 0]) |
(gpc.kernel_.theta == gpc.kernel_.bounds[:, 1]))
示例4: test_custom_optimizer
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessClassifier [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessClassifier import log_marginal_likelihood [as 别名]
def test_custom_optimizer(kernel):
# Test that GPC can use externally defined optimizers.
# Define a dummy optimizer that simply tests 50 random hyperparameters
def optimizer(obj_func, initial_theta, bounds):
rng = np.random.RandomState(0)
theta_opt, func_min = \
initial_theta, obj_func(initial_theta, eval_gradient=False)
for _ in range(50):
theta = np.atleast_1d(rng.uniform(np.maximum(-2, bounds[:, 0]),
np.minimum(1, bounds[:, 1])))
f = obj_func(theta, eval_gradient=False)
if f < func_min:
theta_opt, func_min = theta, f
return theta_opt, func_min
gpc = GaussianProcessClassifier(kernel=kernel, optimizer=optimizer)
gpc.fit(X, y_mc)
# Checks that optimizer improved marginal likelihood
assert_greater(gpc.log_marginal_likelihood(gpc.kernel_.theta),
gpc.log_marginal_likelihood(kernel.theta))
示例5: test_random_starts
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessClassifier [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessClassifier import log_marginal_likelihood [as 别名]
def test_random_starts():
# Test that an increasing number of random-starts of GP fitting only
# increases the log marginal likelihood of the chosen theta.
n_samples, n_features = 25, 2
rng = np.random.RandomState(0)
X = rng.randn(n_samples, n_features) * 2 - 1
y = (np.sin(X).sum(axis=1) + np.sin(3 * X).sum(axis=1)) > 0
kernel = C(1.0, (1e-2, 1e2)) \
* RBF(length_scale=[1e-3] * n_features,
length_scale_bounds=[(1e-4, 1e+2)] * n_features)
last_lml = -np.inf
for n_restarts_optimizer in range(5):
gp = GaussianProcessClassifier(
kernel=kernel, n_restarts_optimizer=n_restarts_optimizer,
random_state=0).fit(X, y)
lml = gp.log_marginal_likelihood(gp.kernel_.theta)
assert_greater(lml, last_lml - np.finfo(np.float32).eps)
last_lml = lml
示例6: test_lml_precomputed
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessClassifier [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessClassifier import log_marginal_likelihood [as 别名]
def test_lml_precomputed():
# Test that lml of optimized kernel is stored correctly.
for kernel in kernels:
gpc = GaussianProcessClassifier(kernel=kernel).fit(X, y)
assert_almost_equal(gpc.log_marginal_likelihood(gpc.kernel_.theta),
gpc.log_marginal_likelihood(), 7)
示例7: test_lml_improving
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessClassifier [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessClassifier import log_marginal_likelihood [as 别名]
def test_lml_improving(kernel):
# Test that hyperparameter-tuning improves log-marginal likelihood.
gpc = GaussianProcessClassifier(kernel=kernel).fit(X, y)
assert_greater(gpc.log_marginal_likelihood(gpc.kernel_.theta),
gpc.log_marginal_likelihood(kernel.theta))
示例8: GaussianProcessClassifier
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessClassifier [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessClassifier import log_marginal_likelihood [as 别名]
# Generate data
train_size = 50
rng = np.random.RandomState(0)
X = rng.uniform(0, 5, 100)[:, np.newaxis]
y = np.array(X[:, 0] > 2.5, dtype=int)
# Specify Gaussian Processes with fixed and optimized hyperparameters
gp_fix = GaussianProcessClassifier(kernel=1.0 * RBF(length_scale=1.0), optimizer=None)
gp_fix.fit(X[:train_size], y[:train_size])
gp_opt = GaussianProcessClassifier(kernel=1.0 * RBF(length_scale=1.0))
gp_opt.fit(X[:train_size], y[:train_size])
print("Log Marginal Likelihood (initial): %.3f" % gp_fix.log_marginal_likelihood(gp_fix.kernel_.theta))
print("Log Marginal Likelihood (optimized): %.3f" % gp_opt.log_marginal_likelihood(gp_opt.kernel_.theta))
print(
"Accuracy: %.3f (initial) %.3f (optimized)"
% (
accuracy_score(y[:train_size], gp_fix.predict(X[:train_size])),
accuracy_score(y[:train_size], gp_opt.predict(X[:train_size])),
)
)
print(
"Log-loss: %.3f (initial) %.3f (optimized)"
% (
log_loss(y[:train_size], gp_fix.predict_proba(X[:train_size])[:, 1]),
log_loss(y[:train_size], gp_opt.predict_proba(X[:train_size])[:, 1]),
)
示例9: RBF
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessClassifier [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessClassifier import log_marginal_likelihood [as 别名]
X = rng.randn(200, 2)
Y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0)
# fit the model
plt.figure(figsize=(10, 5))
kernels = [1.0 * RBF(length_scale=1.0), 1.0 * DotProduct(sigma_0=1.0)**2]
for i, kernel in enumerate(kernels):
clf = GaussianProcessClassifier(kernel=kernel, warm_start=True).fit(X, Y)
# plot the decision function for each datapoint on the grid
Z = clf.predict_proba(np.vstack((xx.ravel(), yy.ravel())).T)[:, 1]
Z = Z.reshape(xx.shape)
plt.subplot(1, 2, i + 1)
image = plt.imshow(Z, interpolation='nearest',
extent=(xx.min(), xx.max(), yy.min(), yy.max()),
aspect='auto', origin='lower', cmap=plt.cm.PuOr_r)
contours = plt.contour(xx, yy, Z, levels=[0], linewidths=2,
linetypes='--')
plt.scatter(X[:, 0], X[:, 1], s=30, c=Y, cmap=plt.cm.Paired)
plt.xticks(())
plt.yticks(())
plt.axis([-3, 3, -3, 3])
plt.colorbar(image)
plt.title("%s\n Log-Marginal-Likelihood:%.3f"
% (clf.kernel_, clf.log_marginal_likelihood(clf.kernel_.theta)),
fontsize=12)
plt.tight_layout()
plt.show()
示例10: plot
# 需要导入模块: from sklearn.gaussian_process import GaussianProcessClassifier [as 别名]
# 或者: from sklearn.gaussian_process.GaussianProcessClassifier import log_marginal_likelihood [as 别名]
def plot(df, options):
UNIQ_GROUPS = df.group.unique()
UNIQ_GROUPS.sort()
sns.set_style("white")
grppal = sns.color_palette("Set2", len(UNIQ_GROUPS))
print '# UNIQ GROUPS', UNIQ_GROUPS
cent_stats = df.groupby(
['position', 'group', 'side']).apply(stats_per_group)
cent_stats.reset_index(inplace=True)
import time
from sklearn import preprocessing
from sklearn.gaussian_process import GaussianProcessRegressor, GaussianProcessClassifier
from sklearn.gaussian_process.kernels import Matern, WhiteKernel, ExpSineSquared, ConstantKernel, RBF
ctlDF = cent_stats[ cent_stats['group'] == 0 ]
TNRightDF = cent_stats[ cent_stats['group'] != 0]
TNRightDF = TNRightDF[TNRightDF['side'] == 'right']
dataDf = pd.concat([ctlDF, TNRightDF], ignore_index=True)
print dataDf
yDf = dataDf['group'] == 0
yDf = yDf.astype(int)
y = yDf.values
print y
print y.shape
XDf = dataDf[['position', 'values']]
X = XDf.values
X = preprocessing.scale(X)
print X
print X.shape
# kernel = ConstantKernel() + Matern(length_scale=mean, nu=3 / 2) + \
# WhiteKernel(noise_level=1e-10)
kernel = 1**2 * Matern(length_scale=1, nu=1.5) + \
WhiteKernel(noise_level=0.1)
figure = plt.figure(figsize=(10, 6))
stime = time.time()
gp = GaussianProcessClassifier(kernel)
gp.fit(X, y)
print gp.kernel_
print gp.log_marginal_likelihood()
print("Time for GPR fitting: %.3f" % (time.time() - stime))
# create a mesh to plot in
h = 0.1
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
plt.figure(figsize=(10, 5))
# Plot the predicted probabilities. For that, we will assign a color to
# each point in the mesh [x_min, m_max]x[y_min, y_max].
Z = gp.predict_proba(np.c_[xx.ravel(), yy.ravel()])
Z = Z[:,1]
print Z
print Z.shape
# Put the result into a color plot
Z = Z.reshape((xx.shape[0], xx.shape[1]))
print Z.shape
plt.imshow(Z, extent=(x_min, x_max, y_min, y_max), origin="lower")
# Plot also the training points
plt.scatter(X[:, 0], X[:, 1], c=np.array(["r", "g"])[y])
plt.xlabel('position')
plt.ylabel('normalized val')
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.xticks(())
plt.yticks(())
plt.title("%s, LML: %.3f" %
("TN vs. Control", gp.log_marginal_likelihood(gp.kernel_.theta)))
plt.tight_layout()
if options.title:
plt.suptitle(options.title)
if options.output:
plt.savefig(options.output, dpi=150)
#.........这里部分代码省略.........