0
0
ML Pythonml~8 mins

Random forest in depth in ML Python - Model Metrics & Evaluation

Choose your learning style9 modes available
Metrics & Evaluation - Random forest in depth
Which metric matters for Random Forest and WHY

Random forest is often used for classification and regression. For classification, accuracy, precision, recall, and F1 score are important. Accuracy shows overall correctness. Precision tells how many predicted positives are truly positive. Recall shows how many real positives were found. F1 balances precision and recall. For regression, mean squared error (MSE) or R-squared are used to measure prediction quality.

We choose metrics based on the problem. For example, if missing a positive case is costly, recall matters more. Random forest can handle imbalanced data well, but metrics guide us to tune it properly.

Confusion Matrix for Random Forest Classification
      | Predicted Positive | Predicted Negative |
      |--------------------|--------------------|
      | True Positive (TP)  | False Negative (FN) |
      | False Positive (FP) | True Negative (TN)  |

    Example:
    TP = 85, FP = 15, TN = 90, FN = 10
    Total samples = 85 + 15 + 90 + 10 = 200

    Precision = TP / (TP + FP) = 85 / (85 + 15) = 0.85
    Recall = TP / (TP + FN) = 85 / (85 + 10) = 0.8947
    F1 Score = 2 * (Precision * Recall) / (Precision + Recall) ≈ 0.871
    Accuracy = (TP + TN) / Total = (85 + 90) / 200 = 0.875
    
Precision vs Recall Tradeoff in Random Forest

Random forest can be tuned to favor precision or recall by adjusting the decision threshold or class weights.

Example 1: Spam Email Filter
High precision is important. We want to avoid marking good emails as spam (false positives). So, we tune random forest to reduce false positives, even if some spam emails slip through (lower recall).

Example 2: Disease Detection
High recall is critical. We want to catch as many sick patients as possible, even if some healthy people are flagged (false positives). So, random forest is tuned to reduce false negatives.

This tradeoff depends on the cost of errors in the real world.

Good vs Bad Metric Values for Random Forest

Good values:

  • Accuracy above 85% on balanced data
  • Precision and recall both above 80%, showing balanced performance
  • F1 score close to precision and recall, indicating no extreme tradeoff
  • For regression, low MSE and R-squared near 1

Bad values:

  • High accuracy but very low recall (e.g., recall 20%) means many positives missed
  • High recall but very low precision means many false alarms
  • F1 score much lower than precision or recall shows imbalance
  • For regression, very high MSE or negative R-squared means poor fit
Common Pitfalls in Random Forest Metrics
  • Accuracy paradox: High accuracy on imbalanced data can be misleading. For example, if 95% of data is negative, predicting all negative gives 95% accuracy but zero recall for positives.
  • Data leakage: If test data leaks into training, metrics look unrealistically good.
  • Overfitting: Very high training accuracy but low test accuracy means the model memorizes training data and fails to generalize.
  • Ignoring class imbalance: Not adjusting for imbalance can cause poor recall or precision on minority class.
Self Check

Your random forest model has 98% accuracy but only 12% recall on fraud cases. Is it good for production? Why or why not?

Answer: No, it is not good. Although accuracy is high, the model misses 88% of fraud cases (low recall). This means many frauds go undetected, which is costly. For fraud detection, recall is more important to catch as many frauds as possible.

Key Result
Random forest evaluation focuses on balanced precision and recall to ensure reliable predictions, especially on imbalanced data.