Confusion Matrix
Free confusion matrix calculator for ML models. Compute accuracy, precision, recall, F1 score, specificity, and MCC from TP, FN, FP, TN with interactive charts.
About This Calculator
The Confusion Matrix Calculator is an essential tool for data scientists, machine learning engineers, and students who need to evaluate classification model performance. By entering the four fundamental values from your model's predictions — True Positives (TP), False Negatives (FN), False Positives (FP), and True Negatives (TN) — you get a comprehensive suite of performance metrics that reveal how well your classifier is working.
A confusion matrix is a table layout that allows you to visualize the performance of a supervised learning algorithm. Each row represents the actual class, while each column represents the predicted class. From this 2x2 matrix, we derive:
- Accuracy — the proportion of all correct predictions: (TP + TN) / (TP + FN + FP + TN)
- Precision — how many positive predictions were actually correct: TP / (TP + FP)
- Recall (Sensitivity) — how many actual positives were found: TP / (TP + FN)
- F1 Score — the harmonic mean of precision and recall, balancing both metrics
- Specificity (TNR) — how many actual negatives were correctly identified: TN / (TN + FP)
- False Positive Rate — the proportion of negatives incorrectly classified as positive: FP / (FP + TN)
- False Negative Rate — the proportion of positives incorrectly classified as negative: FN / (FN + TP)
- False Discovery Rate — the proportion of positive predictions that are wrong: FP / (TP + FP)
- Matthews Correlation Coefficient (MCC) — a balanced measure that considers all four categories, ranging from -1 to +1
This calculator is useful across all regions and industries — from healthcare diagnostics and fraud detection to spam filtering and sentiment analysis. The metrics computed are universally standard in machine learning and do not depend on any specific country's regulations or currency.
Frequently Asked Questions
What is a confusion matrix in machine learning?
A confusion matrix is a performance evaluation tool for classification machine learning models. It summarizes prediction results by comparing actual versus predicted classifications using four metrics: true positives (correctly predicted positive cases), false negatives (positive cases incorrectly predicted as negative), false positives (negative cases incorrectly predicted as positive), and true negatives (correctly predicted negative cases). From these four values, you can derive accuracy, precision, recall, F1 score, specificity, and the Matthews correlation coefficient.
How do you calculate accuracy from a confusion matrix?
Accuracy is calculated as the sum of true positives and true negatives divided by the total number of predictions: Accuracy = (TP + TN) / (TP + FN + FP + TN). For example, if a model produces 80 TP, 70 FN, 20 FP, and 30 TN out of 200 total predictions, the accuracy would be (80 + 30) / 200 = 0.55 or 55%.
What is the difference between precision and recall?
Precision measures how many of the positive predictions were correct, calculated as TP / (TP + FP). Recall (sensitivity) measures how many of the actual positive cases were correctly identified, calculated as TP / (TP + FN). High precision means few false positives, while high recall means few false negatives. The F1 score combines both using their harmonic mean.
How is the F1 score calculated?
The F1 score is the harmonic mean of precision and recall, calculated as F1 = 2 × Precision × Recall / (Precision + Recall). It ranges from 0 to 1, with 1 being perfect precision and recall. The F1 score is especially useful when you need to balance precision and recall, such as in imbalanced datasets where accuracy alone can be misleading.
What is the Matthews correlation coefficient (MCC)?
The Matthews correlation coefficient (MCC) is a balanced metric that considers all four confusion matrix values. It is calculated as MCC = (TP × TN - FP × FN) / sqrt((TP + FP)(TP + FN)(TN + FP)(TN + FN)). MCC ranges from -1 (complete disagreement) through 0 (random predictions) to +1 (perfect predictions). It is considered more reliable than accuracy for imbalanced datasets.
What is a good F1 score value?
A good F1 score depends on your specific use case and dataset. In general, an F1 score above 0.8 is considered excellent, 0.6 to 0.8 is good, 0.4 to 0.6 is fair, and below 0.4 is poor. However, in critical applications like medical diagnosis, even an F1 score of 0.9 may not be sufficient, and in simpler tasks, 0.7 might be acceptable.
What is specificity in a confusion matrix?
Specificity, also called the true negative rate (TNR), measures the proportion of actual negative cases that were correctly identified. It is calculated as TN / (TN + FP). High specificity means the model rarely produces false positives. Specificity complements recall (sensitivity), and together they provide a complete picture of model performance on both positive and negative classes.
What is the difference between Type I and Type II errors in a confusion matrix?
A Type I error (false positive) occurs when the model predicts a positive class for an actual negative instance. A Type II error (false negative) occurs when the model predicts a negative class for an actual positive instance. In the confusion matrix, Type I errors are FP, and Type II errors are FN. The false positive rate measures Type I errors, while the false negative rate measures Type II errors.
Can the confusion matrix be used for multi-class classification?
Yes, confusion matrices can be extended to multi-class classification problems. Instead of a 2x2 matrix, you get an n x n matrix where n is the number of classes. For each class, you can calculate per-class precision, recall, and F1 scores. The overall accuracy is the sum of diagonal elements divided by the total number of predictions.