classification
Classification metrics of a confusion matrix: the matrix itself and the accuracy.
Functions:
-
confusion_matrixβCompute the confusion matrix.
-
accuracyβAccuracy of a confusion matrix.
confusion_matrix [source] ΒΆ
confusion_matrix(
preds: Tensor,
target: Tensor,
num_classes: int,
ignore_index: Union[int, Sequence[int], None] = None,
) -> Tensor
Compute the confusion matrix.
Parameters:
-
preds(Tensor) βPredicted class indices, shape \((N,)\).
-
target(Tensor) βGround truth class indices, shape \((N,)\).
-
num_classes(int) βTotal number of classes.
-
ignore_index(Union[int, Sequence[int], None], default:None) βClass index, or indices, whose points are excluded from the matrix.
Returns:
-
TensorβConfusion matrix of shape \((\text{num\_classes}, \text{num\_classes})\) where
-
Tensorβcm[i, j]is the number of points with true classi -
Tensorβpredicted as class
j.
accuracy [source] ΒΆ
accuracy(
cm: Tensor,
*,
average: Literal["micro", "macro"] = ...,
ignore_index: Union[int, Sequence[int], None] = ...,
zero_division: float = ...,
class_names: Optional[Sequence[str]] = ...,
) -> float
accuracy(
cm: Tensor,
*,
average: Literal["micro", "macro", "none"] = "micro",
ignore_index: Union[int, Sequence[int], None] = None,
zero_division: float = 0.0,
class_names: Optional[Sequence[str]] = None,
) -> Union[float, Tensor, Dict[str, float]]
Accuracy of a confusion matrix.
Confusion matrices add up, so the matrix may describe one batch or the sum of confusion_matrix over a whole split.
Parameters:
-
cm(Tensor) βConfusion matrix with true classes as rows, shape \((C, C)\) (see
confusion_matrix). -
average(Literal['micro', 'macro', 'none'], default:'micro') β"micro"returns the overall accuracy (the fraction of points on the diagonal);"macro"returns the mean class accuracy (the mean of the per-class recalls);"none"returns the per-class accuracy. -
ignore_index(Union[int, Sequence[int], None], default:None) βClass index, or indices, to ignore: points whose true class is ignored are dropped, and the ignored classes are left out of the mean. Indices outside \([0, C)\) have no effect.
-
zero_division(float, default:0.0) βAccuracy given to a class without any point (and to an empty matrix with
"micro"). -
class_names(Optional[Sequence[str]], default:None) βName of each class index; with
average="none"the per-class accuracy comes back as a{name: accuracy}dict instead of a tensor.
Returns:
-
Union[float, Tensor, Dict[str, float]]βThe accuracy as a float with
average="micro"or"macro", or the per-class accuracy, shape \((C,)\), -
Union[float, Tensor, Dict[str, float]]βwith
average="none"(a{name: accuracy}dict whenclass_namesis given).
Shape
- cm: \((C, C)\)
- output: scalar, or \((C,)\) with
average="none"
Example
>>> cm = confusion_matrix(torch.tensor([0, 1, 1, 1]), torch.tensor([0, 1, 0, 1]), num_classes=2)
>>> accuracy(cm), accuracy(cm, average="macro")
(0.75, 0.75)
>>> accuracy(cm, average="none")
tensor([0.5000, 1.0000])
>>> accuracy(cm, average="none", class_names=["wall", "floor"])
{'wall': 0.5, 'floor': 1.0}