Skip to main content

Understanding the Confusion Matrix, Precision, Recall, F1 Score, and Accuracy (A Beginner’s Guide part 6)

Understanding the Confusion Matrix, Precision, Recall, F1 Score, and Accuracy

In the realm of machine learning, evaluating the performance of your models is crucial. Various metrics help in understanding how well your model is performing, and among them, the confusion matrix, precision, recall, F1 score, and accuracy are fundamental. This guide will walk you through these concepts, providing a clear understanding and practical examples.

What is a Confusion Matrix?

A confusion matrix is a table used to evaluate the performance of a classification model. It helps in understanding the types of errors made by the model. The matrix contrasts the actual target values with those predicted by the model.

Structure of a Confusion Matrix

For a binary classification problem, the confusion matrix looks like this:


  • True Positive (TP): The model correctly predicts the positive class.
  • True Negative (TN): The model correctly predicts the negative class.
  • False Positive (FP): The model incorrectly predicts the positive class.
  • False Negative (FN): The model incorrectly predicts the negative class.

Precision

Precision is the ratio of correctly predicted positive observations to the total predicted positives. It answers the question: What proportion of positive identifications was actually correct?

Precision=TPTP+FP\text{Precision} = \frac{TP}{TP + FP}

High precision indicates a low false positive rate.

Example Calculation

Let's say you have the following confusion matrix:

Using the above confusion matrix:

Precision=44+1=45=0.80

Recall (Sensitivity)

Recall, or sensitivity, is the ratio of correctly predicted positive observations to all observations in the actual positive class. It answers the question: What proportion of actual positives was identified correctly?

Recall=TPTP+FN​

High recall indicates a low false negative rate.

Example Calculation

Using the same confusion matrix:

Recall=44+1=45=0.80\text{Recall} = \frac{4}{4 + 1} = \frac{4}{5} = 0.80


F1 Score

The F1 Score is the harmonic mean of precision and recall, providing a balance between the two metrics. It is particularly useful when you need to account for both false positives and false negatives.

F1 Score=2×Precision×RecallPrecision+Recall\text{F1 Score} = 2 \times \frac{\text{Precision} \times \text{Recall}}{\text{Precision} + \text{Recall}}

Example Calculation

Using our previous precision and recall values:

F1 Score=2×0.80×0.800.80+0.80=2×0.641.60=0.80\text{F1 Score} = 2 \times \frac{0.80 \times 0.80}{0.80 + 0.80} = 2 \times \frac{0.64}{1.60} = 0.80


Accuracy

Accuracy is the ratio of correctly predicted observations to the total observations. It answers the question: What proportion of the total predictions were correct?

Accuracy=TP+TNTP+TN+FP+FN\text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN}

Accuracy is a great measure when the classes are balanced, but it can be misleading when there is an imbalance.

Example Calculation

Using the same confusion matrix:

Accuracy=4+44+4+1+1=810=0.80\text{Accuracy} = \frac{4 + 4}{4 + 4 + 1 + 1} = \frac{8}{10} = 0.80



equations

  • Accuracy:

    Accuracy=TP+TNTP+TN+FP+FN\text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN}
  • Precision:

    Precision=TPTP+FP\text{Precision} = \frac{TP}{TP + FP}
  • Recall:

    Recall=TPTP+FN\text{Recall} = \frac{TP}{TP + FN}
  • F1 Score:

    F1 Score=2×(Precision×Recall)Precision+Recall\text{F1 Score} = \frac{2 \times (\text{Precision} \times \text{Recall})}{\text{Precision} + \text{Recall}}



  • Sithija Theekshana 

    (bsc in Computer Science and Information Technology)

    (bsc in Applied Physics and Electronics)


    linkedin ;- www.linkedin.com/in/sithija-theekshana-008563229


    Comments

    Popular posts from this blog

    Tensorflow

      When comparing various deep learning frameworks, it’s evident that TensorFlow stands out as the preferred choice among academics, businesses, and developers. GitHub activity for different ML frameworks( Source ) What is TensorFlow? TensorFlow is an open-source software library designed for machine learning and artificial intelligence. While it supports a variety of tasks, it is particularly well-suited for training and inference of deep neural networks. Alongside PyTorch, TensorFlow is one of the two most widely used deep learning libraries. Developed by Google Brain for internal research and production, the first version was released under the Apache License 2.0 in 2015. Google later introduced TensorFlow 2.0 in September 2019. TensorFlow supports multiple programming languages, including Python, JavaScript, C++, and Java, making it versatile for various applications across different industries. EXPLAIN TENSORFLOW What is a Tensor? A tensor is an n-dimensional vector or mat...

    Understanding the K-Nearest Neighbors Algorithm (A Beginner's Guide part 7)

    Understanding the K-Nearest Neighbors Algorithm (A Beginner's Guide) Machine learning algorithms can seem complex, but breaking them down into simpler terms can make them more approachable. One such algorithm is the K-Nearest Neighbors (K-NN) algorithm, which is popular for its simplicity and effectiveness. In this blog, we'll explore what K-NN is, how it works, and some practical applications. What is K-Nearest Neighbors? K-Nearest Neighbors (K-NN) is a supervised learning algorithm used for classification and regression tasks. In simple terms, K-NN classifies data points based on the 'votes' of their nearest neighbors. It doesn't make any assumptions about the underlying data distribution, making it a non-parametric algorithm. How Does K-NN Work? The K-Nearest Neighbors algorithm operates based on the idea that data points that are close to each other tend to have similar properties or belong to the same class. Here’s a detailed step-by-step process of how K-NN wo...

    cloud computing(sinhala)

                          cloud computing  cloud යනු කුමක්ද යන්න  තවදුරටත් රහසක් නොවේ. එය ඩිජිටල් පරිවර්තනයේ සහ නවීන තාක්‍ෂණයේ සෑම අංශයකම බහුලව භාවිතා වන යෙදුමක් වන අතර clouds එදිනෙදා ජීවිතයේ කොටසක් වනු ඇතැයි අපි පිළිගෙන ඇත්තෙමු .cloud shift යන්න තවමත් සම්පූර්ණයෙන් වටහාගෙන නැතත්. නමුත් cloud infrastructure (වලාකුළු යටිතල ව්‍යුහය) සහ එය අපට ලබා දෙන දේ තේරුම් නොගැනීමෙන් අදහස් වන්නේ අපි මෙම අත්‍යවශ්‍ය තාක්‍ෂණය සුළුවෙන් ලබාගන්නා වගයි. cloud  හොඳින් භාවිතා කිරීම සඳහා  cloud computing පිළිබඳ  හොඳ අවබෝධයක් අවශ්‍ය වේ. cloud computing යනු කුමක්ද ? සහ එය ක්‍රියා කරන්නේ කෙසේද? මීට වසර කිහිපයකට පෙර, cloud පිළිබඳ මූලික සංකල්පය එය "වෙනත් කෙනෙකුගේ පරිගණකය"  (“someone else’s computer,”) අදහස් කිරීම මගින් උපහාසයට ලක් කරන ලදී, එය තොරතුරු තාක්ෂණ වෘත්තිකයන් කිහිප දෙනෙකුගේ කෝපි මග් අලංකාර කරන කියමනකි.Oracle CTO  ලැරී එලිසන් ඒ හා සමානව අර්ත දැක්වූ  අතර, "අපි දැනටමත් කරන සෑම දෙයක්ම ඇතුළත් කිරීම සඳහා අපි cloud compu...