Loading...

vastorbit.machine_learning.metrics.confusion_matrix

vastorbit.machine_learning.metrics.confusion_matrix(y_true: str, y_score: str, input_relation: Annotated[str | VastFrame, ''], labels: Annotated[list | ndarray, 'Array Like Structure'] | None = None, pos_label: Annotated[bool | float | str | timedelta | datetime, 'Python Scalar'] | None = None) ndarray

Computes the confusion matrix using SQL.

Parameters:
  • y_true (str) – Response column.

  • y_score (str) – Prediction column.

  • input_relation (SQLRelation) – Relation used for scoring. This relation can be a view, table, or a customized relation (if an alias is used at the end of the relation). For example: (SELECT … FROM …) x

  • labels (ArrayLike, optional) – List of the response column categories.

  • pos_label (PythonScalar, optional) – Label used to identify the positive class. If pos_label is NULL then the global accuracy is computed.

Returns:

Confusion matrix as 2D array. For binary: [[TN, FP], [FN, TP]] For multi-class: rows=actual, cols=predicted

Return type:

numpy.ndarray

Examples

Binary Classification:

import vastorbit as vo
from vastorbit.machine_learning.metrics import confusion_matrix

data = vo.VastFrame({
    "y_true": [1, 1, 0, 0, 1],
    "y_pred": [1, 1, 1, 0, 1],
})

cm = confusion_matrix(
    y_true="y_true",
    y_score="y_pred",
    input_relation=data,
)
print(cm)
# [[1 1]
#  [0 3]]

Multi-class Classification:

data = vo.VastFrame({
    "y_true": [1, 2, 0, 0, 1],
    "y_pred": [1, 2, 0, 1, 1],
})

cm = confusion_matrix(
    y_true="y_true",
    y_score="y_pred",
    labels=[0, 1, 2],
    input_relation=data,
)
print(cm)
# [[2 0 0]
#  [1 1 0]
#  [0 0 1]]