Accuracy Formula in Machine Learning

By Rahul Singh

Updated on Jun 10, 2026 | 8 min read | 2.91K+ views

Share:

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. 

What Is the Accuracy Formula in Machine Learning?

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:

  • TP (True Positive): The model predicted "yes" and the actual answer was "yes"
  • TN (True Negative): The model predicted "no" and the actual answer was "no"
  • FP (False Positive): The model predicted "yes" but the actual answer was "no"
  • FN (False Negative): The model predicted "no" but the actual answer was "yes"

These four values come from what is called a confusion matrix, which is a table that summarises all prediction outcomes for a classification model.

A Quick Example

Suppose your model evaluates 100 patient records to predict whether someone has a disease.

  • 40 patients actually have the disease, and the model correctly identifies 35 of them (TP = 35)
  • 60 patients do not have the disease, and the model correctly identifies 55 of them (TN = 55)
  • The model wrongly flags 5 healthy patients as sick (FP = 5)
  • The model misses 5 actual patients (FN = 5)

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

Accuracy as a Percentage vs. a Score

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%.

How to Calculate Accuracy in Machine Learning: Step by Step

Knowing the formula is one thing. Applying it correctly is another. Here is a clean step-by-step process.

Step 1: Run Your Model on Test Data

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.

Step 2: Collect Predictions and Actual Labels

You need two lists:

  • The actual labels (what the correct answers are)
  • The predicted labels (what your model guessed)

Step 3: Build a Confusion Matrix

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) 

Step 4: Apply the Accuracy Score Formula

Use the accuracy score formula in machine learning:

Accuracy = (TP + TN) / (TP + TN + FP + FN)

Step 5: Calculate It in Python (Optional)

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

When Accuracy Works Well and When It Misleads You

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.

When Accuracy Is a Reliable Metric

Accuracy works well when:

  • Your dataset is balanced, meaning roughly equal numbers of each class
  • All types of errors carry the same cost
  • You need a quick, general sense of model performance
  • You are comparing two models on the same dataset

Also Read: 25 Must-Try Machine Learning Projects in Python for Beginners and Experts in 2026

Examples where accuracy is appropriate:

  • Predicting whether a handwritten digit is 0 through 9 (multi-class, balanced)
  • Classifying product categories in an e-commerce catalogue
  • Identifying spam vs. not spam in a reasonably balanced inbox

When Accuracy Can Mislead You

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:

  • Medical diagnosis (rare diseases, where most samples are negative)
  • Fraud detection
  • Anomaly detection in manufacturing
  • Credit default prediction

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

Accuracy and Precision Formula in Machine Learning: Understanding the Difference

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.

Accuracy vs. Precision vs. Recall

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

When to Prioritise Precision

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.

When to Prioritise Recall

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

Putting It Together with an Example

Say a model predicts whether a loan applicant will default.

  • TP = 80 (correctly predicted defaults)
  • TN = 870 (correctly predicted non-defaults)
  • FP = 30 (flagged as default, but were fine)
  • FN = 20 (missed actual defaults)

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

Accuracy Formula Across Different Types of ML Problems

The accuracy score formula in machine learning applies most cleanly to classification tasks. Here is how it fits across different problem types.

1. Binary Classification

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.

2. Multi-Class Classification

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

3. Multi-Label Classification

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."

4. Regression Problems

Accuracy in its standard form does not apply to regression, where the output is a continuous number. Instead, you use metrics like:

  • Mean Absolute Error (MAE)
  • Mean Squared Error (MSE)
  • R-squared (R²)

Accuracy is specifically a classification metric.

Tips to Improve Model Accuracy in Machine Learning

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:

  • Remove duplicates and handle missing values
  • Fix incorrect labels in your training data
  • Collect more data if possible, especially for underrepresented classes

Feature engineering:

  • Create new features that better capture patterns
  • Drop irrelevant or redundant features
  • Normalise or scale numerical features

Also Read: Top 30+ Linear Regression Projects

Model selection and tuning:

  • Try multiple algorithms before settling on one
  • Use cross-validation instead of a single train-test split
  • Tune hyperparameters using Grid Search or Random Search

Handle class imbalance:

  • Use oversampling (SMOTE) or undersampling techniques
  • Adjust class weights in your model
  • Use stratified sampling when splitting data

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

Conclusion

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.      

Frequently Asked Question (FAQs)

1. What is the accuracy formula in machine learning?

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.

2. How is the formula for accuracy in machine learning different from precision?

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.

3. What does an accuracy score of 0.85 mean in machine learning?

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.

4. When should I not rely on accuracy in machine learning?

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.

5. What is the accuracy and precision formula in machine learning and when do you use each?

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.

6. Can accuracy be high even if a model is bad?

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.

7. How do I calculate accuracy in Python using Scikit-learn?

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.

8. What is a good accuracy score for a machine learning 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.

9. Is accuracy used for regression models in machine learning?

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.

10. What is the difference between training accuracy and test accuracy?

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.

11. How does the accuracy and precision formula in machine learning relate to the confusion matrix?

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.

Rahul Singh

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

View Program