The function will create a data frame of confusion matrix with embedded Recall and precision scores. The data frame can then be easily exported to excel. Works with any number of categories
def confusion_max_df(actual, prediction, labels):
"""
Input: A list of actual values, prediction values and labels
returns: A data frame of confusion matrix embedded with precision and recall
"""
# confusion matrix from sklearn.metrix library
cnf_matrix = confusion_matrix(actual, prediction,labels=labels)
# calculatimg recall and precision at category label
tp_and_fn = cnf_matrix.sum(1)
tp_and_fp = cnf_matrix.sum(0)
tp = cnf_matrix.diagonal()
precision = [str(round(num, 2)*100) +'%' for num in list(tp / tp_and_fp)]
recall = [str(round(num, 2)*100)+'%' for num in list(tp / tp_and_fn)]
# creating dataframe for exporting to excel
cnf_matrix_df = pd.DataFrame(cnf_matrix, columns=labels)
cnf_matrix_df = cnf_matrix_df.add_prefix('Predicted - ')
actual_list = ['Actual - ' + str(x) for x in labels]
cnf_matrix_df['Confusion matrix'] = actual_list
cnf_matrix_df = cnf_matrix_df.set_index('Confusion matrix')
cnf_matrix_df['Recall'] = recall
# adding a row in the dataframe for precision scores
precision_row = ['Precision']
precision_row.extend(precision)
precision_row.append('')
cnf_matrix_df.loc['Precision'] = precision_row[1:]
return cnf_matrix_df
confusion_max_df(['Cat A','Cat A','Cat B','Cat B','Cat A','Cat B'],['Cat A','Cat A','Cat B','Cat B','Cat A','Cat A'],['Cat A','Cat B'])