Advertisement
mayankjoin3

41 metrics regression

Nov 8th, 2024 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.15 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. import os
  4. from sklearn.metrics import (
  5.     mean_absolute_error, mean_squared_error, mean_absolute_percentage_error,
  6.     median_absolute_error, explained_variance_score, r2_score, max_error,
  7.     mean_squared_log_error
  8. )
  9. from scipy.stats import pearsonr, spearmanr
  10.  
  11. # Define the folder containing the CSV files
  12. input_folder = 'output'
  13.  
  14. # Function to compute each metric and handle errors
  15. def compute_metrics(actual, predicted):
  16.     metrics = {}
  17.     try: metrics['Adjusted R-squared'] = round(1 - (1 - r2_score(actual, predicted)) * ((len(actual) - 1) / (len(actual) - len(predicted) - 1)), 3)
  18.     except: metrics['Adjusted R-squared'] = -1
  19.     try: metrics['Coefficient of Determination (R-squared)'] = round(r2_score(actual, predicted), 3)
  20.     except: metrics['Coefficient of Determination (R-squared)'] = -1
  21.     try: metrics['Pearson Correlation Coefficient'] = round(pearsonr(actual, predicted)[0], 3)
  22.     except: metrics['Pearson Correlation Coefficient'] = -1
  23.     try: metrics['Spearman\'s Rank Correlation'] = round(spearmanr(actual, predicted)[0], 3)
  24.     except: metrics['Spearman\'s Rank Correlation'] = -1
  25.     try: metrics['Explained Variance'] = round(explained_variance_score(actual, predicted), 3)
  26.     except: metrics['Explained Variance'] = -1
  27.     try: metrics['Mean Absolute Error (MAE)'] = round(mean_absolute_error(actual, predicted), 3)
  28.     except: metrics['Mean Absolute Error (MAE)'] = -1
  29.     try: metrics['Mean Absolute Percentage Error (MAPE)'] = round(mean_absolute_percentage_error(actual, predicted), 3)
  30.     except: metrics['Mean Absolute Percentage Error (MAPE)'] = -1
  31.     try: metrics['Mean Squared Error (MSE)'] = round(mean_squared_error(actual, predicted), 3)
  32.     except: metrics['Mean Squared Error (MSE)'] = -1
  33.     try: metrics['Root Mean Squared Error (RMSE)'] = round(np.sqrt(mean_squared_error(actual, predicted)), 3)
  34.     except: metrics['Root Mean Squared Error (RMSE)'] = -1
  35.     try: metrics['Median Absolute Error (MedAE)'] = round(median_absolute_error(actual, predicted), 3)
  36.     except: metrics['Median Absolute Error (MedAE)'] = -1
  37.     try: metrics['Normalized Root Mean Squared Error (NRMSE)'] = round(np.sqrt(mean_squared_error(actual, predicted)) / (np.max(actual) - np.min(actual)), 3)
  38.     except: metrics['Normalized Root Mean Squared Error (NRMSE)'] = -1
  39.     try: metrics['Mean Absolute Deviation (MAD)'] = round(np.mean(np.abs(actual - np.mean(actual))), 3)
  40.     except: metrics['Mean Absolute Deviation (MAD)'] = -1
  41.     try: metrics['Max Error'] = round(max_error(actual, predicted), 3)
  42.     except: metrics['Max Error'] = -1
  43.     try: metrics['Huber Loss'] = round(np.mean(np.where(np.abs(actual - predicted) < 1, 0.5 * (actual - predicted) ** 2, np.abs(actual - predicted) - 0.5)), 3)
  44.     except: metrics['Huber Loss'] = -1
  45.     try: metrics['Log-Cosh Loss'] = round(np.mean(np.log(np.cosh(predicted - actual))), 3)
  46.     except: metrics['Log-Cosh Loss'] = -1
  47.     try: metrics['Mean Squared Log Error (MSLE)'] = round(mean_squared_log_error(actual, predicted), 3)
  48.     except: metrics['Mean Squared Log Error (MSLE)'] = -1
  49.     try: metrics['Symmetric Mean Absolute Percentage Error (sMAPE)'] = round(100 * np.mean(2 * np.abs(predicted - actual) / (np.abs(actual) + np.abs(predicted))), 3)
  50.     except: metrics['Symmetric Mean Absolute Percentage Error (sMAPE)'] = -1
  51.     try: metrics['Akaike Information Criterion (AIC)'] = round(2 * len(predicted) - 2 * np.log(mean_squared_error(actual, predicted)), 3)
  52.     except: metrics['Akaike Information Criterion (AIC)'] = -1
  53.     try: metrics['Bayesian Information Criterion (BIC)'] = round(len(predicted) * np.log(len(actual)) + np.log(mean_squared_error(actual, predicted)), 3)
  54.     except: metrics['Bayesian Information Criterion (BIC)'] = -1
  55.     try: metrics['Coefficient of Variation (CV)'] = round(np.std(predicted) / np.mean(predicted), 3)
  56.     except: metrics['Coefficient of Variation (CV)'] = -1
  57.     try: metrics['Fraction of Variance Explained (FVE)'] = round(explained_variance_score(actual, predicted), 3)
  58.     except: metrics['Fraction of Variance Explained (FVE)'] = -1
  59.     return metrics
  60.  
  61. # Initialize an empty list to collect all results
  62. all_results = []
  63.  
  64. # Iterate over all CSV files in the input folder
  65. for filename in os.listdir(input_folder):
  66.     if filename.endswith('.csv'):
  67.         file_path = os.path.join(input_folder, filename)
  68.         data = pd.read_csv(file_path)
  69.        
  70.         # Extract actual and predicted values
  71.         actual = data['actual'].values
  72.         predicted = data['predicted'].values
  73.  
  74.         # Compute metrics
  75.         metrics = compute_metrics(actual, predicted)
  76.         metrics['Filename'] = filename  # Add filename to identify each set of metrics
  77.  
  78.         # Append the metrics to all_results list
  79.         all_results.append(metrics)
  80.  
  81. # Convert results to a DataFrame
  82. output_df = pd.DataFrame(all_results)
  83.  
  84. # Reorder columns to have 'Filename' as the first column
  85. cols = ['Filename'] + [col for col in output_df.columns if col != 'Filename']
  86. output_df = output_df[cols]
  87.  
  88. # Save the consolidated metrics to a CSV file
  89. output_df.to_csv('metrics.csv', mode='a', index=False, header=not os.path.exists('metrics.csv'))
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement