python - 反向支持向量机 : calculating the predictions

标签 python scikit-learn regression svm

我想知道,给定 svm 回归模型的回归系数,是否可以“手动”计算该模型做出的预测。 更准确地说,假设:

svc = SVR(kernel='rbf', epsilon=0.3, gamma=0.7, C=64)
svc.fit(X_train, y_train)

然后你可以很容易地通过使用获得预测

y_pred = svc.predict(X_test)

我想知道如何通过直接计算得到这个结果。从决策函数开始, Decision function of the RBF kernel 其中,K 是 RBF 核函数,b 是截距,alpha 是对偶系数。

因为我使用 RBF 内核,所以我是这样开始的:

def RBF(x,z,gamma,axis=None):
    return np.exp((-gamma*np.linalg.norm(x-z, axis=axis)**2))


for i in len(svc.support_):
    A[i] = RBF(X_train[i], X_test[0], 0.7)

然后我计算

np.sum(svc._dual_coef_*A)+svc.intercept_

但是,此计算结果与 y_pred 的第一项不同。我怀疑我的推理不完全正确和/或我的代码不是它应该的样子,所以如果这不是正确的问题,我深表歉意。在过去的 2 个小时里,我一直在盯着这个问题看,所以非常感谢任何帮助!

更新

经过更多研究,我发现了以下帖子:Replication of scikit.svm.SRV.predict(X)Calculating decision function of SVM manually . 在第一篇文章中,他们谈论回归,在第二篇文章中谈论分类,但想法保持不变。在这两种情况下,OP 基本上都在问同样的事情,但是当我尝试实现他们的代码时,我总是在步骤中遇到错误

diff = sup_vecs - X_test

形式

ValueError: operands could not be broadcast together with shapes 

(number equal to amount of support vectors,7) (number equal to len(Xtest),7)

我不明白为什么支持向量的数量应该等于测试数据的数量。据我了解,几乎从来没有这种情况。那么谁能阐明应该如何更普遍地解决这个问题,即如何改进代码以使其适用于多维数组?

附言与问题无关,但准确地说:7 是特征数。

最佳答案

你犯的错误是在 for i in len(svc.support_): 这个循环中。

您遍历第一个 n_SV(支持向量数)个训练点,不一定是支持向量。因此,只需遍历 svc.support_vectors_ 即可获得实际的支持向量。您的其余代码保持不变。下面我提供了带有更正的代码。

from sklearn import datasets
from sklearn.svm import SVR

# Load the IRIS dataset for demonstration
iris = datasets.load_iris()
X = iris.data
y = iris.target

# Train-test split
X_train, y_train = X[:140], y[:140]
X_test, y_test = X[140:], y[140:]

print(X.shape, X_train.shape, X_test.shape) # prints (150, 4) (140, 4) (10, 4)

# Fit a rbf kernel SVM
svc = SVR(kernel='rbf', epsilon=0.3, gamma=0.7, C=64)
svc.fit(X_train, y_train)

# Get prediction for a point X_test using train SVM, svc
def get_pred(svc, X_test):

    def RBF(x,z,gamma,axis=None):
        return np.exp((-gamma*np.linalg.norm(x-z, axis=axis)**2))

    A = []
    # Loop over all suport vectors to calculate K(Xi, X_test), for Xi belongs to the set of support vectors
    for x in svc.support_vectors_:
        A.append(RBF(x, X_test, 0.7))
    A = np.array(A)

    return (np.sum(svc._dual_coef_*A)+svc.intercept_)

for i in range(X_test.shape[0]):
    print(get_pred(svc, X_test[i]))
    print(svc.predict(X_test[i].reshape(1,-1))) # The same oputput by both

关于python - 反向支持向量机 : calculating the predictions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51879623/

相关文章:

python - 将python路径放在windows机器上

python - 有没有一种Python式的方法来迭代两个列表的差异?

python - 使用 Scikit-learn 进行加权线性回归

python - 为分类器设置参数并在不拟合的情况下使用它

r - 如何使用向量作为预测变量运行多元线性回归?

r - 错误 : please supply starting values

python - 在方法链接时保留参数默认值

python - venv/pyvenv 中没有 activate_this.py 文件

python - 如何创建包含所有相关文本的单个列?

python - 可视化未出现在 Tensorflow-PyCharm IDE 中