Start learning free

LESSON 36 OF 165 · MACHINE LEARNING: YOUR FIRST MODEL

Four Kinds of Right and Wrong

Practice

Accuracy gives you one number: how often the model is right. But a yes/no model can be wrong in two different ways. It can say a customer will buy again when they won't, or say they won't when they will. A confusion matrix counts all four kinds of right and wrong, each on its own.

confusion_matrix(real, said) from sklearn.metrics takes the real answers first and the model's answers second. It gives back a 2×2 numpy array of counts. The rows are what really happened (row 0 = no, row 1 = yes). The columns are what the model said (column 0 = no, column 1 = yes). Here is an example with five customers:

from sklearn.metrics import confusion_matrix

real = [0, 0, 0, 1, 1]
said = [0, 1, 1, 1, 0]
m = confusion_matrix(real, said)
print(m)
# [[1 2]
#  [1 1]]
print(m[0, 1])   # 2: said yes, was no

To read one cell of a 2D array, put the row and the column in one pair of brackets: m[0, 1] is row 0, column 1. The right answers sit on the diagonal, in m[0, 0] and m[1, 1]. The other two cells hold the two kinds of mistake.

For the shop's test customers, your finished code prints the lines below. The last two lines come from the lesson's check, which runs after your code.

[[19  7]
 [14 35]]
Said no, was no: 19
Said yes, was no: 7
Said no, was yes: 14
Said yes, was yes: 35
All four add up to: 75
Right answers (the diagonal): 54

Your task: the data, the split, the depth-5 tree and its predictions are already in the editor. Make the confusion matrix of y_test and predictions, keep it in matrix and print it. Then print each of its four numbers after its name in plain words.


Assignment
Follow the Coding Tutorial to make the confusion matrix of the tree's predictions and print its four numbers with their names.


Hint
The real answers go first and the model's answers second. The row is what really happened and the column is what the model said, so "said yes, was no" is row 0, column 1.


STEP 1 OF 6
    Ctrl + Enter to check