python - How compute confusion matrix for multiclass classification in Scikit?

Python - How compute confusion matrix for multiclass classification in Scikit?

To compute the confusion matrix for multiclass classification in Scikit-Learn, you can use the confusion_matrix function from the sklearn.metrics module. Here's how you can do it:

from sklearn.metrics import confusion_matrix import numpy as np # Example true labels and predicted labels y_true = np.array([2, 0, 2, 2, 0, 1]) y_pred = np.array([0, 0, 2, 2, 0, 2]) # Compute confusion matrix cm = confusion_matrix(y_true, y_pred) # Print confusion matrix print("Confusion Matrix:") print(cm) 

This code will output the confusion matrix:

Confusion Matrix: [[2 0 0] [0 0 1] [1 0 2]] 

In the confusion matrix:

  • Rows represent the actual classes.
  • Columns represent the predicted classes.
  • Each cell (i, j) in the matrix represents the number of instances of class i that were predicted as class j.

You can interpret the confusion matrix to analyze the performance of your multiclass classification model.

Examples

  1. Python: Compute confusion matrix for multiclass classification using Scikit-learn

    Description: This code computes the confusion matrix for multiclass classification using Scikit-learn's confusion_matrix function.

    from sklearn.metrics import confusion_matrix # y_true: true labels, y_pred: predicted labels confusion_mat = confusion_matrix(y_true, y_pred) 
  2. Python: Generate confusion matrix for multiclass classification problem in Scikit-learn

    Description: This code generates the confusion matrix for a multiclass classification problem using Scikit-learn.

    from sklearn.metrics import confusion_matrix # y_true: true labels, y_pred: predicted labels confusion_mat = confusion_matrix(y_true, y_pred) 
  3. Python: Calculate confusion matrix for multiclass classification task using Scikit-learn

    Description: This code calculates the confusion matrix for a multiclass classification task using Scikit-learn's confusion_matrix function.

    from sklearn.metrics import confusion_matrix # y_true: true labels, y_pred: predicted labels confusion_mat = confusion_matrix(y_true, y_pred) 
  4. Python: Compute and visualize confusion matrix for multiclass classification in Scikit-learn

    Description: This code computes and visualizes the confusion matrix for multiclass classification using Scikit-learn.

    import matplotlib.pyplot as plt from sklearn.metrics import confusion_matrix import seaborn as sns # y_true: true labels, y_pred: predicted labels confusion_mat = confusion_matrix(y_true, y_pred) sns.heatmap(confusion_mat, annot=True, cmap='Blues') plt.xlabel('Predicted labels') plt.ylabel('True labels') plt.show() 
  5. Python: Generate confusion matrix with class names for multiclass classification in Scikit-learn

    Description: This code generates a confusion matrix with class names for multiclass classification using Scikit-learn.

    from sklearn.metrics import confusion_matrix import pandas as pd # y_true: true labels, y_pred: predicted labels confusion_mat = confusion_matrix(y_true, y_pred) confusion_df = pd.DataFrame(confusion_mat, columns=class_names, index=class_names) 
  6. Python: Calculate confusion matrix and classification report for multiclass classification using Scikit-learn

    Description: This code calculates the confusion matrix and classification report for multiclass classification using Scikit-learn.

    from sklearn.metrics import confusion_matrix, classification_report # y_true: true labels, y_pred: predicted labels confusion_mat = confusion_matrix(y_true, y_pred) class_report = classification_report(y_true, y_pred) 
  7. Python: Plot confusion matrix for multiclass classification in Scikit-learn with normalized values

    Description: This code plots the confusion matrix for multiclass classification in Scikit-learn with normalized values.

    import matplotlib.pyplot as plt from sklearn.metrics import confusion_matrix import seaborn as sns # y_true: true labels, y_pred: predicted labels confusion_mat = confusion_matrix(y_true, y_pred, normalize='true') sns.heatmap(confusion_mat, annot=True, cmap='Blues') plt.xlabel('Predicted labels') plt.ylabel('True labels') plt.show() 
  8. Python: Compute confusion matrix for multiclass classification with labels in Scikit-learn

    Description: This code computes the confusion matrix for multiclass classification with specified labels using Scikit-learn.

    from sklearn.metrics import confusion_matrix # y_true: true labels, y_pred: predicted labels, labels: class labels confusion_mat = confusion_matrix(y_true, y_pred, labels=labels) 
  9. Python: Generate confusion matrix and accuracy score for multiclass classification in Scikit-learn

    Description: This code generates the confusion matrix and accuracy score for multiclass classification in Scikit-learn.

    from sklearn.metrics import confusion_matrix, accuracy_score # y_true: true labels, y_pred: predicted labels confusion_mat = confusion_matrix(y_true, y_pred) accuracy = accuracy_score(y_true, y_pred) 
  10. Python: Plot confusion matrix with percentages for multiclass classification in Scikit-learn

    Description: This code plots the confusion matrix with percentages for multiclass classification in Scikit-learn.

    import matplotlib.pyplot as plt from sklearn.metrics import confusion_matrix import seaborn as sns # y_true: true labels, y_pred: predicted labels confusion_mat = confusion_matrix(y_true, y_pred) confusion_mat = confusion_mat.astype('float') / confusion_mat.sum(axis=1)[:, np.newaxis] sns.heatmap(confusion_mat, annot=True, cmap='Blues', fmt='.2%') plt.xlabel('Predicted labels') plt.ylabel('True labels') plt.show() 

More Tags

immutable.js xcode7 salesforce air parseexception dividebyzeroexception restify gravity linq-to-xml directory-listing

More Programming Questions

More Genetics Calculators

More Everyday Utility Calculators

More Chemical thermodynamics Calculators

More Fitness-Health Calculators