核回归与线性核回归是否相同?

Is kernel regression the same as linear kernel regression?

我想在 sklearn 中编写线性核回归代码,所以我编写了这段代码:

model = LinearRegression()
weights = rbf_kernel(X_train,X_test)

for i in range(weights.shape[1]):
    model.fit(X_train,y_train,weights[:,i])
    model.predict(X_test[i])

然后我发现sklearn里面有KernelRidge :

model = KernelRidge(kernel='rbf')

model.fit(X_train,y_train)

pred = model.predict(X_train)

我的问题是:
1-这两个代码有什么区别? 2-在 KernelRidge() 之后的 model.fit() 中,我在文档中发现我可以向 fit() 函数添加第三个参数 "weight",如果我已经应用了模型的核函数?

  1. What is the difference between these two code snippets?

基本上,他们没有任何共同点。您的第一个代码片段实现了 线性回归 ,具有任意设置的样本权重。 (你是怎么想出这样调用 rbf_kernel 的?)这仍然只是一个线性模型,仅此而已。您只是简单地(有点随机地)分配了哪些样本是重要的,然后遍历特征(?)。这根本没有意义。总的来说:你用 rbf_kernel 所做的完全是错误的;这完全不是它应该如何使用的(以及为什么当你试图将它传递给 fit 方法时它给你错误,你最终做了一个循环并分别传递每一列)。

将此类模型拟合到余弦数据(因此平均值为 0)的示例:

  1. I found in the documentation for the model.fit() function that comes after KernelRidge() that I can add a third argument, weight. Would I do that if I had already applied a kernel function to the model?

这是实际核方法,核不是样本加权。 (可能会使用核函数来分配权重,但这不是"linear kernel regression"或一般"kernel methods"中核的含义。)核是一种将非线性引入分类器的方法,它来自于事实许多方法(包括线性回归)都可以表示为向量之间的点积,可以用核函数代替,从而导致在不同 space 中解决问题(Reproducing Hilbert Kernel Space),这可能有非常高的复杂性(如 RBF 核引起的连续函数的无限维 space)。

拟合上述相同数据的示例:

from sklearn.linear_model import LinearRegression
from sklearn.kernel_ridge import KernelRidge
import numpy as np
from matplotlib import pyplot as plt

X = np.linspace(-10, 10, 100).reshape(100, 1)
y = np.cos(X)

for model in [LinearRegression(), KernelRidge(kernel='rbf')]:

    model.fit(X, y)
    p = model.predict(X)

    plt.figure()
    plt.title(model.__class__.__name__)
    plt.scatter(X[:, 0], y)
    plt.plot(X, p)

    plt.show()