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
...
I'm using the toy example for knn3
from the caret
package. It seems like the last call returns a list of predicted probabilities. While the columns where the predicted probability is 1 for s
suggests that the predicted species is s
, there are some other rows where the predicted probability of species c
is 0.2, and 0.8 for species v
. In that case, what is the predicted outcome? I'm guessing it's species v
since its predicted probability is higher?
Is there a function call that can quickly assess the accuracy of knn
model fit here?
First, save your predictions:
fit=knn3Train(train, test, cl, k = 5, prob = TRUE)
Then, you need a confusion matrix:
cm = as.matrix(table(Actual = cl, Predicted = fit))
Now you can calculate accuracy:
sum(diag(cm))/length(cl)
Or any number of other performance measurements: https://en.wikipedia.org/wiki/Precision_and_recall
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.