Research breakthrough possible @S-Logix pro@slogix.in

Office Address

Social List

How to Compute Precision and Recall from Scratch in Python for Multi-Class Classification?

Precision and Recall Calculation for 3-Class Classification

Condition f0r Calculating Precision and Recall from Scratch for a 3-Class Classification Problem

  • Description: Precision and recall are metrics used to evaluate the performance of a classification model, particularly for multi-class problems. Precision helps determine how many of the predicted positive instances are correct, while recall measures how many of the actual positive instances are correctly identified. For a 3-class classification problem, we calculate precision and recall for each class individually.
Why Should We Choose Precision and Recall?
  • Precision: Precision is crucial when false positives are costly. For example, in medical diagnoses, predicting a healthy person as diseased (false positive) can be dangerous.
  • Recall: Recall is important when false negatives are critical. For instance, in fraud detection, missing a fraudulent transaction (false negative) can be very costly.
  • Multi-Class Evaluation: Precision and recall offer valuable insights when evaluating the performance of a model across multiple classes, especially when class distribution is imbalanced.
Step-by-Step Process
  • Define the Confusion Matrix: For a 3-class classification, the confusion matrix is a 3x3 matrix that shows the true vs predicted classes for each instance. The values represent:
    • True Positive (TP): Correctly predicted instances for a class.
    • False Positive (FP): Instances predicted as a class, but the true class is different.
    • False Negative (FN): Instances of a class predicted as a different class.
    • True Negative (TN): Instances that are neither the predicted class nor the actual class.
  • Calculate Precision and Recall: Calculate precision and recall for each class using the confusion matrix. Precision for class i is TP/(TP+FP), and recall for class i is TP/(TP+FN).
  • Evaluate Performance: Precision and recall for each class give insights into the model's performance. You can also compute macro-average or weighted average precision and recall.
  • Plot Results: Visualize the performance using confusion matrices, precision-recall curves, or bar graphs for each class.
Sample Source Code
  • import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.metrics import confusion_matrix
    import seaborn as sns

    # Generate a synthetic 3-class classification dataset
    y_true = np.array([0, 1, 2, 0, 1, 1, 2, 0, 2, 1, 0, 2, 1, 2, 0])
    y_pred = np.array([0, 1, 1, 0, 0, 1, 2, 0, 2, 1, 0, 2, 1, 1, 0])

    # Calculate confusion matrix
    cm = confusion_matrix(y_true, y_pred)

    # Display confusion matrix
    plt.figure(figsize=(7, 5))
    sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=["Class 0", "Class 1", "Class 2"], yticklabels=["Class 0", "Class 1", "Class 2"])
    plt.title("Confusion Matrix")
    plt.xlabel("Predicted")
    plt.ylabel("True")
    plt.show()

    # Calculate precision and recall
    def calculate_precision_recall(cm):
    num_classes = cm.shape[0]
    precision = np.zeros(num_classes)
    recall = np.zeros(num_classes)

    for i in range(num_classes):
    TP = cm[i, i]
    FP = cm[:, i].sum() - TP
    FN = cm[i, :].sum() - TP

    precision[i] = TP / (TP + FP) if TP + FP > 0 else 0
    recall[i] = TP / (TP + FN) if TP + FN > 0 else 0

    return precision, recall

    precision, recall = calculate_precision_recall(cm)
    for i in range(len(precision)):
    print(f"Class {i}: Precision = {precision[i]:.2f}, Recall = {recall[i]:.2f}")

    # Plotting Precision and Recall
    classes = ["Class 0", "Class 1", "Class 2"]
    x = np.arange(len(classes))
    fig, ax = plt.subplots(figsize=(8, 5))
    ax.bar(x - 0.2, precision, 0.4, label="Precision", color='b')
    ax.bar(x + 0.2, recall, 0.4, label="Recall", color='r')
    ax.set_xticks(x)
    ax.set_xticklabels(classes)
    ax.set_ylabel('Score')
    ax.set_title('Precision and Recall for Each Class')
    ax.legend()
    plt.show()
Screenshots
  • Precision and Recall Calculation Screenshot