ebert (13) [Avatar] Offline
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:


The code is:

import matplotlib.pyplot as plt
import numpy as np

def print_cmatrix(matrix, values):
    matrix_range = len(values)
    plt.imshow(matrix, cmap=plt.cm.Wistia)
    plt.title('Churn Confusion Matrix')
    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")

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.

Doug Hudgeon (20) [Avatar] Offline
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.
Doug Hudgeon (20) [Avatar] Offline
Hi Ebert, we're still thinking about how to best display this. We've left it as is for the November MEAP release.