Python Metric Calculation
Setting Up the Environment
Prerequisites
sudo apt update
sudo apt install python3-pip
pip3 install numpy scikit-learn pandas matplotlib
Comprehensive Metric Calculation Strategies
Importing Essential Libraries
import numpy as np
import pandas as pd
from sklearn.metrics import (
mean_squared_error,
mean_absolute_error,
r2_score
)
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
Metric Calculation Workflow
graph TD
A[Raw Data] --> B[Data Preparation]
B --> C[Model Training]
C --> D[Predictions]
D --> E[Metric Calculation]
E --> F[Model Evaluation]
Core Metric Calculation Functions
Custom Metric Calculation Class
class RegressionMetrics:
def __init__(self, y_true, y_pred):
self.y_true = y_true
self.y_pred = y_pred
def mean_squared_error(self):
return np.mean((self.y_true - self.y_pred)**2)
def mean_absolute_error(self):
return np.mean(np.abs(self.y_true - self.y_pred))
def r_squared(self):
ss_res = np.sum((self.y_true - self.y_pred)**2)
ss_tot = np.sum((self.y_true - np.mean(self.y_true))**2)
return 1 - (ss_res / ss_tot)
Practical Regression Metrics Example
## Sample Regression Scenario
def regression_metrics_demo():
## Generate synthetic data
np.random.seed(42)
X = np.random.rand(100, 1)
y = 2 + 3 * X + np.random.normal(0, 0.1, (100, 1))
## Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
## Train model
model = LinearRegression()
model.fit(X_train, y_train)
## Predictions
y_pred = model.predict(X_test)
## Metric Calculation
metrics = RegressionMetrics(y_test, y_pred)
## Results
results = {
'MSE': metrics.mean_squared_error(),
'MAE': metrics.mean_absolute_error(),
'R-Squared': metrics.r_squared()
}
return results
## Execute and print results
print(regression_metrics_demo())
Metric Comparison Table
Metric |
Calculation Method |
Interpretation |
MSE |
Mean of Squared Errors |
Lower is Better |
MAE |
Mean of Absolute Errors |
Lower is Better |
R-Squared |
Variance Explained |
Closer to 1 is Better |
Advanced Metric Considerations for LabEx Users
Best Practices
- Use multiple metrics for comprehensive evaluation
- Consider domain-specific requirements
- Validate metrics across different datasets
- Vectorize calculations
- Use built-in NumPy/Scikit-learn functions
- Minimize computational complexity
Error Handling and Robustness
def safe_metric_calculation(y_true, y_pred):
try:
metrics = RegressionMetrics(y_true, y_pred)
return {
'MSE': metrics.mean_squared_error(),
'MAE': metrics.mean_absolute_error(),
'R-Squared': metrics.r_squared()
}
except Exception as e:
print(f"Metric calculation error: {e}")
return None