【问题标题】:Custom SVM Polynomial kernel sklearn自定义 SVM 多项式内核 sklearn
【发布时间】:2021-12-03 00:07:49
【问题描述】:

我被要求为 SVM 开发一个自定义多项式(度数 = 3,4,5)内核,并将其精度与 sklearnkit 的内置多内核进行比较(应该几乎相同) 我尝试遵循多项式内核定义,但我的结果似乎不太相似,这是我的代码:

def poly_kernel_fn(X, Y):
# Implement a polynomial kernel
#  - args: 2 numpy arrays of shape [n_samples, n_features]
#  - returns: computed kernel matrix of shape [n_samples, n_samples]

K = np.zeros((X.shape[0],Y.shape[0]))
K = (X.dot(Y.T) + 1)**4
return K

clfpoly = svm.SVC(kernel='poly', degree=4)
clfpoly.fit(X_train, y_train)
zpoly = clfpoly.predict(X_test)
print("The accuracy with in-built 3D polynomial kernel is: ",accuracy_score(y_test, zpoly)*100,"%")

clf = svm.SVC(kernel=poly_kernel_fn)
clf.fit(X_train, y_train)
z = clf.predict(X_test)
print("The accuracy with custom rbf kernel is: ",accuracy_score(y_test, z)*100,"%")

准确率结果如下:

  • 内置 4D 多项式内核的精度为:56.99999999999999 %
  • 使用内核的准确率是:59.0 %

如果我将多项式等级更改为 3 或 5,它的变化会更大,所以我不知道我做错了什么,或者根本无法匹配内置精度。

感谢您的帮助

【问题讨论】:

    标签: python machine-learning svm polynomial-math


    【解决方案1】:

    你得看一下poly case的定义

    https://scikit-learn.org/stable/modules/svm.html#svm-kernels

    coef = 0 和 gamma = 1/(n_features*.var()) 然后你就可以得到相同的

    from sklearn.datasets import make_classification
    from sklearn import svm
    import numpy as np
    
    gamma = None
       
    def poly_kernel_fn(X, Y):
       
       K = np.zeros((X.shape[0],Y.shape[0]))
       K = (gamma*X.dot(Y.T))**4
       return K
       
       
    
    if __name__=="__main__":
        
        X, Y = make_classification(10, 5)  # random data
        
        clf1 = svm.SVC(kernel='poly', degree=4) # built in function
        
        clf1.fit(X, Y)
        print("built in score  = ", clf1.score(X,Y))
        
        gamma = 1/(5*X.var())
        
        clf2 = svm.SVC(kernel=poly_kernel_fn)
        
        clf2.fit(X, Y)
        print("custom in score = ", clf2.score(X,Y))
    
    In [9]: run main.py
    built in score  =  0.8
    custom in score =  0.8
    

    【讨论】:

    • 谢谢,我意识到我忘记了伽玛值。这是我一直在寻找的解决方案。再次感谢
    猜你喜欢
    • 2014-05-08
    • 2014-04-28
    • 2021-12-24
    • 2018-03-07
    • 2015-06-02
    • 2018-10-29
    • 2015-01-13
    • 2017-07-26
    • 2015-08-06
    相关资源
    最近更新 更多