1

我正在尝试将GaussianProcessRegressorin scikit-learn 与 grakel 软件计算的一些图形内核一起使用。下面是我对 100 个图形数据进行 5 折交叉验证的代码。为了测试方便,我已经注释掉了所有与图形相关的行,并改用随机内核矩阵和y值。

from sklearn.model_selection import KFold
from sklearn.utils import check_random_state
from sklearn.gaussian_process import GaussianProcessRegressor as GPR
from sklearn.metrics import mean_squared_error
#from grakel.kernels import WeisfeilerLehman
import numpy as np

def Kfold_CV_GPR(Gs, y, n_iter=4, n_splits=5, random_state=None):
    random_state = check_random_state(random_state)
    kf = KFold(n_splits=n_splits, random_state=random_state, shuffle=True)
    errors = []
    for train_idxs, test_idxs in kf.split(y):
#        gk = WeisfeilerLehman(n_iter=n_iter, normalize=True)
#        K_train = gk.fit_transform(Gs[train_idxs])
#        K_test = gk.transform(Gs[test_idxs])
        K_train = np.random.randn(80, 80)
        K_test = np.random.randn(20, 80)
        gpr = GPR(kernel='precomputed')
        gpr.fit(K_train, y[train_idxs])
        y_pred = gpr.predict(K_test)
        rmse = mean_squared_error(y[test_idxs], y_pred, squared=False)
        errors.append(rmse)
    return -np.mean(errors)

score = Kfold_CV_GPR(Gs=None, y=np.random.randn(100, ), n_iter=4, n_splits=5)
print(score)

但是,我收到以下错误

TypeError: Cannot clone object ''precomputed'' (type <class 'str'>): it does not seem to be a scikit-learn
estimator as it does not implement a 'get_params' method.

当我更改sklearn.gaussian_process.GaussianProcessRegressorsklearn.svm.SVR(支持向量回归)时,我的代码不会抛出任何错误,但由于某种原因它会永远运行。我还测试了类似的分类器,sklearn.svm.SVC并且我的代码运行良好。

有人知道如何在 scikit-learn 中使用预计算内核GaussianProcessRegressor吗?

4

0 回答 0