Confusion Matrix (xcolumns.confusion_matrix)

xcolumns.confusion_matrix module implements confusion matrix object and functions that can be used to calculate it. In xCOLUMNs, the confusion matrix is parametrized by four matrices: true positive (tp), false positive (fp), false negative (fn), and true negative (tn). The confusion matrix object can be used to calculate the metrics based on the confusion matrix. xCOLUMNs implements the popular metrics in xcolumns.metrics module.

class xcolumns.confusion_matrix.ConfusionMatrix(tp: int | float | number | ndarray, fp: int | float | number | ndarray, fn: int | float | number | ndarray, tn: int | float | number | ndarray)[source]

Bases: object

Class representing a confusion matrix, containing counts or ratios of true positives, false positives, false negatives, and true negatives. Implements basic operations for confusion matrices such as comparison, addition, subtraction, multiplication, and division.

Additionally, it can be unpacked into a tuple of counts or ratios (tp, fp, fn, tn) (in this order). So it can be used with metric functions that expect tp, fp, fn, and tn as separate arguments.

normalize() ConfusionMatrix[source]

Normalize the confusion matrix, resulting the rates instead of counts.

Returns:

The normalized confusion matrix.

xcolumns.confusion_matrix.calculate_confusion_matrix(y_true: ndarray | csr_matrix, y_pred: ndarray | csr_matrix, normalize: bool = False, skip_tn: bool = False, axis: int | None = 0) ConfusionMatrix[source]

Calculate confusion matrix for given true and predicted labels along an axis.

Parameters:
  • y_true – The true labels.

  • y_pred – The predicted labels.

  • normalize – Whether to normalize the confusion matrix, resulting the rates instead of counts.

  • skip_tn – Whether to skip calculating true negatives, as they may not be always needed.

  • axis – The axis along which to calculate the confusion matrix.

Returns:

The confusion matrix.

xcolumns.confusion_matrix.calculate_fn(y_true: ndarray | csr_matrix, y_pred: ndarray | csr_matrix, normalize: bool = False, axis: int | None = 0) int | float | number | ndarray[source]

Calculate number of false negatives for the given true and predicted labels along an axis.

Parameters:
  • y_true – The true labels.

  • y_pred – The predicted labels.

  • normalize – Whether to normalize the confusion matrix, resulting the rates instead of counts.

  • axis – The axis along which to calculate the confusion matrix.

Returns:

The number of vector of false negatives counts or rates.

xcolumns.confusion_matrix.calculate_fp(y_true: ndarray | csr_matrix, y_pred: ndarray | csr_matrix, normalize: bool = False, axis: int | None = 0) int | float | number | ndarray[source]

Calculate number of false positives for the given true and predicted labels along an axis.

Parameters:
  • y_true – The true labels.

  • y_pred – The predicted labels.

  • normalize – Whether to normalize the confusion matrix, resulting the rates instead of counts.

  • axis – The axis along which to calculate the confusion matrix.

Returns:

The number of vector of false positives counts or rates.

xcolumns.confusion_matrix.calculate_tp(y_true: ndarray | csr_matrix, y_pred: ndarray | csr_matrix, normalize: bool = False, axis: int | None = 0) int | float | number | ndarray[source]

Calculate number of true positives for the given true and predicted labels along an axis.

Parameters:
  • y_true – The true labels.

  • y_pred – The predicted labels.

  • normalize – Whether to normalize the confusion matrix, resulting the rates instead of counts.

  • axis – The axis along which to calculate the confusion matrix.

Returns:

The number of vector of false positives counts or rates.