-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
DOC Fix UserWarning in plot_gpr_prior_posterior #29268
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -196,23 +196,30 @@ def plot_gpr_samples(gpr_model, n_samples, ax): | |||||||||
# %% | ||||||||||
# Dot-product kernel | ||||||||||
# .................. | ||||||||||
from sklearn.gaussian_process import GaussianProcessRegressor | ||||||||||
from sklearn.gaussian_process.kernels import ConstantKernel, DotProduct | ||||||||||
from sklearn.preprocessing import StandardScaler | ||||||||||
import matplotlib.pyplot as plt | ||||||||||
|
||||||||||
kernel = ConstantKernel(0.1, (0.01, 10.0)) * ( | ||||||||||
DotProduct(sigma_0=1.0, sigma_0_bounds=(0.1, 10.0)) ** 2 | ||||||||||
) | ||||||||||
gpr = GaussianProcessRegressor(kernel=kernel, random_state=0) | ||||||||||
# Scale the data | ||||||||||
scaler = StandardScaler() | ||||||||||
X_train_scaled = scaler.fit_transform(X_train) | ||||||||||
|
||||||||||
fig, axs = plt.subplots(nrows=2, sharex=True, sharey=True, figsize=(10, 8)) | ||||||||||
# Define the kernel | ||||||||||
kernel = ConstantKernel(0.1, (0.01, 10.0)) * (DotProduct(sigma_0=1.0, sigma_0_bounds=(0.1, 10.0)) ** 2) | ||||||||||
Comment on lines
+208
to
+209
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please avoid unrelated changes.
Suggested change
|
||||||||||
|
||||||||||
# plot prior | ||||||||||
# Increase the number of iterations | ||||||||||
gpr = GaussianProcessRegressor(kernel=kernel, random_state=0, n_restarts_optimizer=10, optimizer='fmin_l_bfgs_b') | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding comments to the code is mostly required when showing the reasoning behind a complex decision, for instance here, a comment would rather explain the need for the "fmin_l_bfgs_b" optimizer. Also, try to keep lines under 88 characters (see the linter comment on this PR). |
||||||||||
|
||||||||||
# Plot prior | ||||||||||
fig, axs = plt.subplots(nrows=2, sharex=True, sharey=True, figsize=(10, 8)) | ||||||||||
plot_gpr_samples(gpr, n_samples=n_samples, ax=axs[0]) | ||||||||||
axs[0].set_title("Samples from prior distribution") | ||||||||||
|
||||||||||
# plot posterior | ||||||||||
gpr.fit(X_train, y_train) | ||||||||||
# Fit the model and plot posterior | ||||||||||
gpr.fit(X_train_scaled, y_train) | ||||||||||
plot_gpr_samples(gpr, n_samples=n_samples, ax=axs[1]) | ||||||||||
axs[1].scatter(X_train[:, 0], y_train, color="red", zorder=10, label="Observations") | ||||||||||
axs[1].scatter(X_train_scaled[:, 0], y_train, color="red", zorder=10, label="Observations") | ||||||||||
axs[1].legend(bbox_to_anchor=(1.05, 1.5), loc="upper left") | ||||||||||
axs[1].set_title("Samples from posterior distribution") | ||||||||||
|
||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
matplotlib.pyplot
is already imported in line 33GaussianProcessRegressor
is already imported in line 100