Get startedGet started for free

Evaluating four categories

The confusion matrix is the most straightforward tool to look at the four categories of outcomes: true positives (TP), false positives (FP), true negatives (TN), and false negatives (FN). In this exercise, you will use a standard decision tree classifier DecisionTreeClassifier() from sklearn on the sample click data and calculate the breakdowns of outcomes by the four categories.

The pandas module is available as pd in your workspace and the sample DataFrame is loaded as df. The features are loaded in X and the target is loaded in y for use. Additionally, DecisionTreeClassifier from sklearn.tree is available.

This exercise is part of the course

Predicting CTR with Machine Learning in Python

View Course

Exercise instructions

  • Obtain the training and testing splits for X and y.
  • Define a decision tree classifier and produce predictions y_pred by fitting the model.
  • Use the confusion matrix to get the counts for categories of each outcome, with a 1 being a positive (click) and 0 being a negative (non-click).
  • For example: true negatives would be [0,0] and true positives would be [1,1].

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Set up classifier using training data to predict test data
X_train, X_test, y_train, y_test = ____(
  X, y, test_size = .2, random_state = 0)
clf = ____
y_pred = clf.____(X_train, y_train).____(X_test) 

# Define confusion matrix and four categories
conf_matrix = ____(y_test, y_pred)
tn = conf_matrix[____][____]
fp = conf_matrix[____][____]
fn = conf_matrix[____][____]
tp = conf_matrix[____][____]

print("TN: %s, FP: %s, FN: %s, TP: %s" %(tn, fp, fn, tp))
Edit and Run Code