Table of Contents

    Accuracy

    MACHINE LEARNING

    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.

    In simple words — Accuracy tells us how often the model got the prediction right.

    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.
    Accuracy works best when your dataset has nearly equal classes.

    Mathematical Formula

    ACCURACY FORMULA
    $$ Accuracy = \frac{\text{Correct Predictions}}{\text{Total Predictions}} \times 100 $$

    Using the Confusion Matrix terminology:

    USING CONFUSION MATRIX
    $$ Accuracy = \frac{TP + TN}{TP + TN + FP + FN} $$

    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
    CALCULATION
    $$ Accuracy = \frac{90}{100} \times 100 = 90\% $$
    Insight The model correctly predicted 90 out of 100 emails — an accuracy of 90%.

    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

    Prerequisites: Python 3.x, scikit-learn.
    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}%")
    Output The model's accuracy is calculated directly from actual and predicted values.

    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}%")
    Output Random Forest typically achieves over 95% accuracy on the Iris dataset.

    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.
    Limitation Accuracy can hide poor performance on minority classes.
    Better Metrics Use Precision, Recall, F1-Score, ROC-AUC for imbalanced datasets.

    Accuracy vs Other Metrics

    Metric Best For Weakness
    AccuracyBalanced dataFails on imbalanced data
    PrecisionWhen false positives matterIgnores false negatives
    RecallWhen false negatives matterIgnores false positives
    F1-ScoreBalance precision & recallSlightly complex
    ROC-AUCOverall classifier qualityLess 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

    Limitation 1 Misleading for imbalanced datasets.
    Limitation 2 Doesn't show type of errors (FP or FN).
    Limitation 3 Doesn't reflect business impact.
    Limitation 4 Cannot evaluate probabilistic predictions.

    Common Mistakes to Avoid

    Mistake 1 Using only accuracy on imbalanced datasets.
    Mistake 2 Comparing models without checking confusion matrix.
    Mistake 3 Ignoring business cost of FP and FN.
    Mistake 4 Assuming higher accuracy = better model.

    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

    REMEMBER
    Right Predictions ÷ Total Predictions = Accuracy

    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.