在 python 中为 scikit-learn 高斯内核使用两个输入X 和 Y

问题描述

我正在尝试使用 python 中的 sklearn 为内核岭回归构建高斯内核。
例如,这是我使用 RBF 内核的示例。

from sklearn.datasets import load_iris
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
X,y = load_iris(return_X_y=True)
kernel = 1.0 * RBF(1.0)
gpc = GaussianProcessClassifier(kernel=kernel,random_state=0).fit(X,y)
gpc.predict(X)

但是,在内核的__call__函数中,似乎不能只取X作为输入,所以作为额外的输入Y(认为None):__call__(X,Y=None,eval_gradient=False)。 我发现的所有例子都只是将 X 作为执行内核的参数。 是否有使用所有两个参数 X 和 Y 来构建内核的示例,以及在这种情况下拟合和预测函数的外观?

这是我正在尝试构建的内核类

class KrrInteractKernel2(GenericKernelMixin,Kernel):
    def __init__(self,z=None):
        self.z = z # this does nothing,_init_ function is required

    def __call__(self,X,eval_gradient=False):
        """Return the kernel k(X,Y) and optionally its gradient.
        Parameters
        ----------
        X : array-like of shape (n_samples_X,n_features) or list of object
            Left argument of the returned kernel k(X,Y)
        Y : array-like of shape (n_samples_X,n_features) or list of object,\
            default=None
            Right argument of the returned kernel k(X,Y). If None,k(X,X)
            is evaluated instead.
        eval_gradient : bool,default=False
            Determines whether the gradient with respect to the log of
            the kernel hyperparameter is computed.
            Only supported when Y is None.
        Returns
        -------
        K : ndarray of shape (n_samples_X,n_samples_Y)
            Kernel k(X,Y)
        K_gradient : ndarray of shape (n_samples_X,n_samples_X,n_dims),\
            optional
            The gradient of the kernel k(X,X) with respect to the log of the
            hyperparameter of the kernel. Only returned when eval_gradient
            is True.
        """
        if Y is None:
            Y = X
        elif eval_gradient:
            raise ValueError("Gradient can only be evaluated when Y is None.")

        for i in np.unique(Y):
            x_ = X[np.where(Y == i)]
            n_ = x_.shape[0]
            m_ = x_ @ x_.T / n_
            if i==1:
                K = m_
            else:
                K = block_diag(K,m_)

        if eval_gradient:
            if not self.hyperparameter_constant_value.fixed:
                return (K,np.full((_num_samples(X),_num_samples(X),1),self.constant_value,dtype=np.array(self.constant_value).dtype))
            else:
                return K,np.empty((_num_samples(X),0))
        else:
            return K

    def diag(self,X):
        return np.diag(X)

    def is_stationary(self):
        return False

    def clone_with_theta(self,theta):
        cloned = clone(self)
        cloned.theta = theta
        return cloned

我想在 XY 函数中使用 fitpredict。 我希望是这样的:

kr = KernelRidge(alpha=0.1,kernel=krrInteractKernel)
kr.fit([X,Y],yobs)
kr.predict([X,yobs)

非常感谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...