如何绘制knn模型的ROC曲线 [英] How to plot a ROC curve for a knn model

查看:1280
本文介绍了如何绘制knn模型的ROC曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ROCR软件包,我想知道如何在R中为knn模型绘制ROC曲线?有什么办法可以用这个程序包将其全部绘制出来吗?

I am using ROCR package and i was wondering how can one plot a ROC curve for knn model in R? Is there any way to plot it all with this package?

我不知道如何对knn使用ROCR的预测功能.这是我的示例,我使用UCI存储库中的isolet数据集,在其中将class属性重命名为y:

I don't know how to use the prediction function of ROCR for knn. Here's my example, i am using isolet dataset from UCI repository where i renamed the class attribute as y:

cl<-factor(isolet_training$y)
knn_isolet<-knn(isolet_training, isolet_testing, cl, k=2, prob=TRUE)

现在我的问题是,传递给ROC的预测函数的参数是什么.我尝试了以下两种无效的替代方法:

Now my question is, what are the arguments to pass to the prediction function of ROC. I tried the 2 below alternatives which are not working:

library(ROCR)
pred_knn<-prediction(knn_isolet$y, cl)
pred_knn<-prediction(knn_isolet$y, isolet_testing$y)

推荐答案

要在此处获得ROC曲线,需要解决几个步骤.由于您没有提供获取正在使用的数据的简便方法,因此我将组成一些数据.请注意,ROCR程序包希望类标签为正/负,而不是因数,因此让它们像这样.

There's several steps to solve in order to get you a ROC curve here. I am just going to make up some data since you did not provide an easy way of getting the data you are using. Note that the ROCR package wants the class labels to be positive/negative, not factors, so let's make them like that.

# Generate fake data
isolet_training <- sweep(matrix(rnorm(400), 40, 10), 1, rep(0:1, each=20))
isolet_testing <- sweep(matrix(rnorm(400), 40, 10), 1, rep(0:1, each=20))
# Generate class labels
cl <- cl_testing <- rep(c(-1, 1), each=20)

您现在可以训练您的knn并从"prob"属性获取其类别概率.

You can now train your knn and obtain its class probabilities from the "prob" attribute.

knn_isolet <- class::knn(isolet_training, isolet_testing, cl, k=2, prob=TRUE)
prob <- attr(knn_isolet, "prob")
# you can probably use just `knn` instead of `class::knn`,
# but for some reason it did not work for me.

但是,它们以ROCR不接受的形式出现,因此我们需要将它们转换为-1类并重新缩放.

However, they come on a form that ROCR does not accept so we need to invert them for the -1 class and rescale them.

prob <- 2*ifelse(knn_isolet == "-1", 1-prob, prob) - 1

现在,您可以将概率"输入到ROCR包的函数中,并获得ROC曲线.

Now you can feed the "probabilities" into the ROCR package's functions and obtain a ROC curve.

pred_knn <- prediction(prob, cl_testing)
pred_knn <- performance(pred_knn, "tpr", "fpr")
plot(pred_knn, avg= "threshold", colorize=T, lwd=3, main="Voilà, a ROC curve!")

这篇关于如何绘制knn模型的ROC曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆