Accuracy Formula in Machine Learning
By Rahul Singh
Updated on Jun 10, 2026 | 8 min read | 2.91K+ views
Share:
Looks like you're browsing from the
United StatesSome programs may not be available in your location
Some programs may not be available in your location
Switch to upGrad USAll courses
Certifications
More
By Rahul Singh
Updated on Jun 10, 2026 | 8 min read | 2.91K+ views
Share:
Table of Contents
The accuracy formula in machine learning is one of the most widely used metrics for evaluating classification models. It measures how often a model makes correct predictions by comparing the number of correct predictions against the total number of predictions.
Because of its simplicity, accuracy is often the first metric data scientists use to assess model performance.
In this blog, you will learn exactly what the accuracy formula in machine learning is, how to calculate it step by step, when to use it, when not to rely on it, and how it compares to other evaluation metrics like precision, recall, and F1 score.
Accuracy measures how often your model predicts the correct outcome. In simple terms, it is the ratio of correct predictions to total predictions.
The accuracy formula in machine learning is:
Accuracy = (Number of Correct Predictions) / (Total Number of Predictions)
Or written using classification terms:
Accuracy = (TP + TN) / (TP + TN + FP + FN)
Where:
These four values come from what is called a confusion matrix, which is a table that summarises all prediction outcomes for a classification model.
Suppose your model evaluates 100 patient records to predict whether someone has a disease.
Accuracy = (35 + 55) / (35 + 55 + 5 + 5) = 90 / 100 = 0.90 or 90%
That seems like a strong result. But as you will see further below, 90% accuracy does not always mean the model is performing well.
Also Read: Career in Data Science: Jobs, Salary, and Skills Required
The formula for accuracy in machine learning returns a value between 0 and 1. Multiply by 100 to get the percentage. Most libraries like Scikit-learn return accuracy as a decimal by default, so 0.90 means 90%.
Knowing the formula is one thing. Applying it correctly is another. Here is a clean step-by-step process.
Always evaluate your model on data it has never seen before. This is called the test set. Using training data to measure accuracy gives you inflated numbers that do not reflect real performance.
You need two lists:
Count the TP, TN, FP, and FN values from your predictions.
Actual / Predicted |
Predicted: Positive |
Predicted: Negative |
| Actual: Positive | True Positive (TP) | False Negative (FN) |
| Actual: Negative | False Positive (FP) | True Negative (TN) |
Use the accuracy score formula in machine learning:
Accuracy = (TP + TN) / (TP + TN + FP + FN)
from sklearn.metrics import accuracy_score
y_true = [1, 0, 1, 1, 0, 1, 0, 0, 1, 0]
y_pred = [1, 0, 1, 0, 0, 1, 1, 0, 1, 0]
print(accuracy_score(y_true, y_pred))
# Output: 0.8
This tells you 80% of predictions were correct.
Also Read: Machine Learning with Python: List of Algorithms You Need to Master
This is the part most beginner guides skip. Understanding the limits of the accuracy formula in machine learning is just as important as knowing how to use it.
Accuracy works well when:
Also Read: 25 Must-Try Machine Learning Projects in Python for Beginners and Experts in 2026
Examples where accuracy is appropriate:
Accuracy becomes dangerous in imbalanced datasets. This is where many beginners get tripped up.
Imagine a fraud detection model. Out of 1,000 transactions, only 10 are fraudulent. If your model just predicts "not fraud" for every single transaction, it achieves:
Accuracy = 990 / 1000 = 99%
That looks incredible. But the model has completely failed at its actual job. It catches zero fraud cases.
This is called the accuracy paradox. High accuracy does not always mean a useful model.
Situations where accuracy misleads:
In these cases, you need to look beyond accuracy to metrics like precision, recall, and F1 score.
Also Read: Top 50 Python AI & Machine Learning Open-source Projects
People often confuse accuracy with precision. They are related but measure very different things. Understanding the accuracy and precision formula in machine learning together gives you a much clearer picture of model quality.
Metric |
Formula |
What It Measures |
| Accuracy | (TP + TN) / (TP + TN + FP + FN) | Overall correctness |
| Precision | TP / (TP + FP) | Of all positive predictions, how many were actually positive |
| Recall | TP / (TP + FN) | Of all actual positives, how many did the model catch |
| F1 Score | 2 x (Precision x Recall) / (Precision + Recall) | Balance between precision and recall |
Use precision when false positives are costly. For example, in email spam detection, incorrectly marking a real email as spam (false positive) is more disruptive than letting a spam email through.
Use recall when false negatives are costly. In cancer screening, missing an actual case (false negative) is far more dangerous than flagging a healthy patient for further testing.
Also Read: Machine Learning Tools: A Guide to Platforms and Applications
Say a model predicts whether a loan applicant will default.
Accuracy = (80 + 870) / 1000 = 95%
Precision = 80 / (80 + 30) = 72.7%
Recall = 80 / (80 + 20) = 80%
Accuracy alone would suggest a great model. But precision at 72.7% tells you the model is incorrectly flagging quite a few good borrowers. Depending on business goals, that might be unacceptable.
The accuracy and precision formula in machine learning, used together, tell a much more honest story.
Also Read: 7 Most Used Machine Learning Algorithms in Python You Should Know About
The accuracy score formula in machine learning applies most cleanly to classification tasks. Here is how it fits across different problem types.
This is the most common case. Two classes: positive or negative, yes or no, 0 or 1. The formula applies directly.
Example: Email is spam or not spam.
Your model predicts one of three or more classes. Accuracy still works the same way.
Accuracy = (All Correct Predictions) / (Total Predictions)
Every class contributes to correct or incorrect predictions.
Example: Classifying news articles as Sports, Politics, Technology, or Entertainment.
Also Read: Classification Model Using Artificial Neural Networks (ANN) with Keras
Each sample can belong to multiple classes at once. Standard accuracy becomes tricky here. Metrics like Hamming Loss or subset accuracy are often used instead.
Example: Tagging a photo as both "beach" and "sunset."
Accuracy in its standard form does not apply to regression, where the output is a continuous number. Instead, you use metrics like:
Accuracy is specifically a classification metric.
Getting a higher accuracy score formula in machine learning result is not just about trying different algorithms. It requires a structured approach.
Data quality first:
Feature engineering:
Also Read: Top 30+ Linear Regression Projects
Model selection and tuning:
Handle class imbalance:
Evaluate with multiple metrics:
Never rely on the formula for accuracy in machine learning alone. Always check precision, recall, and F1 score alongside accuracy to get the full picture.
Also Read: Multiple Linear Regression in Machine Learning: Concepts and Implementation
The accuracy formula in machine learning is where every practitioner starts. It is intuitive, easy to compute, and gives you a quick read on how your model is performing. But as this guide has shown, it is just one piece of the puzzle.
Accuracy works best with balanced datasets and equal error costs. In imbalanced scenarios, it can give you a false sense of security. That is why combining accuracy with precision, recall, and F1 score is the standard practice in real-world ML projects.
Want personalized guidance on AI and upskilling? Speak with an expert for a free 1:1 counselling session today.
The accuracy formula in machine learning is: Accuracy = (TP + TN) / (TP + TN + FP + FN). It measures the percentage of predictions your model got correct out of all predictions made. It is the simplest and most widely used evaluation metric for classification models.
The formula for accuracy in machine learning measures overall correctness across all predictions. Precision specifically measures how many of the model's positive predictions were actually correct. Accuracy looks at the big picture while precision zooms into positive predictions only.
An accuracy score of 0.85 means the model correctly predicted 85% of all test cases. The accuracy score formula in machine learning returns values between 0 and 1, where 1 means perfect predictions. Multiply by 100 to convert to a percentage.
You should not rely on accuracy when your dataset is imbalanced, meaning one class has far more samples than others. In such cases, a model can achieve high accuracy by always predicting the majority class while completely ignoring the minority class. Use precision, recall, or F1 score instead.
Accuracy = (TP + TN) / (TP + TN + FP + FN) and Precision = TP / (TP + FP). Use accuracy when your classes are balanced and all errors are equally costly. Use precision when false positives are specifically harmful, like incorrectly flagging healthy patients as sick.
Yes. This is called the accuracy paradox. If 95% of your data belongs to one class, a model that always predicts that class achieves 95% accuracy without learning anything useful. This is why the formula for accuracy in machine learning must always be interpreted alongside your dataset's class distribution.
You can use accuracy_score(y_true, y_pred) from sklearn.metrics. Pass your actual labels and predicted labels as arguments. The function returns a value between 0 and 1 representing the accuracy score formula in machine learning result for your model.
There is no universal benchmark. A 90% accuracy score can be excellent for one problem and completely inadequate for another. Context matters. For medical diagnosis or fraud detection, even 95% accuracy may not be acceptable if the recall for the positive class is very low.
No. The standard accuracy score formula in machine learning applies only to classification tasks where predictions fall into discrete categories. For regression problems, where outputs are continuous numbers, you use metrics like Mean Absolute Error, Mean Squared Error, or R-squared.
Training accuracy measures how well your model performs on data it was trained on. Test accuracy measures performance on unseen data. A large gap between the two suggests overfitting. Always use test accuracy as the true indicator of model performance and avoid evaluating on training data alone.
Both metrics are derived from the confusion matrix. The confusion matrix breaks predictions into four categories: TP, TN, FP, and FN. Accuracy uses all four values to measure overall correctness. Precision uses only TP and FP to measure the quality of positive predictions. The confusion matrix is the foundation for nearly all classification metrics.
57 articles published
Rahul Singh is an Associate Content Writer at upGrad, with a strong interest in Data Science, Machine Learning, and Artificial Intelligence. He combines technical development skills with data-driven s...
India’s #1 Tech University
Executive Program in Generative AI for Leaders
76%
seats filled