Quantcast
Channel: Active questions tagged kernel - Stack Overflow
Viewing all articles
Browse latest Browse all 6334

Custom Kernel in sklearn, gaussian exponentiated Kernel as function of implemented Kernels

$
0
0

I want to use the kernel ridge regression in sklearn with the gaussian exponentiated kernel. Which is of the form of K(x, y) = e^{-\gamma ||x-y||^2 + \beta x^T y}.

To do this I want to use the implemented kernels of sklearn, as it is the product of the usual RBF Kernel with the linear kernel evaluated in the RBF kernel. I therefore define my custom Kernel as:

import numpy as npfrom sklearn.kernel_ridge import KernelRidgefrom sklearn.metrics import pairwisedef gaussian_exponentiated_kernel(X, Y=None, gamma = None, beta = None):    return pairwise.rbf_kernel(X, Y, gamma) * pairwise.rbf_kernel(beta * pairwise.linear_kernel(X, Y))

I then use the dummy code for KRR in sklearn:

n_samples, n_features = 10, 5rng = np.random.RandomState(0)y = rng.randn(n_samples)X = rng.randn(n_samples, n_features)clf = KernelRidge(kernel=gaussian_exponentiated_kernel)clf.fit(X, y)

I then get the following error message which I do not fully understand:

ValueError: Expected 2D array, got 1D array instead:array=[0.14404357 1.45427351 0.76103773 0.12167502 0.44386323].Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

Evaluating the kernel function on arrays of matching size works fine, so I do not see where the dimension issue comes from.


Viewing all articles
Browse latest Browse all 6334

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>