From 1c1932776bafb0dc04877ab6b5fca7d8340d8f22 Mon Sep 17 00:00:00 2001 From: Philip Loche Date: Mon, 24 Apr 2023 22:16:47 +0200 Subject: [PATCH] Use switch for recalculation --- examples/pcovr/PCovR-WHODataset.py | 44 +++++++++++++++++------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/examples/pcovr/PCovR-WHODataset.py b/examples/pcovr/PCovR-WHODataset.py index 4ff09b22d..bb44df49e 100644 --- a/examples/pcovr/PCovR-WHODataset.py +++ b/examples/pcovr/PCovR-WHODataset.py @@ -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 @@ -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 # 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