Skip to main content

Preciasion, Recall and F1 Score in Machine Learning

 




Precision and recall are two metrics used to evaluate the performance of a classifier in binary classification problems.

  • Precision measures the accuracy of positive predictions. It is calculated by dividing the number of true positives by the total number of positive predictions.
  • Recall measures the completeness of positive predictions. It is calculated by dividing the number of true positives by the total number of actual positives.

For example, let's say we have a classifier that predicts whether an email is spam or not. The classifier correctly predicts that 8 emails are spam and incorrectly predicts that 2 emails are not spam. There are actually 12 spam emails in the dataset.

In this case, the precision of the classifier is 8/10 = 0.8, and the recall is 8/12 = 0.67.

  • Precision is often used when the cost of false positives is high. For example, in the spam filtering example, a false positive would be an email that is incorrectly classified as spam. This could lead to the email being deleted, which could be a problem if the email is important.
  • Recall is often used when the cost of false negatives is high. For example, in the medical diagnosis example, a false negative would be a patient who is incorrectly classified as not having a disease. This could lead to the patient not receiving the treatment they need, which could be a serious problem.

The ideal classifier would have perfect precision and recall, but this is rarely possible. In most cases, there is a trade-off between precision and recall. Increasing precision typically reduces recall, and vice versa.

The best way to choose between precision and recall depends on the specific application. For example, if the cost of false positives is high, then precision should be prioritized. If the cost of false negatives is high, then recall should be prioritized.

Here are some other examples of precision and recall:

  • A medical diagnostic test with a precision of 0.9 and a recall of 0.8 means that the test correctly identifies 90% of the patients who have the disease and 80% of the patients who do not have the disease.
  • A spam filtering algorithm with a precision of 0.8 and a recall of 0.6 means that the algorithm correctly identifies 80% of the spam emails and 60% of the non-spam emails.
  • A search engine with a precision of 0.7 and a recall of 0.5 means that the search engine correctly returns 70% of the relevant results and 50% of all the results.

The F1 score is a measure of a model's accuracy that combines precision and recall. It is calculated as the harmonic mean of precision and recall. The harmonic mean is more sensitive to low values than the arithmetic mean, so it gives more weight to the lower of the two scores.

The F1 score is often used in machine learning and natural language processing to evaluate the performance of binary classifiers. A binary classifier is a model that predicts one of two classes, such as spam or not spam, or positive or negative.

The F1 score is calculated as follows:

F1 = 2 * (precision * recall) / (precision + recall)

where:

  • Precision is the number of true positives divided by the number of true positives plus the number of false positives.
  • Recall is the number of true positives divided by the number of true positives plus the number of false negatives.

The F1 score can range from 0 to 1, where 1 is the best possible score. A score of 0 means that the model is not making any correct predictions, and a score of 1 means that the model is making perfect predictions.

The F1 score is a good measure of a model's accuracy because it takes into account both precision and recall. Precision measures how accurate the model is, while recall measures how complete the model is. The F1 score gives more weight to the lower of the two scores, so it is a good measure of a model's overall performance.

The F1 score is often used in conjunction with other metrics, such as accuracy, to evaluate the performance of a model. Accuracy is the percentage of predictions that the model makes correctly. The F1 score and accuracy can be complementary metrics, as a model can have a high accuracy but a low F1 score if it is only predicting one class of data.

The F1 score is a useful metric for evaluating the performance of binary classifiers. It is a good measure of a model's overall accuracy and takes into account both precision and recall. The F1 score is often used in conjunction with other metrics, such as accuracy, to get a more complete picture of a model's performance.

We can get the TP, FP, FN, and FP from the confusion matrix.

Here is a confusion matrix for the spam filtering example:

Actual Spam | Predicted Spam | Predicted Not Spam

---------------------------------------------------------------------------

Spam | True Positive (TP) | False Negative (FN)

Not Spam | False Positive (FP) | True Negative (TN)

True Positive (TP): The email is actually spam and is correctly classified as spam.

False Negative (FN): The email is actually spam but is incorrectly classified as not spam.

False Positive (FP): The email is not spam but is incorrectly classified as spam.

True Negative (TN): The email is not spam and is correctly classified as not spam.

In this case, the TP is 8, the FN is 4, the FP is 2, and the TN is 10.

The confusion matrix can be used to calculate precision and recall. Precision is calculated by dividing the TP by the sum of the TP and FP. Recall is calculated by dividing the TP by the sum of the TP and FN.

In this case, the precision is 8/10 = 0.8, and the recall is 8/12 = 0.67.

Photo by Thomas Griggs on Unsplash

Comments

Popular posts from this blog

Financial Engineering

Financial Engineering: Key Concepts Financial engineering is a multidisciplinary field that combines financial theory, mathematics, and computer science to design and develop innovative financial products and solutions. Here's an in-depth look at the key concepts you mentioned: 1. Statistical Analysis Statistical analysis is a crucial component of financial engineering. It involves using statistical techniques to analyze and interpret financial data, such as: Hypothesis testing : to validate assumptions about financial data Regression analysis : to model relationships between variables Time series analysis : to forecast future values based on historical data Probability distributions : to model and analyze risk Statistical analysis helps financial engineers to identify trends, patterns, and correlations in financial data, which informs decision-making and risk management. 2. Machine Learning Machine learning is a subset of artificial intelligence that involves training algorithms t...

Wholesale Customer Solution with Magento Commerce

The client want to have a shop where regular customers to be able to see products with their retail price, while Wholesale partners to see the prices with ? discount. The extra condition: retail and wholesale prices hasn’t mathematical dependency. So, a product could be $100 for retail and $50 for whole sale and another one could be $60 retail and $50 wholesale. And of course retail users should not be able to see wholesale prices at all. Basically, I will explain what I did step-by-step, but in order to understand what I mean, you should be familiar with the basics of Magento. 1. Creating two magento websites, stores and views (Magento meaning of website of course) It’s done from from System->Manage Stores. The result is: Website | Store | View ———————————————— Retail->Retail->Default Wholesale->Wholesale->Default Both sites using the same category/product tree 2. Setting the price scope in System->Configuration->Catalog->Catalog->Price set drop-down to...

How to Prepare for AI Driven Career

  Introduction We are all living in our "ChatGPT moment" now. It happened when I asked ChatGPT to plan a 10-day holiday in rural India. Within seconds, I had a detailed list of activities and places to explore. The speed and usefulness of the response left me stunned, and I realized instantly that life would never be the same again. ChatGPT felt like a bombshell—years of hype about Artificial Intelligence had finally materialized into something tangible and accessible. Suddenly, AI wasn’t just theoretical; it was writing limericks, crafting decent marketing content, and even generating code. The world is still adjusting to this rapid shift. We’re in the middle of a technological revolution—one so fast and transformative that it’s hard to fully comprehend. This revolution brings both exciting opportunities and inevitable challenges. On the one hand, AI is enabling remarkable breakthroughs. It can detect anomalies in MRI scans that even seasoned doctors might miss. It can trans...