Skip to content

Commit

Permalink
Use switch for recalculation
Browse files Browse the repository at this point in the history
  • Loading branch information
PicoCentauri committed Apr 24, 2023
1 parent 0966ff6 commit 1c19327
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions examples/pcovr/PCovR-WHODataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from sklearn.kernel_ridge import KernelRidge
from sklearn.linear_model import Ridge, RidgeCV
from sklearn.metrics import r2_score
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV, train_test_split

from skmatter.datasets import load_who_dataset
from skmatter.decomposition import KernelPCovR, PCovR
Expand Down Expand Up @@ -145,38 +145,44 @@
#
# 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 the following code to determine
# the best hyperparameters for the kernel ridge regression via a cross-
# validated grid-search, which determined that the best results came
# from using the following parameters.
#
# 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``.

# from sklearn.model_selection import GridSearchCV
# 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)

gamma = 0.08858667904100832
alpha = 0.0016237767391887243
recalc = False

if recalc:
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)

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

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


# %%
#
# You can rebtain these values by running the following code block

This comment has been minimized.

Copy link
@rosecers

rosecers Apr 24, 2023

Collaborator

^^typo

This comment has been minimized.

Copy link
@PicoCentauri

PicoCentauri Apr 24, 2023

Author Collaborator

ahh leftover

# Kernel Regression
# ^^^^^^^^^^^^^^^^^
#
KernelRidge(**kernel_params, alpha=alpha).fit(X_train, y_train).score(X_test, y_test)


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

# %%
#
# KPCovR
Expand Down

0 comments on commit 1c19327

Please sign in to comment.