我被要求為 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,它會發生更大的變化,所以我不知道我是否做錯了什么,或者根本不可能匹配內置精度。
謝謝你的幫助
uj5u.com熱心網友回復:
您必須查看 poly case 的定義
coef = 0 and 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/317855.html
