Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions 25 examples/gaussian_process/plot_gpr_prior_posterior.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +199 to +202
Copy link
Member

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 33
GaussianProcessRegressor is already imported in line 100

Suggested change
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import ConstantKernel, DotProduct
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
from sklearn.gaussian_process.kernels import ConstantKernel, DotProduct
from sklearn.preprocessing import StandardScaler


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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid unrelated changes.

Suggested change
# Define the kernel
kernel = ConstantKernel(0.1, (0.01, 10.0)) * (DotProduct(sigma_0=1.0, sigma_0_bounds=(0.1, 10.0)) ** 2)
kernel = ConstantKernel(0.1, (0.01, 10.0)) * (
DotProduct(sigma_0=1.0, sigma_0_bounds=(0.1, 10.0)) ** 2
)


# plot prior
# Increase the number of iterations
gpr = GaussianProcessRegressor(kernel=kernel, random_state=0, n_restarts_optimizer=10, optimizer='fmin_l_bfgs_b')
Copy link
Member

@ArturoAmorQ ArturoAmorQ Jun 17, 2024

Choose a reason for hiding this comment

The 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")

Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.