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: Optional[int] = 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(Optional[int], default:None) –Class index to exclude from computation.
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}