# A 3-class confusion matrix [closed]

I am trying to visualize a 3-class confusion matrix for the iris data. The things that I performed.

1. Partitioned iris into 50% Train, 25% Test, 25% validation.
2. I used knn to determine which is optimal from a set of k values (1,3,5) by Grid search.
3. Based on the winner model (obtained with k=1) I want to predict the performance on its test set with a 25% split.

I'm having to generate/ visualizing the confusion matrix for this 3-class problem. When I compile it throws me error as below:

Error in eval(predvars, data, env) : object 'Petal.Width' not found


Cannot figure out where it went wrong.

Code:

library(caret)
library(class)

spec = c(train = .5, test = .25, validate = .25)

byparts = sample(cut(
seq(nrow(iris)),
nrow(iris)*cumsum(c(0,spec)),
labels = names(spec)
))

res = split(iris, byparts)

classifier = train(form = Species ~ ., data = res$train, method = 'knn', tuneGrid = expand.grid(k = c(1,3,5))) classifier #Confusion Matrix y_pred = predict(classifier, newdata = res$$test[-4]) cm = table(res$$test[,4], y_pred) $$$$  • Hi, the Species variable, tyhat is the want that you want to remove for making predictions, has index 5, not 4. So you should write y_pred = predict(classifier, newdata = res$test[-5]) cm = table(res\$test[,5], y_pred) And it will work Apr 21 at 10:53
• Oh, a silly mistake thanks for the help. Also, would like to know is there a way to visually plot this confusion matrix? Apr 21 at 10:59

I an comment the OP asked how to visually plot a 3x3 confusion matrix. There are an aweful lot of alternatives. Some examples (in no way exhaustive) in R to give a starter for your own ideas:

#some example data
confusion <- data.frame(true = iris$$Species, predicted = iris$$Species)
confusion$$predicted[sample.int(150,40)] <- iris$$Species[sample.int(150,40)]
str(confusion)

plot(confusion$$true, confusion$$predicted) plot(table(confusion$$true, confusion$$predicted)) library(ggplot2)
ggplot(confusion) +
geom_count(aes(x = true, y = predicted)) +
scale_size_continuous(range=c(0,30)) ggplot(confusion) +
geom_jitter(aes(x = true, y = predicted), width = .25, height = .25) library(ggplot2)
ggplot(confusion) +
geom_count(aes(x = true, y = predicted), color ="lightgrey") +
scale_size_continuous(range=c(0,30)) +
geom_jitter(aes(x = true, y = predicted), width = .25, height = .25) +
theme_bw() library(ggplot2)
ggplot(aggregate(confusion, list(confusion$$true, confusion$$predicted), FUN = length))+
scale_color_continuous() +
geom_text(aes(x = Group.1, y = Group.2, label = true, color = true), size = 25) +
theme_bw()
` • This was what I pretty much looking for! Appreciate your time and explanations. Apr 21 at 12:13