Classification Measures#

class ConfusionMatrix(method_name: str, description: str)[source]#

Bases: MeasureEvaluator

Calculate a confusion matrix for a classification model.

This evaluator generates confusion matrices for classification models, providing detailed information about the model’s performance across different classes. The confusion matrix shows the count of true positives, false positives, true negatives, and false negatives for each class.

Parameters:
method_namestr

The name of the evaluator

descriptionstr

The description of the evaluator output

Attributes:
method_namestr

The name of the evaluator

descriptionstr

The description of the evaluator output

servicesServiceBundle or None

The global services bundle

metric_configMetricManager or None

The metric configuration manager

Notes

The confusion matrix is a fundamental tool for evaluating classification model performance. It provides a detailed breakdown of prediction accuracy across all classes, making it easy to identify which classes the model predicts well and which it struggles with.

The evaluator automatically determines the unique labels from the true target values and creates a square matrix where rows represent true classes and columns represent predicted classes.

Examples

Use the confusion matrix evaluator:
>>> from brisk.evaluation.evaluators import registry
>>> evaluator = registry.get("brisk_confusion_matrix")
>>> evaluator.evaluate(model, X, y, "confusion_matrix_results")
calculate_measures(prediction: Series, y: ndarray) Dict[str, Any][source]#

Generate a confusion matrix.

Calculates the confusion matrix for the given predictions and true labels. The matrix shows the count of correct and incorrect predictions for each class.

Parameters:
predictionpd.Series

The predicted target values from the model

ynp.ndarray

The true target values

Returns:
Dict[str, Any]

A dictionary containing: - confusion_matrix: 2D list representing the confusion matrix - labels: List of unique class labels in order

Notes

The confusion matrix is calculated using scikit-learn’s confusion_matrix function with labels determined from the unique values in the true target array. The matrix is converted to a list format for JSON serialization.

The labels are sorted to ensure consistent ordering across different evaluations.

evaluate(model: Any, X: ndarray, y: ndarray, filename: str) None[source]#

Generate and save a confusion matrix.

Executes the complete evaluation workflow for generating a confusion matrix. This includes generating predictions, calculating the confusion matrix, and saving the results with metadata.

Parameters:
modelAny

Trained classification model with predict method

Xnp.ndarray

The input features for evaluation

ynp.ndarray

The true target values

filenamestr

The name of the output file (without extension)

Returns:
None

Notes

This method overrides the base evaluate method to provide classification-specific evaluation workflow. It generates predictions using the model and calculates the confusion matrix with appropriate class labels.

The results are saved as JSON with metadata for later analysis and reporting.

log_results(data: Dict[str, Any]) None[source]#

Log the results of the confusion matrix to console.

Displays the confusion matrix in a formatted table format for easy reading and analysis. The table shows class labels as both row and column headers with prediction counts in the corresponding cells.

Parameters:
dataDict[str, Any]

The confusion matrix data containing: - confusion_matrix: 2D list of prediction counts - labels: List of class labels

Returns:
None

Notes

The logged table format makes it easy to quickly assess model performance across different classes. The format shows true classes as rows and predicted classes as columns, making it straightforward to identify misclassification patterns.

The table is formatted with proper spacing and alignment for readability in console output.

report(results: Dict[str, Any]) Tuple[List[str], List[List[Any]]][source]#

Generate a report of the confusion matrix results.

Converts confusion matrix results into a tabular format suitable for reporting, with actual labels as rows and predicted labels as columns.

Parameters:
resultsDict[str, Any]

The results containing confusion_matrix and labels

Returns:
Tuple[List[str], List[List[Any]]]

A tuple containing: - List of column headers: [“Actual Predicted”, label1, label2, …] - Nested list of rows with actual labels and counts