holisticai.bias.metrics.confusion_tensor#
- holisticai.bias.metrics.confusion_tensor(p_attr, y_pred, y_true, groups=None, classes=None, as_tensor=False)[source]#
Confusion Tensor
This function computes the confusion tensor. The k, i, jth entry is the number of instances of group k with predicted class i and true class j.
Parameters
- p_attrarray-like
Protected attribute vector (categorical)
- y_predarray-like
Prediction vector (categorical)
- y_truearray-like
Target vector (categorical)
- groupslist, optional
Unique groups from p_attr in order
- classeslist, optional
The unique output classes in order
- as_tensorbool, optional
Whether we return a tensor or DataFrame, default False
Returns
- numpy ndarray
Confusion Tensor : shape (num_groups, num_classes, num_classes)
Examples
>>> import numpy as np >>> from holisticai.bias.metrics import confusion_tensor >>> p_attr = np.array(["A", "A", "A", "A", "B", "B", "B", "B", "C", "C"]) >>> y_pred = np.array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0]) >>> y_true = np.array([0, 1, 1, 0, 1, 0, 2, 1, 2, 1]) >>> confusion_tensor(p_attr, y_pred, y_true, as_tensor=True).shape (3, 3, 3) >>> confusion_tensor(p_attr, y_pred, y_true, as_tensor=True) array([[[2., 0., 0.], [0., 1., 0.], [0., 1., 0.]],
- [[0., 0., 1.],
[0., 2., 0.], [1., 0., 0.]],
- [[0., 1., 0.],
[0., 0., 0.], [0., 0., 1.]]])
>>> confusion_tensor(p_attr, y_pred, y_true, as_tensor=False) A B C 0 1 2 0 1 2 0 1 2 0 2.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 1 0.0 1.0 0.0 0.0 2.0 0.0 0.0 0.0 0.0 2 0.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0