Scikit-learn confusion matrix

Scikit-learn confusion matrix

In scikit-learn, a confusion matrix is a table used to describe the performance of a classification model. It helps you understand the count of true positive, true negative, false positive, and false negative predictions made by the model. Scikit-learn provides a function confusion_matrix() that computes the confusion matrix based on the true labels and predicted labels.

Here's how you can use the confusion_matrix() function:

from sklearn.metrics import confusion_matrix # True labels (ground truth) true_labels = [1, 0, 1, 1, 0, 1, 0, 0, 1, 0] # Predicted labels from your model predicted_labels = [1, 0, 1, 0, 1, 0, 1, 0, 0, 1] # Compute the confusion matrix confusion = confusion_matrix(true_labels, predicted_labels) print("Confusion Matrix:") print(confusion) 

In this example, the confusion_matrix() function takes two arguments: the true labels and the predicted labels. It calculates the confusion matrix as a 2x2 array where each cell represents a count of samples falling into one of the four categories: true positive, false negative, false positive, and true negative.

The resulting output will be something like:

Confusion Matrix: [[3 2] [1 4]] 

Here's the interpretation of the output:

  • The top-left cell (3) represents true positives: the model correctly predicted 3 samples as positive.
  • The top-right cell (2) represents false negatives: the model incorrectly predicted 2 samples as negative when they were actually positive.
  • The bottom-left cell (1) represents false positives: the model incorrectly predicted 1 sample as positive when it was actually negative.
  • The bottom-right cell (4) represents true negatives: the model correctly predicted 4 samples as negative.

A confusion matrix helps you assess the performance of your classification model and evaluate its ability to correctly classify different classes.

Examples

  1. Query: "How to generate a confusion matrix with Scikit-learn?"

    • Description: This code snippet shows how to generate a basic confusion matrix using Scikit-learn.

    • Code:

      # Ensure Scikit-learn is installed pip install scikit-learn 
      from sklearn.metrics import confusion_matrix # Example true labels and predictions y_true = [1, 0, 1, 1, 0] y_pred = [1, 0, 1, 0, 0] # Generate the confusion matrix cm = confusion_matrix(y_true, y_pred) print("Confusion Matrix:\n", cm) 
  2. Query: "How to visualize a confusion matrix in Scikit-learn?"

    • Description: This snippet shows how to plot a confusion matrix using Matplotlib.

    • Code:

      # Install Matplotlib for plotting pip install matplotlib 
      import matplotlib.pyplot as plt import seaborn as sns from sklearn.metrics import confusion_matrix # Sample data for confusion matrix y_true = [1, 0, 1, 1, 0] y_pred = [1, 0, 1, 0, 0] # Create a confusion matrix cm = confusion_matrix(y_true, y_pred) # Visualize the confusion matrix using seaborn heatmap sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=['Negative', 'Positive'], yticklabels=['Negative', 'Positive']) plt.xlabel('Predicted') plt.ylabel('Actual') plt.title('Confusion Matrix') plt.show() 
  3. Query: "How to normalize a confusion matrix in Scikit-learn?"

    • Description: This snippet demonstrates how to normalize a confusion matrix to show percentages rather than raw counts.
    • Code:
      from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt import seaborn as sns # Sample data for confusion matrix y_true = [1, 0, 1, 1, 0] y_pred = [1, 0, 1, 0, 0] # Generate a normalized confusion matrix cm = confusion_matrix(y_true, y_pred, normalize='true') # Visualize the normalized confusion matrix sns.heatmap(cm, annot=True, fmt='.2f', cmap='Blues', xticklabels=['Negative', 'Positive'], yticklabels=['Negative', 'Positive']) plt.xlabel('Predicted') plt.ylabel('Actual') plt.title('Normalized Confusion Matrix') plt.show() 
  4. Query: "How to generate a confusion matrix for multi-class classification in Scikit-learn?"

    • Description: This code snippet illustrates how to create a confusion matrix for multi-class classification.
    • Code:
      from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt import seaborn as sns # Sample multi-class data y_true = [0, 1, 2, 2, 1, 0, 2] y_pred = [0, 1, 2, 1, 1, 0, 2] # Create a confusion matrix for multi-class cm = confusion_matrix(y_true, y_pred) # Plot the confusion matrix sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=['Class 0', 'Class 1', 'Class 2'], yticklabels=['Class 0', 'Class 1', 'Class 2']) plt.xlabel('Predicted') plt.ylabel('Actual') plt.title('Multi-Class Confusion Matrix') plt.show() 
  5. Query: "How to create a Scikit-learn confusion matrix with custom labels?"

    • Description: This snippet shows how to customize the axis labels in a confusion matrix.
    • Code:
      from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt import seaborn as sns # Sample data for confusion matrix y_true = [1, 0, 1, 1, 0] y_pred = [1, 0, 1, 0, 0] # Generate a confusion matrix cm = confusion_matrix(y_true, y_pred) # Customize the axis labels sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=['Class A', 'Class B'], yticklabels=['Class A', 'Class B']) plt.xlabel('Predicted Class') plt.ylabel('Actual Class') plt.title('Confusion Matrix with Custom Labels') plt.show() 
  6. Query: "How to extract confusion matrix metrics from Scikit-learn?"

    • Description: This snippet demonstrates how to extract specific metrics (like true positive, false positive, etc.) from a confusion matrix.
    • Code:
      from sklearn.metrics import confusion_matrix # Example data for confusion matrix y_true = [1, 0, 1, 1, 0] y_pred = [1, 0, 1, 0, 0] # Create a confusion matrix cm = confusion_matrix(y_true, y_pred) # Extract key metrics true_positive = cm[1, 1] # True Positives false_positive = cm[0, 1] # False Positives true_negative = cm[0, 0] # True Negatives false_negative = cm[1, 0] # False Negatives print("True Positives:", true_positive) print("False Positives:", false_positive) print("True Negatives:", true_negative) print("False Negatives:", false_negative) 
  7. Query: "How to plot confusion matrix with Scikit-learn using ConfusionMatrixDisplay?"

    • Description: This snippet demonstrates how to use Scikit-learn's ConfusionMatrixDisplay to plot a confusion matrix.
    • Code:
      from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay import matplotlib.pyplot as plt # Example data y_true = [1, 0, 1, 1, 0] y_pred = [1, 0, 1, 0, 0] # Create a confusion matrix cm = confusion_matrix(y_true, y_pred) # Use ConfusionMatrixDisplay to plot the confusion matrix disp = ConfusionMatrixDisplay(cm, display_labels=["Negative", "Positive"]) disp.plot(cmap='Blues', values_format='d') plt.title('Confusion Matrix') plt.show() 
  8. Query: "How to create a confusion matrix with cross-validation in Scikit-learn?"

    • Description: This snippet shows how to use cross-validation and generate a confusion matrix from cross-validated results.

    • Code:

      # Ensure Scikit-learn is installed pip install scikit-learn 
      from sklearn.model_selection import cross_val_predict from sklearn.metrics import confusion_matrix from sklearn.linear_model import LogisticRegression import matplotlib.pyplot as plt import seaborn as sns # Sample dataset from sklearn.datasets import load_iris iris = load_iris() # Logistic Regression model with cross-validation model = LogisticRegression() predictions = cross_val_predict(model, iris.data, iris.target, cv=5) # Generate a confusion matrix from cross-validated predictions cm = confusion_matrix(iris.target, predictions) # Plot the confusion matrix sns.heatmap(cm, annot=True, fmt='d', cmap='Blues') plt.xlabel('Predicted') plt.ylabel('Actual') plt.title('Confusion Matrix with Cross-Validation') plt.show() 
  9. Query: "How to interpret Scikit-learn's confusion matrix?"

    • Description: This snippet provides a guide on interpreting a confusion matrix, explaining terms like true positives, true negatives, false positives, and false negatives.
    • Code:
      from sklearn.metrics import confusion_matrix # Example data y_true = [1, 0, 1, 1, 0] y_pred = [1, 0, 1, 0, 0] # Create a confusion matrix cm = confusion_matrix(y_true, y_pred) # Interpretation of the confusion matrix true_positives = cm[1, 1] # Cases where actual and predicted are positive false_positives = cm[0, 1] # Cases where actual is negative, but predicted positive true_negatives = cm[0, 0] # Cases where actual and predicted are negative false_negatives = cm[1, 0] # Cases where actual is positive, but predicted negative # Display confusion matrix values print("Confusion Matrix:\n", cm) print("True Positives:", true_positives) print("False Positives:", false_positives) print("True Negatives:", true_negatives) print("False Negatives:", false_negatives) 
  10. Query: "How to create a confusion matrix with multiple classifiers in Scikit-learn?"


More Tags

ssh-keygen identifier angular-universal mailx iqueryable .net-3.5 seeding lint-staged python-unittest subnet

More Python Questions

More Trees & Forestry Calculators

More Fitness Calculators

More Electrochemistry Calculators

More General chemistry Calculators