0

I am running a Bidirectional LSTM for multiclass text classification in R using Keras. I have run my model and I need to create a confusion matrix. I tried using predict_classes() but my RStudio threw an error that predict_classes() was deprecated. I tried to use this bit of code that I found on the RStudio Keras website:

prediction1 <- model %>% 
  predict(x.test)  %>% 
  k_argmax(axis = -1)

NOTE: x.test is my matrix that contains the text features.

I am not sure how to use it + I have not found any examples of how to use it online so I am quite confused. I would appreciate any help that anyone could provide!

Thanks

fnas
  • 55
  • 5

1 Answers1

0

You can use 'caret' library to achieve that.

#Install required packages
install.packages('caret')
 
#Import required library
library(caret)
 
#Creates vectors having data points
expected_value <- factor(c(1,0,1,0,1,1,1,0,0,1))
predicted_value <- factor(c(1,0,0,1,1,1,0,0,0,1))
 
#Creating confusion matrix
example <- confusionMatrix(data=predicted_value, reference = expected_value)
 
#Display results 
example

Or the table function:

pred <- model %>% predict(x_test, batch_size = batch_size)
y_pred = round(pred)
# Confusion matrix
confusion_matrix = table(y_pred, y_test)

For the 'caret' example:

https://www.journaldev.com/46732/confusion-matrix-in-r

Timbus Calin
  • 13,809
  • 5
  • 41
  • 59
  • Thanks for this advice. I tried the method mentioned but it gave me a binary confusion matrix of only 0s and 1s. I have 7 different levels so I am unsure of how to achieve that – fnas Aug 31 '21 at 13:21
  • In this one here: https://stackoverflow.com/questions/25497398/how-to-construct-the-confusion-matrix-for-a-multi-class-variable, I can see that the OP convers also the multiclass classification. – Timbus Calin Aug 31 '21 at 13:49
  • The difference is that there are no "FP" or "FN" declared in the matrices per say, but you can deduce them like (above the main diagonal is FP, below the main diagonal is FN)/ – Timbus Calin Aug 31 '21 at 13:49
  • The second one with table should be easier for your approach. – Timbus Calin Aug 31 '21 at 13:55
  • Thanks, @Timbus Calin. I am using a matrix and not a dataframe. I believe the issue is due to the use of a matrix! – fnas Aug 31 '21 at 20:30
  • Great, let me know if it works; df_matrix <- as.data.frame(my_matrix) – Timbus Calin Sep 01 '21 at 06:40
  • 1
    I am about to try that right now! I will update you!! – fnas Sep 01 '21 at 08:57
  • @TimbusCalin hi! I am so sorry for getting back to you so late. Unfortunately, it did not work. I am trying to use: pred <- model %>% predict(x.test) %>% k_argmax(axis = 1) Unfortunately, I am still not sure how to implement it though – fnas Sep 17 '21 at 09:26