⏱️ 45 min

Model Evaluation Metrics

Learn to assess ML model performance

Classification Metrics

Understand key metrics for classification:

python
import numpy as np

def compute_metrics(y_true, y_pred, y_proba=None):
    # Confusion Matrix
    tp = np.sum((y_true == 1) & (y_pred == 1))
    tn = np.sum((y_true == 0) & (y_pred == 0))
    fp = np.sum((y_true == 0) & (y_pred == 1))
    fn = np.sum((y_true == 1) & (y_pred == 0))
    
    # Accuracy
    accuracy = (tp + tn) / (tp + tn + fp + fn)
    
    # Precision
    precision = tp / (tp + fp) if (tp + fp) > 0 else 0
    
    # Recall
    recall = tp / (tp + fn) if (tp + fn) > 0 else 0
    
    # F1 Score
    f1 = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0
    
    metrics = {
        'Confusion Matrix': [[tn, fp], [fn, tp]],
        'Accuracy': accuracy,
        'Precision': precision,
        'Recall': recall,
        'F1 Score': f1
    }
    
    return metrics

# Example
y_true = np.array([1, 0, 1, 1, 0, 1, 0, 0, 1, 1])
y_pred = np.array([1, 0, 1, 0, 0, 1, 0, 1, 1, 1])

metrics = compute_metrics(y_true, y_pred)
print("Classification Metrics:")
print(f"Confusion Matrix:")
print(f"  [[TN={metrics['Confusion Matrix'][0][0]}, FP={metrics['Confusion Matrix'][0][1]}],")
print(f"   [FN={metrics['Confusion Matrix'][1][0]}, TP={metrics['Confusion Matrix'][1][1]}]]")
print(f"\nAccuracy: {metrics['Accuracy']:.3f}")
print(f"Precision: {metrics['Precision']:.3f}")
print(f"Recall: {metrics['Recall']:.3f}")
print(f"F1 Score: {metrics['F1 Score']:.3f}")
Output:
Classification Metrics:
Confusion Matrix:
  [[TN=4, FP=1],
   [FN=1, TP=4]]

Accuracy: 0.800
Precision: 0.800
Recall: 0.800
F1 Score: 0.800

Regression Metrics

Evaluate regression models:

python
def regression_metrics(y_true, y_pred):
    n = len(y_true)
    
    # Mean Absolute Error
    mae = np.mean(np.abs(y_true - y_pred))
    
    # Mean Squared Error
    mse = np.mean((y_true - y_pred) ** 2)
    
    # Root Mean Squared Error
    rmse = np.sqrt(mse)
    
    # R² Score
    ss_res = np.sum((y_true - y_pred) ** 2)
    ss_tot = np.sum((y_true - np.mean(y_true)) ** 2)
    r2 = 1 - (ss_res / ss_tot)
    
    return {
        'MAE': mae,
        'MSE': mse,
        'RMSE': rmse,
        'R²': r2
    }

# Example
y_true = np.array([3, -0.5, 2, 7, 4.2])
y_pred = np.array([2.5, 0.0, 2, 8, 4.5])

metrics = regression_metrics(y_true, y_pred)
print("Regression Metrics:")
for name, value in metrics.items():
    print(f"{name}: {value:.4f}")
Output:
Regression Metrics:
MAE: 0.4200
MSE: 0.2950
RMSE: 0.5432
R²: 0.9486
Sharan Initiatives - Making a Difference Together