SVM分类后如何绘制AUC曲线

R中使用SVM构建分类器时,怎么才能绘制AUC曲线呢,找了好久没有找到相应的函数。

请先 登录 后评论

1 个回答

admin

给你一个例子:

准备数据

1、data:一个表达谱数据矩阵,行为样本,列为基因
2、risk:一个向量,各样本的分类向量

library('e1071')
library(ROCR)
 
train_data <- data
train_label <- risk
train_label <- as.factor(train_label)
obj <- tune(svm, train_data, train_label, ranges = list(gamma = 10^(-5:5),
                                               cost = 2^(0:2)),
            tunecontrol = tune.control(sampling = "cross"))##找最优的gamma,和cost
best_gm <- obj$best.parameters$gamma
best_ct <- obj$best.parameters$cost
model <- svm(train_data, train_label, probability = TRUE, scale = TRUE,
             kernel = "radial", gamma = best_gm, cost = best_ct, cross = 0)
pre_label <- predict(model, train_data, probability = TRUE)
stat_res <- table(pre_label, train_label)
accurary <- (stat_res[1, 1] + stat_res[2, 2])/length(pre_label)
sensitivity <- stat_res[2, 2]/(stat_res[1, 2] + stat_res[2, 2])
specificity <- stat_res[1, 1]/(stat_res[1, 1] + stat_res[2, 1])
pre_prob <- attr(pre_label, "probabilities")
pred <- prediction(pre_prob[, 2], train_label)
perf <- performance(pred, "tpr", "fpr")
plot(perf,colorize=TRUE)
auc.tmp <- performance(pred, "auc")
auc.value <- auc.tmp@y.values
AUC <- round(auc.value[[1]], digits = 4)
auc.value



请先 登录 后评论