I am trying to visualize a 3-class confusion matrix for the
iris data. The things that I performed.
irisinto 50% Train, 25% Test, 25% validation.
- I used
knnto determine which is optimal from a set of k values (1,3,5) by Grid search.
- 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.
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) addmargins(prop.table(table(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) ```