【问题标题】:Plotting Roc Curve in SVM在 SVM 中绘制 Roc 曲线
【发布时间】:2016-02-05 12:11:18
【问题描述】:

我正在尝试为 SVM 创建 ROC 曲线,这是我使用的代码:

#learning from training
#tuned <- tune.svm(y~., data=train, gamma = 10^(-6:-1), cost = 10^(1:2))
summary(tuned)


svmmodel<-svm(y~., data=train, method="C-classification",
              kernel="radial", gamma = 0.01, cost = 100,cross=5, probability=TRUE) 

svmmodel

#predicting the test data
svmmodel.predict<-predict(svmmodel,subset(test,select=-y),decision.values=TRUE)
svmmodel.probs<-attr(svmmodel.predict,"decision.values")
svmmodel.class<-predict(svmmodel,test,type="class")
svmmodel.labels<-test$y
#analyzing result
svmmodel.confusion<-confusion.matrix(svmmodel.labels,svmmodel.class)
svmmodel.accuracy<-prop.correct(svmmodel.confusion)

#roc analysis for test data
svmmodel.prediction<-prediction(svmmodel.probs,svmmodel.labels)
svmmodel.performance<-performance(svmmodel.prediction,"tpr","fpr")
svmmodel.auc<-performance(svmmodel.prediction,"auc")@y.values[[1]]

但是ROC曲线的问题是这样的:

【问题讨论】:

标签: r machine-learning svm roc


【解决方案1】:

我在MATLAB - generate confusion matrix from classifier回答了类似的问题
通过使用上面链接中给出的代码,如果您得到如图所示的逆 ROC 曲线,则替换以下行(在链接中给出的代码中):
1.替换链接上给出的代码中的行。

b_pred = (tot_op>=th_vals(i,1)); 

b_pred = (tot_op<=th_vals(i,1));  

2。换行

AUC = sum(0.5*(sens(2:end)+sens(1:end-1)).*(cspec(2:end) - cspec(1:end-1))); 

AUC = sum(0.5*(sens(2:end)+sens(1:end-1)).*(cspec(1:end-1) - cspec(2:end)));

在链接上给出的代码中。

【讨论】:

    【解决方案2】:

    使用 'ROCR' 包可以很容易地做到这一点。 我使用这样的东西来获得 ROC 曲线。

    p1<- predict(svm,test, type="decision")
    pr<-prediction(p1, test$status)
    prf<- performance(pr, measure="tpr",x.measure="fpr")
    plot(prf)
    lines(x = c(0,1), y = c(0,1),col="blue")
    

    【讨论】:

      【解决方案3】:

      你解决问题了吗? 我和你有同样的问题,反向 ROC 和 AUC 是从测试集中获得的。

      就我而言,可以解决对训练数据集进行排序的问题。

      例如,

      train <- train[order(train$y, decreasing = TRUE),]
      

      【讨论】:

        【解决方案4】:

        不要使用decision.values,试试这个:

        fit = f(x,y,probability = TRUE)
        pred = prediction(attr(predict(fit,x_test, probability = TRUE), "probabilities")[,2], test[,colnames(test) == y_name])
        

        【讨论】:

          猜你喜欢
          • 2018-04-01
          • 1970-01-01
          • 2013-08-10
          • 2017-09-11
          • 2016-12-26
          • 1970-01-01
          • 2016-02-04
          • 2019-02-27
          • 2021-03-03
          相关资源
          最近更新 更多