自定义 SVM 多项式内核 sklearn
Custom SVM Polynomial kernel sklearn
我被要求分配一项任务,为 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,它会发生更大的变化,所以我不知道是我做错了什么,还是根本无法匹配内置精度。
感谢您的帮助
你得看看
的poly case的定义
https://scikit-learn.org/stable/modules/svm.html#svm-kernels
系数 = 0 和伽玛 = 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
我被要求分配一项任务,为 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,它会发生更大的变化,所以我不知道是我做错了什么,还是根本无法匹配内置精度。
感谢您的帮助
你得看看
的poly case的定义https://scikit-learn.org/stable/modules/svm.html#svm-kernels
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