ebert (13) [Avatar] Offline
#1
Hi Doug, Richard,

In section 3.6.6, pag. 68, you say about confusion matrixes:

"A confusion matrix is easy to understand once you understand what the rows and columns represent"

In this respect, at least in my case, watching the array without labels in rows and columns makes things "darker" so I've been looking for ways to print a sklearn confusion matrix with them. Unfortunately there is no simple method to do it.

What I've found is an easy example here for matrixes in general: http://www.tarekatwan.com/index.php/2017/12/how-to-plot-a-confusion-matrix-in-python/.

Based on that example and after adapting it a bit to make it more general, I added some code to the notebook, which produces the result that you can see here:

https://drive.google.com/file/d/17euAq7V6GdqU3TlFFVy5FdHbqS-Ab9eY/view?usp=sharing

The code is:

import matplotlib.pyplot as plt
import numpy as np

def print_cmatrix(matrix, values):
    matrix_range = len(values)
    plt.clf()
    plt.imshow(matrix, cmap=plt.cm.Wistia)
    plt.title('Churn Confusion Matrix')
    plt.ylabel('Actual')
    plt.xlabel('Predicted')
    tick_marks = np.arange(matrix_range)
    plt.xticks(tick_marks, values)
    plt.yticks(tick_marks, values)
    for i in range(matrix_range):
        for j in range(matrix_range):
            plt.text(j,i, cm[i][j], horizontalalignment="center")
    plt.show()

cm_values = test_data['churned'].unique()
cm = metrics.confusion_matrix(test_data['churned'],test_data['prediction'], labels = cm_values)
print_cmatrix(cm, cm_values)


Maybe this can help other people like me in understanding better these matrixes.

Regards
Doug Hudgeon (9) [Avatar] Offline
#2
Thanks Ebert,

We'll look to improve it in the next release. So far, we've stayed away from visualisation code so as not to load too much new stuff onto our readers but, if we are going to delve into confusion matrices, we may need to rethink that.