Reputation: 512
Say i have the matrix
> a <- matrix(c(1,2,3,4,5,6,7,8,9),nrow=3)
> rownames(a)=c('A','B','C')
> colnames(a)=c('A','B','C')
> a
A B C
A 1 4 7
B 2 5 8
C 3 6 9
Considering that columns would represent the actual class and rows the predicted class, how can I extract the predictions and actual values vectors to use them in confusionMatrix()?.
Upvotes: 1
Views: 584
Reputation: 46968
I guess you are referring to confusionMatrix()
from caret
. This is already a confusion matrix, and you can just pass the predictions into the function using as.table(), see example, where we set up a model and train / test data:
library(caret)
set.seed(111)
idx = sample(1:nrow(iris),100)
trainData = iris[idx,]
testData = iris[-idx,]
mdl = train(Species ~ .,data=trainData,
method="rf",trControl=trainControl(method="cv"))
pred = predict(mdl,testData)
actual = testData$Species
Confusion matrix with labels:
confusionMatrix(pred,actual)
Confusion Matrix and Statistics
Reference
Prediction setosa versicolor virginica
setosa 20 0 0
versicolor 0 11 2
virginica 0 0 17
Confusion matrix with table or a matrix :
a = matrix(table(pred,actual),nrow=3)
colnames(a) = levels(testData$Species)
rownames(a) = levels(testData$Species)
setosa versicolor virginica
setosa 20 0 0
versicolor 0 11 2
virginica 0 0 17
confusionMatrix(as.table(a))
Confusion Matrix and Statistics
setosa versicolor virginica
setosa 20 0 0
versicolor 0 11 2
virginica 0 0 17
Overall Statistics
Accuracy : 0.96
95% CI : (0.8629, 0.9951)
No Information Rate : 0.4
P-Value [Acc > NIR] : < 2.2e-16
If you really need them in a vector, (this sounds super bizarre for me) using a:
actual_vector = rep(colnames(a),colSums(a))
pred_vector = rep(rownames(a),rowSums(a))
table(actual_vector) == table(actual)
actual_vector
setosa versicolor virginica
TRUE TRUE TRUE
table(pred_vector) == table(pred)
pred_vector
setosa versicolor virginica
TRUE TRUE TRUE
Upvotes: 2