【问题标题】:How to calculate score while using SVM?使用 SVM 时如何计算分数?
【发布时间】:2020-09-21 10:31:03
【问题描述】:

我是机器学习的新手,我对 sklearn 关于如何在使用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 值,即

    svm.score(x_test,y_test))
    

【问题讨论】:

  • 你没有在循环中改变 SVC 的 c 参数,它总是 0.6,这就是为什么你总是得到相同的分数
  • 请看我编辑的问题,我已经按照你的指出改了,但我的结果仍然没有改变

标签: machine-learning svm cross-validation


【解决方案1】:

你的问题:

  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 进行比较。这就是测试的想法,您可以根据原始值测试您的预测,看看您的预测有多好/多坏。

【讨论】:

    猜你喜欢
    • 2019-12-13
    • 1970-01-01
    • 2016-05-16
    • 2017-04-29
    • 2012-08-18
    • 2018-06-27
    • 2017-04-30
    • 2015-12-04
    • 2019-11-20
    相关资源
    最近更新 更多