[Python] Custom SVM Polynomial kernel

Discussão em 'Python' iniciado por Stack, Março 31, 2025.

  1. Stack

    Stack Membro Participativo

    I got asked as an assignment to develop a custom polynomial (degree = 3,4,5) kernel for SVM and compare its accuracy to the in-built poly kernel of sklearn (should be almost the same). I tried to follow the polynomial kernel definition but my results seems not to be quite similar, this is my code:

    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,"%")


    The accuracy results is the following:

    • The accuracy with in-built 4D polynomial kernel is: 56.99999999999999 %
    • The accuracy with kernel is: 59.0 %

    If I change the polynomial grade to 3 or 5 it changes even more, so I do not know if I am doing something wrong or simply is not possible to match the in-built accuracy.

    Continue reading...

Compartilhe esta Página