问题:我需要训练一个分类器(在matlab中)来分类多级信号噪声。
因此,我使用fitcecoc在matlab中训练了一个多类SVM,并获得了92%的准确度。
然后我在python中使用sklearn.svm.svc训练了一个多类SVM,但似乎无论如何摆弄参数,我都无法达到69%以上的准确度。
30%的数据被阻止并用于验证培训。混淆矩阵可以在下面看到。
因此,如果任何人对svm.svc多类培训有一些经验或建议,并且可以在我的代码中看到问题,或者有一个建议,那将非常感激。
Python代码:
import numpy as np
from sklearn import svm
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
#from sklearn import preprocessing
#### SET fitting parameters here
C = 100
gamma = 1e-8
#### SET WEIGHTS HERE
C0_Weight = 1*C
C1_weight = 1*C
C2_weight = 1*C
C3_weight = 1*C
C4_weight = 1*C
#####
X = np.genfromtxt('data/features.csv', delimiter=',')
Y = np.genfromtxt('data/targets.csv', delimiter=',')
print 'feature data is of size: ' + str(X.shape)
print 'target data is of size: ' + str(Y.shape)
# SPLIT X AND Y INTO TRAINING AND TEST SET
test_size = 0.3
X_train, x_test, Y_train, y_test = train_test_split(X, Y,
... test_size=test_size, random_state=0)
svc = svm.SVC(C=C,kernel='rbf', gamma=gamma, class_weight = {0:C0_Weight,
... 1:C1_weight, 2:C2_weight, 3:C3_weight, 4:C4_weight},cache_size = 1000)
svc.fit(X_train, Y_train)
scores = cross_val_score(svc, X_train, Y_train, cv=10)
print scores
print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))
Out = svc.predict(x_test)
np.savetxt("data/testPredictions.csv", Out, delimiter=",")
np.savetxt("data/testTargets.csv", y_test, delimiter=",")
# calculate accuracy in test data
Hits = 0
HitsOverlap = 0
for idx, val in enumerate(Out):
Hits += int(y_test[idx]==Out[idx])
HitsOverlap += int(y_test[idx]==Out[idx]) + int(y_test[idx]==
... (Out[idx]-1)) + int(y_test[idx]==(Out[idx]+1))
print "Accuracy in testset: ", Hits*100/(11595*test_size)
print "Accuracy in testset w. overlap: ", HitsOverlap*100/(11595*test_size)
那些好奇我如何获得参数,他们被发现与GridSearchCV(并将准确度从40%提高到69)
非常感谢任何帮助或建议。
答案 0 :(得分:1)
在拉了我的头发之后,答案就在这里找到:http://neerajkumar.org/writings/svm/
当使用StandardScaler()缩放输入时,svm.svc现在可以为matlab生成出色的结果!!