Sklearn SVM vs Matlab SVM



问题:我需要训练分类器(以MATLAB)进行分类以对多个信号噪声进行分类。

因此,我使用FITCECOC训练了MATLAB中的多类SVM,并获得了92%的精度。

然后,我使用python中的Sklearn.svm训练了一个多类SVM,但是看来我使用参数,我无法获得超过69%的准确性。

30%的数据被保留并用来验证培训。混淆矩阵可以在下面看到。

matlab混淆矩阵

python混淆矩阵

因此,如果有人在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)

任何帮助或建议都非常感谢。

在大量拉我的头发之后,答案是在这里找到的:http://neerajkumar.org/writings/svm/

当输入使用标准标准器()缩放时,SVM.SVC现在产生的结果优于MATLAB !!

相关内容

  • 没有找到相关文章

最新更新