Professional Documents
Culture Documents
Navigation
Search...
Classification accuracy is the total number of correct predictions divided by the total number of predictions
made for a dataset.
The main reason is that the overwhelming number of examples from the majority class (or classes) will
overwhelm the number of examples in the minority class, meaning that even unskillful models can achieve
accuracy scores of 90 percent, or 99 percent, depending on how severe the class imbalance happens to
be.
In this tutorial, you will discover how to calculate and develop an intuition for precision and recall for
imbalanced classification.
Precision quantifies the number of positive class predictions that actually belong to the positive class.
Recall quantifies the number of positive class predictions made out of all positive examples in the
dataset.
F-Measure provides a single score that balances both the concerns of precision and recall in one
number.
Discover SMOTE, one-class classification, cost-sensitive learning, threshold moving, and much more in my
new book, with 30 step-by-step tutorials and full Python source code.
Start Machine Learning
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 1/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
Update Jan/2020: Improved language about the objective of precision and recall. Fixed typos about
what precision and recall seek to minimize (thanks for the comments!).
Update Feb/2020: Fixed typo in variable name for recall and f1.
Email Address
Tutorial Overview
This tutorial is divided into five parts; they are:
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 2/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
For imbalanced classification problems, the majority class is typically referred to as the negative outcome
(e.g. such as “no change” or “negative test result“), and the minority class is typically referred to as the
positive outcome (e.g. “change” or “positive test result”).
The confusion matrix provides more insight into not only the performance of a predictive model, but also
which classes are being predicted correctly, which incorrectly, and what type of errors are being made.
The simplest confusion matrix is for a two-class classification problem, with negative (class 0) and positive
(class 1) classes.
In this type of confusion matrix, each cell in the table has a specific and well-understood name,
summarized as follows:
Now that we have brushed up on the confusion matrix, let’s take a closer look at the precision metric.
Email Address
Precision is a metric that quantifies the number of correct positive predictions made.
It is calculated as the ratio of correctly predicted positive examples divided by the total number of positive
examples that were predicted.
Precision evaluates the fraction of correct classified instances among the ones classified as
positive …
The result is a value between 0.0 for no precision and 1.0 for full or perfect precision.
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 3/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
Consider a dataset with a 1:100 minority to majority ratio, with 100 minority examples and 10,000 majority
class examples.
A model makes predictions and predicts 120 examples as belonging to the minority class, 90 of which are
correct, and 30 of which are incorrect.
The result is a precision of 0.75, which is a reasonable value but not outstanding.
Start Machine Learning ×
You can see that precision is simply the ratio of correct positive
You canpredictions outMachine
master applied of all positive
Learningpredictions
made, or the accuracy of minority class predictions. without math or fancy degrees.
Find out how in this free and practical course.
Consider the same dataset, where a model predicts 50 examples belonging to the minority class, 45 of
which are true positives and five of which are false positives. We can calculate the precision for this model
Email Address
as follows:
In this case, although the model predicted far fewer examples as belonging to the minority class, the ratio
of correct positive examples is much better.
This highlights that although precision is useful, it does not tell the whole story. It does not comment on how
many real positive class examples were predicted as belonging to the negative class, so-called false
negatives.
Click to sign-up and also get a free PDF Ebook version of the course.
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 4/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
In an imbalanced classification problem with more than two classes, precision is calculated as the sum of
true positives across all classes divided by the sum of true positives and false positives across all classes.
For example, we may have an imbalanced multiclass classification problem where the majority class is the
negative class, but there are two positive minority classes: class 1 and class 2. Precision can quantify the
ratio of correct predictions across both positive classes.
Consider a dataset with a 1:1:100 minority to majority class ratio, that is a 1:1 ratio for each positive class
Start Machine Learning ×
and a 1:100 ratio for the minority classes to the majority class, and we have 100 examples in each minority
class, and 10,000 examples in the majority class. You can master applied Machine Learning
without math or fancy degrees.
A model makes predictions and predicts 70 examples forFind the out
firsthow
minority
in this class,
free andwhere 50course.
practical are correct and
20 are incorrect. It predicts 150 for the second class with 99 correct and 51 incorrect. Precision can be
calculated for this model as follows:
Email Address
We can see that the precision metric calculation scales as we increase the number of minority classes.
For example, we can use this function to calculate precision for the scenarios in the previous section.
First, the case where there are 100 positive to 10,000 negative examples, and a model predicts 90 true
positives and 30 false positives. The complete example is listed below.
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 5/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
Running the example calculates the precision, matching our manual calculation.
1 Precision: 0.750
Next, we can use the same function to calculate precision for the multiclass problem with 1:1:100, with 100
examples in each minority class and 10,000 in the majority class. A model predicts 50 true positives and 20
false positives for class 1 and 99 true positives and 51 false positives for class 2.
When using the precision_score() function for multiclass classification, it is important to specify the minority
classes via the “labels” argument and to perform set the “average” argument to ‘micro‘ to ensure the
calculation is performed as we expect.
Again, running the example calculates the precision for the multiclass example matching our manual
calculation.
1 Precision: 0.677
Unlike precision that only comments on the correct positive predictions out of all positive predictions, recall
provides an indication of missed positive predictions.
In this way, recall provides some notion of the coverage of the positive class.
For imbalanced learning, recall is typically used to measure the coverage of the minority class.
— Page 27, Imbalanced Learning: Foundations, Algorithms, and Applications, 2013.
Start Machine Learning
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 6/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
The result is a value between 0.0 for no recall and 1.0 for full or perfect recall.
As in the previous section, consider a dataset with 1:100 minority to majority ratio, with 100 minority
examples and 10,000 majority class examples.
Start
A model makes predictions and predicts 90 of the positive Machine
class predictions Learning
correctly and 10 incorrectly. We
×
can calculate the recall for this model as follows:
You can master applied Machine Learning
without math or fancy degrees.
Recall = TruePositives / (TruePositives + FalseNegatives)
Find out how in this free and practical course.
Recall = 90 / (90 + 10)
Recall = 90 / 100
Email Address
Recall = 0.9
In an imbalanced classification problem with more than two classes, recall is calculated as the sum of true
positives across all classes divided by the sum of true positives and false negatives across all classes.
As in the previous section, consider a dataset with a 1:1:100 minority to majority class ratio, that is a 1:1
ratio for each positive class and a 1:100 ratio for the minority classes to the majority class, and we have
100 examples in each minority class, and 10,000 examples in the majority class.
A model predicts 77 examples correctly and 23 incorrectly for class 1, and 95 correctly and five incorrectly
for class 2. We can calculate recall for this model as follows:
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 7/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
For example, we can use this function to calculate recall for the scenarios above.
First, we can consider the case of a 1:100 imbalance with 100 and 10,000 examples respectively, and a
model predicts 90 true positives and 10 false negatives.
Running the example, we can see that the score matches the manual calculation above.
START MY EMAIL COURSE
1 Recall: 0.900
We can also use the recall_score() for imbalanced multiclass classification problems.
In this case, the dataset has a 1:1:100 imbalance, with 100 in each minority class and 10,000 in the
majority class. A model predicts 77 true positives and 23 false negatives for class 1 and 95 true positives
and five false negatives for class 2.
Again, running the example calculates the recall for the multiclass example matching our manual
calculation.
Start Machine Learning
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 8/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
1 Recall: 0.860
Maximizing precision will minimize the number false positives, whereas maximizing the recall will minimize
the number of false negatives.
Sometimes, we want excellent predictions of the positive class. We want high precision and high recall.
F-Measure provides a way to combine both precision and recall into a single measure that captures both
properties.
Alone, neither precision or recall tells the whole story. We can have excellent precision with terrible recall,
or alternately, terrible precision with excellent recall. F-measure provides a way to express both concerns
with a single score.
Once precision and recall have been calculated for a binary or multiclass classification problem, the two
scores can be combined into the calculation of the F-Measure.
This is the harmonic mean of the two fractions. This is sometimes called the F-Score or the F1-Score and
might be the most common metric used on imbalanced classification problems.
Start Machine Learning
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 9/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
… the F1-measure, which weights precision and recall equally, is the variant most often used
when learning from imbalanced data.
Like precision and recall, a poor F-Measure score is 0.0 and a best or perfect F-Measure score is 1.0
For example, a perfect precision and recall score would result in a perfect F-Measure score:
Email Address
Consider a model that predicts 150 examples for the positive class, 95 are correct (true positives), meaning
five were missed (false negatives) and 55 are incorrect (false positives).
START MY EMAIL COURSE
We can calculate the precision as follows:
This shows that the model has poor precision, but excellent recall.
We can see that the good recall levels-out the poor precision, giving an okay or reasonable F-measure
score.
Start Machine Learning
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 10/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
For example, we use this function to calculate F-Measure for the scenario above.
This is the case of a 1:100 imbalance with 100 and 10,000 examples respectively, and a model predicts 95
true positives, five false negatives, and 55 false positives.
Running the example computes the F-Measure, matching our manual calculation, within some minor
rounding errors. START MY EMAIL COURSE
1 F-Measure: 0.760
Further Reading
This section provides more resources on the topic if you are looking to go deeper.
Tutorials
How to Calculate Precision, Recall, F1, and More for Deep Learning Models
How to Use ROC Curves and Precision-Recall Curves for Classification in Python
Papers
A Systematic Analysis Of Performance Measures For Classification Tasks, 2009.
Books
Imbalanced Learning: Foundations, Algorithms, and Applications, 2013.
Learning from Imbalanced Data Sets, 2018.
API
sklearn.metrics.precision_score API.
sklearn.metrics.recall_score API. Start Machine Learning
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 11/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
sklearn.metrics.f1_score API.
Articles
Confusion matrix, Wikipedia.
Precision and recall, Wikipedia.
F1 score, Wikipedia.
Summary
In this tutorial, you discovered you discovered how to calculate and develop an intuition for precision and
recall for imbalanced classification.
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 12/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
This seems backwards. Generally, isn’t Precision improved by increasing the classification threshold (i.e., a
higher probability of the Positive class is needed for a True decision) which leads to fewer FalsePositives
and more FalseNegatives. And similarly, isn’t Recall generally improved by lowering the classification
threshold (i.e., a lower probability of the Positive class is needed for a True decision) which leads to more
FalsePositives and fewer FalseNegatives.
REPLY
Jason Brownlee January 4, 2020 at 8:12 am #
Thanks, updated!
REPLY
Ashok Chilakapati January 3, 2020 at 11:59 am #
Very nice set of articles on DS & ML Jason. Thanks for taking the time to write up.
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 13/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
Likewise
By the way, in the context of text classification I have found that working with those
so called “significant terms” enables one to pick the features that enable
better balance between precision and recall
REPLY
Jason Brownlee January 4, 2020 at 8:22 am #
yikes — great catch Curtis — seems rather basic and Im guessing the cause is too much Holiday
Email
Cheer — still a fantastic article Jason, thank you. I am a huge fan.Address
Mark K
REPLY
Anirban January 3, 2020 at 1:20 pm #
There are 3 modes for calculating precision and recall in a multiclass problem, micro, macro and
weighted. Can you kindly discuss when to use which.
REPLY
Jason Brownlee January 4, 2020 at 8:24 am #
REPLY
vaibhav khandelwal January 3, 2020 at 4:55 pm #
The main reason is that the overwhelming number of examples from the majority class (or classes) will
overwhelm the number of examples in the minority class, meaning that even unskillful models can achieve
accuracy scores of 90 percent, or 99 percent, depending on how severe the class imbalance happens to be.
Start Machine Learning
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 14/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
REPLY
Jason Brownlee January 4, 2020 at 8:27 am #
A model will perform “well” by ignoring the minority class and modeling the majority class.
REPLY
Cleyton Farias January 3, 2020 at 8:54 pm #
Great post Jason. I had the same doubt as Curtis. If you have some time to explain the logic behind
the following statement,I would appreciate it.
Start
“Precision: Appropriate when false negatives are more costly. Machine Learning ×
Recall: Appropriate when false positives are more costly.”
You can master applied Machine Learning
without math or fancy degrees.
Find out how in this free and practical course.
REPLY
Jason Brownlee January 4, 2020 at 8:53 am #
Email Address
Thanks, I have updated the tutorial.
REPLY
Kamal Pandey January 17, 2020 at 4:00 am #
REPLY
Jason Brownlee January 17, 2020 at 6:05 am #
REPLY
Kamal Pandey January 17, 2020 at 10:59 am #
Thanks I used it but precision recall and fscore seems to be almost similar just differ in
some digits after decimal is it valid?
REPLY
Jason Brownlee January 17, 2020 at 1:50 pm #
Perhaps investigate the specific predictions made on the test set and understand what
Start Machine Learning
was calculated in the score.
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 15/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
REPLY
faezeh March 26, 2020 at 2:32 am #
REPLY
Jason Brownlee March 26, 2020 at 7:58 am #
REPLY
faezeh March 26, 2020 at 5:14 pm # Email Address
Thanks
it’s true START MY EMAIL COURSE
I want to know how to calculate Precision, Recall, and F-Measure for balanced data?
Does it differ from the unbalanced data method?
REPLY
Jason Brownlee March 27, 2020 at 6:02 am #
Thanks.
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 16/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
REPLY
Jean May 6, 2020 at 11:37 pm #
Great article Jason! The concepts of precision and recall can be useful to assess model
performance in cybersecurity. To illustrate your explanation, here is a blog post about accuracy vs precision
vs recall applied to secrets detection in cybersecurity :
https://blog.gitguardian.com/secrets-detection-accuracy-precision-recall-explained/
REPLY
Jason Brownlee May 7, 2020 at 6:53 am #
You’re welcome.
You can set the “pos_label” argument to specify which is the positive class, for example:
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_score.html
Leave a Reply
Name (required)
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 17/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
Website
SUBMIT COMMENT
Welcome!
My name is Jason Brownlee PhD, and I help developers get results with machine learning.
Read more
Email Address
The Imbalanced Classification with Python EBook is where I keep the Really Good stuff.
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 18/19
6/17/2020 How to Calculate Precision, Recall, and F-Measure for Imbalanced Classification
Email Address
https://machinelearningmastery.com/precision-recall-and-f-measure-for-imbalanced-classification/ 19/19