如何在使用SVM的同时计算分数?

问题描述 投票:0回答:1

我是机器学习的新手,我对sklearn的文档有点困惑,如何在使用SVM时获得分数?sklearn.svm.SVC.

这是我的代码

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.30)

for _c in [0.4,0.6,0.8,1.0,1.2,1.4]:
    svm=SVC(C=_c,kernel='linear')
    svm.fit(x_train,y_train)
    result=svm.predict(x_test)
    print('C value is {} and score is {}'.format(_c,svm.score(x_test,y_test)))

这就是输出

C value is 0.4 and score is 0.0091324200913242
C value is 0.6 and score is 0.0091324200913242
C value is 0.8 and score is 0.0091324200913242
C value is 1.0 and score is 0.0091324200913242
C value is 1.2 and score is 0.0091324200913242
C value is 1.4 and score is 0.0091324200913242

我看到所有的分数都是一样的,我的问题是如何确定我的模型的最佳分数?

  1. 我是否应该将预测值传递给svm.score的y值,即

    result=svm.predict(x_test)
    svm.score(x_test,result))
    
  2. 我是否应该把x_test和y_test的值,即x_test和y_test的值传递给svm.score。

    svm.score(x_test,y_test))
    
machine-learning svm cross-validation
1个回答
0
投票

对于你的问题。

  1. 这是不对的,你不能比较特征 x与你的目标 y
  2. 和1中的错误一样。

你必须使用。

for _c in [0.4,0.6,0.8,1.0,1.2,1.4]:
    svm=SVC(C=_c,kernel='linear')
    svm.fit(x_train,y_train)
    result=svm.predict(x_test)
    print('C value is {} and score is {}'.format(_c,svm.score(y_test,result)))

这将比较你的原始目标值 y_test 与您的预测值 result . 这就是测试的思想,你用原始值来测试你的预测,看看你的预测有多好。

© www.soinside.com 2019 - 2024. All rights reserved.