scikit-learn-metric-computation
Summary
Use scikit-learn's confusion_matrix and related metric functions to compute classification performance matrices from predicted and ground-truth label arrays. This enables rapid evaluation of model predictions against reference labels in mass spectrometry and other classification workflows.
When to use
You have prediction arrays (model outputs) and ground-truth label arrays from a classification task and need to compute confusion matrices, accuracy scores, or other performance metrics for visualization or quantitative evaluation. Typical triggers: post-training model evaluation, cross-validation score aggregation, or comparison of competing classifiers on the same spectrum or compound dataset.
When NOT to use
- Inputs are already in a pre-computed confusion matrix or aggregated metric form — skip directly to visualization or interpretation.
- You need probabilities or decision thresholds rather than hard class labels — use probability calibration or ROC/AUC metrics instead.
- The classification problem is multi-label (samples belong to multiple classes simultaneously) — use multilabel_confusion_matrix or hamming loss instead.
Inputs
- prediction array (numpy array or list of predicted class labels)
- ground-truth label array (numpy array or list of true class labels)
- optional class names (list of strings for axis labels)
- optional normalization mode (string: 'true', 'pred', or None)
Outputs
- confusion matrix (2D numpy array or pandas DataFrame)
- confusion matrix heatmap visualization (matplotlib figure)
- saved figure file (PNG or PDF)
How to apply
Import scikit-learn's confusion_matrix function and pass both the ground-truth labels and predicted labels to compute the confusion matrix. Optionally normalize the resulting matrix by row (per-class recall), column (per-class precision), or across all elements (stochastic normalization) depending on the evaluation context. The normalization mode is a key parameter: use 'true' for row-normalized (recalls per true class), 'pred' for column-normalized (precisions per predicted class), or None for raw counts. Once computed, render the matrix as a heatmap using matplotlib with annotated cell values, axis labels showing class names, a colorbar, and a title; save the figure in PNG or PDF format. Use this metric when you need both a visual confusion summary and quantitative misclassification patterns.
Related tools
- scikit-learn (Provides confusion_matrix function to compute classification performance matrix from predicted and ground-truth labels)
- matplotlib (Renders confusion matrix as heatmap with annotations, colorbar, and styling)
- ms2deepscore (Example application: evaluating Siamese neural network predictions against ground-truth molecular similarity or compound class labels) — https://github.com/matchms/ms2deepscore
Examples
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import numpy as np
y_true = np.array([0, 1, 2, 0, 1, 2])
y_pred = np.array([0, 1, 1, 0, 2, 2])
cm = confusion_matrix(y_true, y_pred, normalize='true')
plt.imshow(cm, cmap='Blues', interpolation='nearest')
plt.colorbar()
plt.savefig('confusion_matrix.png')
Evaluation signals
- Confusion matrix shape matches (n_classes, n_classes) where n_classes = number of unique labels in ground-truth array.
- Matrix row sums or column sums equal total sample count (before normalization) or sum to 1.0 (after normalization), indicating no dropped or duplicated samples.
- Diagonal elements (correct predictions) are visually prominent in the heatmap; off-diagonal elements reveal systematic confusions between specific class pairs.
- Normalization is correctly applied: row-normalized values represent per-class recall (each row sums to 1.0), column-normalized values represent precision (each column sums to 1.0).
- Saved figure is readable and includes clear axis labels, class names, colorbar with scale range, and title.
Limitations
- Confusion matrix assumes single-label classification; multi-label or hierarchical classification requires alternative metrics (multilabel_confusion_matrix, Hamming loss).
- Matrix values are computed from hard class labels only; probabilistic or soft predictions must be thresholded or converted to labels beforehand.
- Imbalanced datasets can produce misleading visual patterns in unnormalized matrices; normalization by row (recall) or column (precision) is strongly recommended for exploration.
- Large numbers of classes (>20) make heatmap difficult to read; consider aggregating minority classes or using alternative visualizations (hierarchical clustering, class-specific precision/recall plots).
Evidence
- [other] Compute the confusion matrix from predictions and labels using scikit-learn's confusion_matrix.: "Compute the confusion matrix from predictions and labels using scikit-learn's confusion_matrix."
- [other] Optionally normalize the matrix (by row, column, or all elements) based on parameter.: "Optionally normalize the matrix (by row, column, or all elements) based on parameter."
- [other] Render the matrix as a heatmap using matplotlib, with annotated cell values and axis labels.: "Render the matrix as a heatmap using matplotlib, with annotated cell values and axis labels."
- [other] Define the
create_confusion_matrix_plot function signature accepting prediction array, ground-truth label array, and optional parameters (class names, normalization mode, colormap).: "Define the create_confusion_matrix_plot function signature accepting prediction array, ground-truth label array, and optional parameters (class names, normalization mode, colormap)."
- [other] Apply styling (colorbar, title, legend) and save the figure to disk in PNG or PDF format.: "Apply styling (colorbar, title, legend) and save the figure to disk in PNG or PDF format."
1---2name: scikit-learn-metric-computation3description: Use when you have prediction arrays (model outputs) and ground-truth label arrays from a classification task and need to compute confusion matrices, accuracy scores, or other performance metrics for visualization or quantitative evaluation.4license: CC-BY-4.05---67# scikit-learn-metric-computation89## Summary1011Use scikit-learn's confusion_matrix and related metric functions to compute classification performance matrices from predicted and ground-truth label arrays. This enables rapid evaluation of model predictions against reference labels in mass spectrometry and other classification workflows.1213## When to use1415You have prediction arrays (model outputs) and ground-truth label arrays from a classification task and need to compute confusion matrices, accuracy scores, or other performance metrics for visualization or quantitative evaluation. Typical triggers: post-training model evaluation, cross-validation score aggregation, or comparison of competing classifiers on the same spectrum or compound dataset.1617## When NOT to use1819- Inputs are already in a pre-computed confusion matrix or aggregated metric form — skip directly to visualization or interpretation.20- You need probabilities or decision thresholds rather than hard class labels — use probability calibration or ROC/AUC metrics instead.21- The classification problem is multi-label (samples belong to multiple classes simultaneously) — use multilabel_confusion_matrix or hamming loss instead.2223## Inputs2425- prediction array (numpy array or list of predicted class labels)26- ground-truth label array (numpy array or list of true class labels)27- optional class names (list of strings for axis labels)28- optional normalization mode (string: 'true', 'pred', or None)2930## Outputs3132- confusion matrix (2D numpy array or pandas DataFrame)33- confusion matrix heatmap visualization (matplotlib figure)34- saved figure file (PNG or PDF)3536## How to apply3738Import scikit-learn's confusion_matrix function and pass both the ground-truth labels and predicted labels to compute the confusion matrix. Optionally normalize the resulting matrix by row (per-class recall), column (per-class precision), or across all elements (stochastic normalization) depending on the evaluation context. The normalization mode is a key parameter: use 'true' for row-normalized (recalls per true class), 'pred' for column-normalized (precisions per predicted class), or None for raw counts. Once computed, render the matrix as a heatmap using matplotlib with annotated cell values, axis labels showing class names, a colorbar, and a title; save the figure in PNG or PDF format. Use this metric when you need both a visual confusion summary and quantitative misclassification patterns.3940## Related tools4142- **scikit-learn** (Provides confusion_matrix function to compute classification performance matrix from predicted and ground-truth labels)43- **matplotlib** (Renders confusion matrix as heatmap with annotations, colorbar, and styling)44- **ms2deepscore** (Example application: evaluating Siamese neural network predictions against ground-truth molecular similarity or compound class labels) — https://github.com/matchms/ms2deepscore4546## Examples4748```49from sklearn.metrics import confusion_matrix50import matplotlib.pyplot as plt51import numpy as np5253y_true = np.array([0, 1, 2, 0, 1, 2])54y_pred = np.array([0, 1, 1, 0, 2, 2])55cm = confusion_matrix(y_true, y_pred, normalize='true')56plt.imshow(cm, cmap='Blues', interpolation='nearest')57plt.colorbar()58plt.savefig('confusion_matrix.png')59```6061## Evaluation signals6263- Confusion matrix shape matches (n_classes, n_classes) where n_classes = number of unique labels in ground-truth array.64- Matrix row sums or column sums equal total sample count (before normalization) or sum to 1.0 (after normalization), indicating no dropped or duplicated samples.65- Diagonal elements (correct predictions) are visually prominent in the heatmap; off-diagonal elements reveal systematic confusions between specific class pairs.66- Normalization is correctly applied: row-normalized values represent per-class recall (each row sums to 1.0), column-normalized values represent precision (each column sums to 1.0).67- Saved figure is readable and includes clear axis labels, class names, colorbar with scale range, and title.6869## Limitations7071- Confusion matrix assumes single-label classification; multi-label or hierarchical classification requires alternative metrics (multilabel_confusion_matrix, Hamming loss).72- Matrix values are computed from hard class labels only; probabilistic or soft predictions must be thresholded or converted to labels beforehand.73- Imbalanced datasets can produce misleading visual patterns in unnormalized matrices; normalization by row (recall) or column (precision) is strongly recommended for exploration.74- Large numbers of classes (>20) make heatmap difficult to read; consider aggregating minority classes or using alternative visualizations (hierarchical clustering, class-specific precision/recall plots).7576## Evidence7778- [other] Compute the confusion matrix from predictions and labels using scikit-learn's confusion_matrix.: "Compute the confusion matrix from predictions and labels using scikit-learn's confusion_matrix."79- [other] Optionally normalize the matrix (by row, column, or all elements) based on parameter.: "Optionally normalize the matrix (by row, column, or all elements) based on parameter."80- [other] Render the matrix as a heatmap using matplotlib, with annotated cell values and axis labels.: "Render the matrix as a heatmap using matplotlib, with annotated cell values and axis labels."81- [other] Define the `create_confusion_matrix_plot` function signature accepting prediction array, ground-truth label array, and optional parameters (class names, normalization mode, colormap).: "Define the `create_confusion_matrix_plot` function signature accepting prediction array, ground-truth label array, and optional parameters (class names, normalization mode, colormap)."82- [other] Apply styling (colorbar, title, legend) and save the figure to disk in PNG or PDF format.: "Apply styling (colorbar, title, legend) and save the figure to disk in PNG or PDF format."