ConfusionMatrix<T>
Models & Types · AiDotNet.LinearAlgebra
Represents a confusion matrix for evaluating the performance of a classification model.
Inheritance: MatrixBase<T> → ConfusionMatrix<T>
For Beginners
A confusion matrix helps you understand how well your AI model is performing when classifying data into categories.
How It Works
A confusion matrix is a table that summarizes the prediction results of a classification model. It supports both binary classification (2x2 matrix) and multi-class classification (NxN matrix).
Binary Classification: For 2-class problems, it shows four important numbers:
- True PositivesWhen your model correctly predicted “Yes” (e.g., correctly identified a cat as a cat)
- True NegativesWhen your model correctly predicted “No” (e.g., correctly identified a non-cat as not a cat)
- False PositivesWhen your model incorrectly predicted “Yes” (e.g., identified a dog as a cat) - also called a “Type I error”
- False NegativesWhen your model incorrectly predicted “No” (e.g., identified a cat as not a cat) - also called a “Type II error”
Multi-Class Classification: For 3+ class problems, the matrix is NxN where N is the number of classes. Rows represent predicted classes, columns represent actual classes. Cell [i,j] contains the count of samples predicted as class i but actually belonging to class j.
Constructors
| Constructor | Summary |
|---|---|
ConfusionMatrix(T truePositives, T trueNegatives, T falsePositives, T falseNegatives) |
Initializes a new instance of the ConfusionMatrix class with the specified values. |
ConfusionMatrix(int dimension) |
Initializes a new instance of the ConfusionMatrix class with the specified dimension. |
Properties
| Property | Summary |
|---|---|
T Accuracy { get; } |
Gets the accuracy of the classification model. |
T F1Score { get; } |
Gets the F1 score of the classification model. |
T FalseNegatives { get; } |
Gets the number of false negative predictions (incorrectly predicted negative cases). |
T FalsePositives { get; } |
Gets the number of false positive predictions (incorrectly predicted positive cases). |
T Precision { get; } |
Gets the precision of the classification model. |
T Recall { get; } |
Gets the recall (sensitivity) of the classification model. |
T Specificity { get; } |
Gets the specificity of the classification model. |
T TrueNegatives { get; } |
Gets the number of true negative predictions (correctly predicted negative cases). |
T TruePositives { get; } |
Gets the number of true positive predictions (correctly predicted positive cases). |
int ClassCount { get; } |
Gets the number of classes represented in the confusion matrix. |
Methods
| Method | Summary |
|---|---|
T GetAccuracy() |
Gets the overall accuracy across all classes. |
T GetCohenKappa() |
Gets the Cohen’s Kappa coefficient, measuring inter-rater agreement. |
T GetF1Score(int classIndex) |
Gets the F1 score for a specific class. |
T GetFalseNegatives(int classIndex) |
Gets the false negatives for a specific class. |
T GetFalsePositives(int classIndex) |
Gets the false positives for a specific class. |
T GetHammingLoss() |
Gets the Hamming Loss, measuring the fraction of incorrect predictions. |
T GetJaccardScore() |
Gets the Jaccard Score (Jaccard Index) for classification. |
T GetJaccardScore(int classIndex) |
Gets the Jaccard Score for a specific class. |
T GetMacroF1Score() |
Gets the macro-averaged F1 score across all classes. |
T GetMacroPrecision() |
Gets the macro-averaged precision across all classes. |
T GetMacroRecall() |
Gets the macro-averaged recall across all classes. |
T GetMatthewsCorrelationCoefficient() |
Gets the Matthews Correlation Coefficient (MCC) for classification. |
T GetMicroF1Score() |
Gets the micro-averaged F1 score across all classes. |
T GetMicroPrecision() |
Gets the micro-averaged precision across all classes. |
T GetMicroRecall() |
Gets the micro-averaged recall across all classes. |
T GetPrecision(int classIndex) |
Gets the precision for a specific class. |
T GetRecall(int classIndex) |
Gets the recall for a specific class. |
T GetTrueNegatives(int classIndex) |
Gets the true negatives for a specific class. |
T GetTruePositives(int classIndex) |
Gets the true positives for a specific class. |
T GetWeightedF1Score() |
Gets the weighted-averaged F1 score across all classes. |
T GetWeightedPrecision() |
Gets the weighted-averaged precision across all classes. |
T GetWeightedRecall() |
Gets the weighted-averaged recall across all classes. |
void Increment(int predictedClass, int actualClass) |
Increments the count for a specific prediction-actual class pair. |