【发布时间】: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