R:你如何计算 KNN 的预测精度?
Posted
技术标签:
【中文标题】R:你如何计算 KNN 的预测精度?【英文标题】:R: how do you calculate prediction accuracy for KNN? 【发布时间】:2018-05-14 21:07:57 【问题描述】:library(caret)
irisFit1 <- knn3(Species ~ ., iris)
irisFit2 <- knn3(as.matrix(iris[, -5]), iris[,5])
data(iris3)
train <- rbind(iris3[1:25,,1], iris3[1:25,,2], iris3[1:25,,3])
test <- rbind(iris3[26:50,,1], iris3[26:50,,2], iris3[26:50,,3])
cl <- factor(c(rep("s",25), rep("c",25), rep("v",25)))
> knn3Train(train, test, cl, k = 5, prob = TRUE)
[1] "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "s" "c"
[27] "c" "v" "c" "c" "c" "c" "c" "v" "c" "c" "c" "c" "c" "c" "c" "c" "c" "c" "c" "c" "c" "c" "c" "c" "v" "c"
[53] "c" "v" "v" "v" "v" "v" "c" "v" "v" "v" "v" "c" "v" "v" "v" "v" "v" "v" "v" "v" "v" "v" "v"
attr(,"prob")
c s v
[1,] 0.0000000 1 0.0000000
[2,] 0.0000000 1 0.0000000
[3,] 0.0000000 1 0.0000000
[4,] 0.0000000 1 0.0000000
[5,] 0.0000000 1 0.0000000
[6,] 0.0000000 1 0.0000000
...
我正在使用来自caret
包的knn3
的玩具示例。似乎最后一次调用返回了一个预测概率列表。虽然 s
的预测概率为 1 的列表明预测的物种是 s
,但在其他一些行中,物种 c
的预测概率为 0.2,物种 v
的预测概率为 0.8。在这种情况下,预测的结果是什么?我猜它是物种v
,因为它的预测概率更高?
这里有没有可以快速评估knn
模型拟合精度的函数调用?
【问题讨论】:
【参考方案1】:首先,保存您的预测:
fit=knn3Train(train, test, cl, k = 5, prob = TRUE)
那么,你需要一个混淆矩阵:
cm = as.matrix(table(Actual = cl, Predicted = fit))
现在您可以计算准确度了:
sum(diag(cm))/length(cl)
或任何数量的其他性能测量:https://en.wikipedia.org/wiki/Precision_and_recall
【讨论】:
以上是关于R:你如何计算 KNN 的预测精度?的主要内容,如果未能解决你的问题,请参考以下文章
R语言实战应用精讲50篇(二十)-R语言时间序列预测模型:ARIMA vs KNN