I'm trying to optimize an unsupervised kernel PCA algorithm. Here is some context:
Another approach, this time entirely unsupervised, is to select the kernel and hyperparameters that yield the lowest reconstruction error. However, reconstruction is not as easy as with linear PCA
....
Fortunately, it is possible to find a point in the original space that would map close to the reconstructed point. This is called the reconstruction pre-image. Once you have this pre-image, you can measure its squared distance to the original instance. You can then select the kernel and hyperparameters that minimize this reconstruction pre-image error.
One solution is to train a supervised regression model, with the projected instances as the training set and the original instances as the targets.
Now you can use grid search with cross-validation to find the kernel and hyperparameters that minimize this pre-image reconstruction error.
The code provided in the book to perfom the reconstruction without cross validation is:
rbf_pca = KernelPCA(n_components = 2, kernel="rbf", gamma=0.0433,fit_inverse_transform=True)
X_reduced = rbf_pca.fit_transform(X)
X_preimage = rbf_pca.inverse_transform(X_reduced)
>>> from sklearn.metrics import mean_squared_error
>>> mean_squared_error(X, X_preimage)
32.786308795766132
My question is, how do i go about implementing cross validation to tune the kernel and hyperparameters to minimze the pre-image reconstruction error?
Here is my go at it so far:
from sklearn.metrics import mean_squared_error
from sklearn.decomposition import KernelPCA
mean_squared_error(X, X_preimage)
kpca=KernelPCA(fit_inverse_transform=True, n_jobs=-1)
from sklearn.model_selection import GridSearchCV
param_grid = [{
"kpca__gamma": np.linspace(0.03, 0.05, 10),
"kpca__kernel": ["rbf", "sigmoid", "linear", "poly"]
}]
grid_search = GridSearchCV(clf, param_grid, cv=3, scoring='mean_squared_error')
X_reduced = kpca.fit_transform(X)
X_preimage = kpca.inverse_transform(X_reduced)
grid_search.fit(X,X_preimage)
Thank you