Accuracy
Accuracy
The simplest and most popular metric to measure how often a Machine Learning model predicts correctly.
What is Accuracy?
Accuracy is one of the most commonly used classification evaluation metrics. It measures the percentage of correct predictions made by the model out of the total predictions.
Why is Accuracy Important?
- Provides a quick overview of model performance.
- Easy to calculate and interpret.
- Useful for balanced classification problems.
- Helps compare different ML models.
- Often used as a baseline metric.
Mathematical Formula
Using the Confusion Matrix terminology:
Where:
- TP = True Positives (correctly predicted positives)
- TN = True Negatives (correctly predicted negatives)
- FP = False Positives (wrongly predicted as positives)
- FN = False Negatives (wrongly predicted as negatives)
Simple Example
Suppose a spam detection model is tested on 100 emails:
- Correctly classified emails = 90
- Wrong predictions = 10
Visual Intuition
| Predicted Positive | Predicted Negative | |
|---|---|---|
| Actual Positive | TP (Correct) | FN (Wrong) |
| Actual Negative | FP (Wrong) | TN (Correct) |
Accuracy adds the green cells (TP + TN) and divides by the total.
Python Example — Calculating Accuracy
pip install scikit-learn
from sklearn.metrics import accuracy_score
# Actual labels
y_true = [1, 0, 1, 1, 0, 1, 0, 0, 1, 1]
# Predicted labels
y_pred = [1, 0, 1, 0, 0, 1, 0, 1, 1, 1]
# Calculate accuracy
accuracy = accuracy_score(y_true, y_pred)
print(f"Accuracy: {accuracy * 100:.2f}%")
Full Example — Using ML Model
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# Step 1: Load dataset
data = load_iris()
X, y = data.data, data.target
# Step 2: Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Step 3: Train model
model = RandomForestClassifier()
model.fit(X_train, y_train)
# Step 4: Predict
y_pred = model.predict(X_test)
# Step 5: Calculate accuracy
print(f"Accuracy: {accuracy_score(y_test, y_pred) * 100:.2f}%")
Where Accuracy is Used
Spam Detection
- Classify spam vs ham emails
- Performance check
Image Classification
- Cat vs Dog
- Object detection accuracy
Text Classification
- Sentiment analysis
- News categorization
Healthcare
- Disease prediction
- Diagnosis support
Banking
- Loan approval
- Customer classification
E-Commerce
- Product recommendation
- Customer segmentation
When Accuracy Can Be Misleading
Accuracy fails when the dataset is imbalanced.
Example: Suppose 95% of emails are not spam, and the model predicts everything as "not spam".
- Accuracy = 95% (looks great!)
- But it never detects spam at all.
Accuracy vs Other Metrics
| Metric | Best For | Weakness |
|---|---|---|
| Accuracy | Balanced data | Fails on imbalanced data |
| Precision | When false positives matter | Ignores false negatives |
| Recall | When false negatives matter | Ignores false positives |
| F1-Score | Balance precision & recall | Slightly complex |
| ROC-AUC | Overall classifier quality | Less intuitive |
Real-Life Analogy
Accuracy = Exam Score
If a student answers 80 out of 100 questions correctly, their accuracy is 80%. ML models are scored the same way — but accuracy alone doesn't show how well they understood the harder topics (minority classes).
Advantages of Accuracy
- Easy to understand and explain.
- Simple to calculate.
- Suitable for balanced datasets.
- Provides a quick model comparison.
- Widely supported by all ML libraries.
Disadvantages
Common Mistakes to Avoid
Best Practices
Quick Tips
- Combine accuracy with Precision, Recall, F1.
- Always check confusion matrix.
- Don't rely on accuracy alone for medical/financial models.
- Use cross-validation for stable evaluation.
- Test on unseen data.
- Analyze errors before deployment.
Importance of Accuracy
Easy Insight
- Quick performance check
- First metric to evaluate
Model Comparison
- Helps benchmark models
- Quick decisions
Foundation Metric
- Base of many other metrics
- Easy to interpret
Business Communication
- Easy to share with stakeholders
- Used in reports & dashboards
Golden Rule
Key Takeaway
Accuracy is the simplest and most widely used metric to evaluate Machine Learning models. While it works great for balanced datasets, it can be misleading when the data is imbalanced. Always pair it with metrics like Precision, Recall, F1-Score, and ROC-AUC to get a complete picture of your model's performance.