Classification Metrics for Beginners

With a function to make it easier to produce and analyze them

THE CONFUSION MATRIX

Actual Observations for Example Above. This is not a confusion matrix. We will produce the confusion matrix for these numbers below.
Predicted Observations for Example Above. This is not a confusion matrix. We will produce the confusion matrix for these numbers below.
imports for both the confusion matrix and the function that plots the confusion matrix. Note: you do not need to run confusion_matrix function in order to run plot_confusion_matrix function. You can just go right to plotting the confusion matrix. Sklearn confusion matrix documentation.
Sklearn function that runs just the confusion matrix and returns an array.
Here we are doing a few things. We are unpacking the true positives, true negatives, false positives and false negatives and we have also raveled the matrix so that we can unpack these values from the tuples that they are contained in. If we do not ravel you will get a shape of 2 X 2 instead of 4 x 1 and you will not be able to unpack the values from the matrix.
Creating a dataframe of confusion matrix values ends up looking a lot like an actual confusion matrix.
Confusion Matrix Plot from Sklearn.

THE MOST POPULAR CLASSIFICATION METRICS

Code Example from Sklearn’s Documentation
Code Example From Sklearn’s Documentation
Code Example From Sklearn’s Documentation
Code Example From Sklearn’s Documentation
Fitting and Scoring the Classifiers on both Train and Test Sets.
Output of the above function
Creating predictions on test data and forming into a list
Function that creates a dataframe of metrics for a variety of models that you choose. I calculated many of these scores manually using python but you can use the Sklearn functions for some of these as well.
Dataframe detailing classification metrics for a variety of models of my choosing. This was ouputted from the custom function created above.

Interested in thoughts and data. Data Science Immersive Student at General Assembly