Skip to content

Removing kernel optimization from who example #189

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

Merged
merged 3 commits into from
Apr 24, 2023
Merged
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
38 changes: 25 additions & 13 deletions examples/pcovr/PCovR-WHODataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,42 @@
# Train the Different Kernel DR Techniques
# ----------------------------------------
#
# Below, we obtain the regression errors using a variety of kernel DR techniques.
#
# Select Kernel Hyperparameters
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# In the original publication, we used a cross-validated grid search to determine the
# best hyperparameters for the kernel ridge regression. We do not rerun this expensive
# search in this example but use the obtained parameters for ``gamma`` and ``alpha``.
# You may rerun the calculation locally by setting ``recalc=True``.

param_grid = {"gamma": np.logspace(-8, 3, 20), "alpha": np.logspace(-8, 3, 20)}
clf = KernelRidge(kernel="rbf")

gs = GridSearchCV(estimator=clf, param_grid=param_grid)
gs.fit(X_train, y_train)
gs.best_estimator_
recalc = False

if recalc:
param_grid = {"gamma": np.logspace(-8, 3, 20), "alpha": np.logspace(-8, 3, 20)}

# %%
# Below, we obtain the regression errors using a variety of kernel DR techniques.
#
# Kernel Regression
# ^^^^^^^^^^^^^^^^^
clf = KernelRidge(kernel="rbf")
gs = GridSearchCV(estimator=clf, param_grid=param_grid)
gs.fit(X_train, y_train)

gamma = gs.best_estimator_.gamma
alpha = gs.best_estimator_.alpha
else:
gamma = 0.08858667904100832
alpha = 0.0016237767391887243

kernel_params = {"kernel": "rbf", "gamma": gamma}

gs.best_score_

# %%
#
# Kernel Regression
# ^^^^^^^^^^^^^^^^^

kernel_params = {"kernel": "rbf", "gamma": gs.best_estimator_.gamma}

KernelRidge(**kernel_params, alpha=alpha).fit(X_train, y_train).score(X_test, y_test)

# %%
#
Expand All @@ -179,7 +191,7 @@

kpcovr = KernelPCovR(
n_components=n_components,
regressor=KernelRidge(alpha=gs.best_estimator_.alpha, **kernel_params),
regressor=KernelRidge(alpha=alpha, **kernel_params),
mixing=0.5,
**kernel_params,
).fit(X_train, y_train)
Expand Down