python - 如何使用自定义 SVM 内核?

标签 python machine-learning scikit-learn svm gaussian

我想用 Python 实现我自己的高斯核,只是为了练习。我在用着: sklearn.svm.SVC(kernel=my_kernel) 但我真的不明白发生了什么。

我希望函数 my_kernel 以 X 矩阵的列作为参数被调用,而不是我用 X, X 调用它> 作为参数。查看示例,事情并没有更清楚。

我错过了什么?

这是我的代码:

'''
Created on 15 Nov 2014

@author: Luigi
'''
import scipy.io
import numpy as np
from sklearn import svm
import matplotlib.pyplot as plt

def svm_class(fileName):

    data = scipy.io.loadmat(fileName)
    X = data['X']
    y = data['y']

    f = svm.SVC(kernel = 'rbf', gamma=50, C=1.0)
    f.fit(X,y.flatten())
    plotData(np.hstack((X,y)), X, f)

    return

def plotData(arr, X, f):

    ax = plt.subplot(111)

    ax.scatter(arr[arr[:,2]==0][:,0], arr[arr[:,2]==0][:,1], c='r', marker='o', label='Zero')
    ax.scatter(arr[arr[:,2]==1][:,0], arr[arr[:,2]==1][:,1], c='g', marker='+', label='One')

    h = .02  # step size in the mesh
    # create a mesh to plot in
    x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                         np.arange(y_min, y_max, h))


    # Plot the decision boundary. For that, we will assign a color to each
    # point in the mesh [x_min, m_max]x[y_min, y_max].
    Z = f.predict(np.c_[xx.ravel(), yy.ravel()])

    # Put the result into a color plot
    Z = Z.reshape(xx.shape)
    plt.contour(xx, yy, Z)



    plt.xlim(np.min(arr[:,0]), np.max(arr[:,0]))
    plt.ylim(np.min(arr[:,1]), np.max(arr[:,1]))
    plt.show()
    return


def gaussian_kernel(x1,x2):
    sigma = 0.5
    return np.exp(-np.sum((x1-x2)**2)/(2*sigma**2))

if __name__ == '__main__':

    fileName = 'ex6data2.mat'
    svm_class(fileName)

最佳答案

阅读上面的答案以及其他一些问题和站点(12345)后,我将其放在一起作为高斯内核在svm.SVC() 中.

使用 kernel=precomputed 调用 svm.SVC()

然后计算一个Gram Matrix a.k.a. 内核矩阵(通常缩写为 K)。

然后将此 Gram 矩阵用作 svm.SVC().fit() 的第一个参数( X) :

我从 following code 开始:

C=0.1
model = svmTrain(X, y, C, "gaussian")

svmTrain() 中调用 sklearn.svm.SVC() ,然后是 sklearn.svm.SVC().fit():

from sklearn import svm

if kernelFunction == "gaussian":
    clf = svm.SVC(C = C, kernel="precomputed")
    return clf.fit(gaussianKernelGramMatrix(X,X), y)

Gram 矩阵计算 - 用作 sklearn.svm.SVC().fit() 的参数 - 在 gaussianKernelGramMatrix() 中完成:

import numpy as np

def gaussianKernelGramMatrix(X1, X2, K_function=gaussianKernel):
    """(Pre)calculates Gram Matrix K"""

    gram_matrix = np.zeros((X1.shape[0], X2.shape[0]))
    for i, x1 in enumerate(X1):
        for j, x2 in enumerate(X2):
            gram_matrix[i, j] = K_function(x1, x2)
    return gram_matrix

它使用 gaussianKernel()获得 x1 和 x2 之间的径向基函数核 ( a measure of similarity based on a gaussian distribution centered on x1 with sigma=0.1 ):

def gaussianKernel(x1, x2, sigma=0.1):

    # Ensure that x1 and x2 are column vectors
    x1 = x1.flatten()
    x2 = x2.flatten()

    sim = np.exp(- np.sum( np.power((x1 - x2),2) ) / float( 2*(sigma**2) ) )

    return sim

然后,一旦使用这个自定义内核训练了模型,我们就可以使用 "the [custom] kernel between the test data and the training data" 进行预测。 :

predictions = model.predict( gaussianKernelGramMatrix(Xval, X) )

简而言之,要使用自定义 SVM 高斯核,您可以使用此代码段:

import numpy as np
from sklearn import svm

def gaussianKernelGramMatrixFull(X1, X2, sigma=0.1):
    """(Pre)calculates Gram Matrix K"""

    gram_matrix = np.zeros((X1.shape[0], X2.shape[0]))
    for i, x1 in enumerate(X1):
        for j, x2 in enumerate(X2):
            x1 = x1.flatten()
            x2 = x2.flatten()
            gram_matrix[i, j] = np.exp(- np.sum( np.power((x1 - x2),2) ) / float( 2*(sigma**2) ) )
    return gram_matrix

X=...
y=...
Xval=...

C=0.1
clf = svm.SVC(C = C, kernel="precomputed")
model = clf.fit( gaussianKernelGramMatrixFull(X,X), y )

p = model.predict( gaussianKernelGramMatrixFull(Xval, X) )

关于python - 如何使用自定义 SVM 内核?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26962159/

相关文章:

python - 如何在 sklearn 中使用带有自定义估算器的 GridSearchCV?

python - 导入 skimage 不是有效的 Win32 应用程序 python3

python - Scikit-learn 的 GridSearchCV 中的 Grid_scores_ 是什么意思

machine-learning - 如何以不同的方式使用词嵌入/word2vec ..?使用实际的物理词典

python - 处理多个类别输入和可变大小的组作为神经网络的输入

python - ValueError : Number of labels is 1. 使用 silhouette_score 时有效值为 2 到 n_samples - 1(含)

python - 在 numpy 中查找对角线总和(更快)

python - 如何在裁剪后检测和对齐倾斜图像

python - 获取 django 对象的一对多设置属性的名称

python - 尝试用更大的字典更新字典