Search code examples
pythonkerasscikit-learncross-validationconfusion-matrix

How to display mean classification report and confusion matrix in case of KFold cross validation


I am trying to perform 5 fold cross validation in python using Keras ANN Classifier and I need a single mean classification report and confusion matrix for all the folds.

I could calculate the confusion matrix and the classification report for each fold and display them following the code snippet provided here.

Question:

How can I calculate the average confusion matrix and average report for all folds?


Solution

  • As far as confusion matrix is concerned, it's used to sum up the confusion matrices for each fold since they contain absolute values and not percentages. However, if you want to have an averaged confusion matrix you should create a confusion_mat_avg variable of the same shape and just take the mean of each cell for all folds.

    About the average report, the solution is similar. The following answer will probably give you a better idea.

    How to compute precision,recall and f1 score of an imbalanced dataset for K fold cross validation with 10 folds in python.