<jupyter_start><jupyter_text>sklearn.metrics.confusion_matrixhttps://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html```python sklearn.metrics.confusion_matrix(y_true, y_pred, *, labels=None, sample_weight=None, normalize=None)```<jupyter_code>%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
y_true = [2, 0, 2, 2, 0, 1]
y_pred = [0, 0, 2, 2, 0, 2]
confusion_matrix(y_true, y_pred)
# labels=["ant", "bird", "cat"]
# labels=[  0  ,   1   ,   2  ]

y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
confusion_matrix(y_true, y_pred, labels=["ant", "bird", "cat"])<jupyter_output><empty_output><jupyter_text>Only in the binary case, we can use "ravel()"<jupyter_code># tp=(true positive)
# fp=(false positive)
# tn=(true negative)
# fn=(false negative) 

y_true = [0, 1, 0, 1]
y_pred = [1, 1, 1, 0]

tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
print((tn, fp, fn, tp))
# This is not correct 
y_true = [2, 0, 2, 2, 0, 1]
y_pred = [0, 0, 2, 2, 0, 2]

confusion = confusion_matrix(y_true, y_pred).ravel()
print(confusion)
def binary_perf_measure(y_true, y_pred):
    TP, FP, TN, FN = 0, 0, 0, 0

    for i in range(len(y_true)):
        if y_true[i] == 1 and y_pred[i] == 1:
            TP += 1
        if y_true[i] == 0 and y_pred[i] == 1:
            FP += 1
        if y_true[i] == 0 and y_pred[i] == 0:
            TN += 1
        if y_true[i] == 1 and y_pred[i] == 0:
            FN += 1

    return TN, FP, FN, TP

y_true = [0, 1, 0, 1]
y_pred = [1, 1, 1, 0]

TN, FP, FN, TP = binary_perf_measure(y_true, y_pred)
print((TN, FP, FN, TP))<jupyter_output>(0, 2, 1, 1)<jupyter_text>Self-Definition Plot<jupyter_code>def plot_confusion_matrix(cm, classes, savename, figsize=(12, 8), fontsize=15, normalized=False):
    
    if normalized:
        title='Normalized Confusion Matrix'
        # Normalize by row
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
    else:
        title='Confusion Matrix'

    plt.figure(figsize=figsize, dpi=100)
    np.set_printoptions(precision=2)

    # each cell probability in confusion matrix
    ind_array = np.arange(len(classes))
    x, y = np.meshgrid(ind_array, ind_array)
    for x_val, y_val in zip(x.flatten(), y.flatten()):
        c = cm[y_val][x_val]
        if c > 0.001:
            plt.text(x_val, y_val, "%0.2f" % (c,), color='red', fontsize=fontsize, va='center', ha='center')
    
    plt.imshow(cm, interpolation='nearest', cmap=plt.cm.binary)
    plt.title(title)
    plt.colorbar()
    xlocations = np.array(range(len(classes)))
    plt.xticks(xlocations, classes, rotation=90)
    plt.yticks(xlocations, classes)
    plt.ylabel('Actual label')
    plt.xlabel('Predicted label')
    
    # offset the tick
    tick_marks = np.array(range(len(classes))) + 0.5
    plt.gca().set_xticks(tick_marks, minor=True)
    plt.gca().set_yticks(tick_marks, minor=True)
    plt.gca().xaxis.set_ticks_position('none')
    plt.gca().yaxis.set_ticks_position('none')
    plt.grid(True, which='minor', linestyle='-')
    plt.gcf().subplots_adjust(bottom=0.15)
    
    # show confusion matrix
    plt.savefig(savename, format='png')
    plt.show()
# Demo 1
classes = ['A', 'B', 'C', 'D', 'E', 'F']

# 6 class with 50 random sample
random_numbers = np.random.randint(6, size=50)  

# sample real label
y_true = random_numbers.copy() 

# generate fake error sample
random_numbers[:10] = np.random.randint(6, size=10)  
# fake predicted label
y_pred = random_numbers  

# Generate confusion matrix and Plot the chart
cm = confusion_matrix(y_true, y_pred)
plot_confusion_matrix(cm, classes, 'confusion_matrix.png', figsize=(12, 4), fontsize=8, normalized=False)
plot_confusion_matrix(cm, classes, 'confusion_matrix.png', figsize=(12, 4), fontsize=8, normalized=True)
# Demo 2 
classes = [0, 1, 2]

y_true = [2, 0, 2, 2, 0, 1]
y_pred = [0, 0, 2, 2, 0, 2]

# Generate confusion matrix and Plot the chart
cm = confusion_matrix(y_true, y_pred)
plot_confusion_matrix(cm, classes, 'confusion_matrix.png', figsize=(12, 4), fontsize=8, normalized=False)
plot_confusion_matrix(cm, classes, 'confusion_matrix.png', figsize=(12, 4), fontsize=8, normalized=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16196690748077372, 'fraction_numerical': 0.030295968305756232, 'mean_word_length': 4.067296340023613, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '260566', 'license': 'MIT', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1642, 'n_words': 392, 'path': 'ML/confusion_matrix/Confusion_Matrix.ipynb', 'repo_name': 'JimCurryWang/Deep-Learning-Jot', 'size': '4291'}