...

/

Exercise: Calculating True and False Rates and Confusion Matrix

Exercise: Calculating True and False Rates and Confusion Matrix

Learn to calculate the true and false positive and negative rates and the confusion matrix.

Confusion matrix calculation in Python

In this exercise, we’ll use the test data and model predictions from the logistic regression model we created previously, using only the EDUCATION feature. We will illustrate how to manually calculate the true and false positive and negative rates, as well as the numbers of true and false positives and negatives needed for the confusion matrix. Then we will show a quick way to calculate a confusion matrix with scikit-learn. Perform the following steps to complete the exercise, noting that some code from the previous lesson must be run before doing this exercise:

  1. Run this code to calculate the number of positive samples:

    P = sum(y_test) 
    P 
    

    The output should appear like this:

    # 1155 
    

    Now we need the number of true positives. These are samples where the true label is 1 and the prediction is also 1. We can identify these with a logical mask for the samples that are positive (y_test==1) AND & is the logical AND operator in Python) have a positive prediction (y_pred==1).

  2. Use this code to calculate the number of true ...