Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import numpy as np
- import os
- from sklearn.metrics import (
- mean_absolute_error, mean_squared_error, mean_absolute_percentage_error,
- median_absolute_error, explained_variance_score, r2_score, max_error,
- mean_squared_log_error
- )
- from scipy.stats import pearsonr, spearmanr
- # Define the folder containing the CSV files
- input_folder = 'output'
- # Function to compute each metric and handle errors
- def compute_metrics(actual, predicted):
- metrics = {}
- try: metrics['Adjusted R-squared'] = round(1 - (1 - r2_score(actual, predicted)) * ((len(actual) - 1) / (len(actual) - len(predicted) - 1)), 3)
- except: metrics['Adjusted R-squared'] = -1
- try: metrics['Coefficient of Determination (R-squared)'] = round(r2_score(actual, predicted), 3)
- except: metrics['Coefficient of Determination (R-squared)'] = -1
- try: metrics['Pearson Correlation Coefficient'] = round(pearsonr(actual, predicted)[0], 3)
- except: metrics['Pearson Correlation Coefficient'] = -1
- try: metrics['Spearman\'s Rank Correlation'] = round(spearmanr(actual, predicted)[0], 3)
- except: metrics['Spearman\'s Rank Correlation'] = -1
- try: metrics['Explained Variance'] = round(explained_variance_score(actual, predicted), 3)
- except: metrics['Explained Variance'] = -1
- try: metrics['Mean Absolute Error (MAE)'] = round(mean_absolute_error(actual, predicted), 3)
- except: metrics['Mean Absolute Error (MAE)'] = -1
- try: metrics['Mean Absolute Percentage Error (MAPE)'] = round(mean_absolute_percentage_error(actual, predicted), 3)
- except: metrics['Mean Absolute Percentage Error (MAPE)'] = -1
- try: metrics['Mean Squared Error (MSE)'] = round(mean_squared_error(actual, predicted), 3)
- except: metrics['Mean Squared Error (MSE)'] = -1
- try: metrics['Root Mean Squared Error (RMSE)'] = round(np.sqrt(mean_squared_error(actual, predicted)), 3)
- except: metrics['Root Mean Squared Error (RMSE)'] = -1
- try: metrics['Median Absolute Error (MedAE)'] = round(median_absolute_error(actual, predicted), 3)
- except: metrics['Median Absolute Error (MedAE)'] = -1
- try: metrics['Normalized Root Mean Squared Error (NRMSE)'] = round(np.sqrt(mean_squared_error(actual, predicted)) / (np.max(actual) - np.min(actual)), 3)
- except: metrics['Normalized Root Mean Squared Error (NRMSE)'] = -1
- try: metrics['Mean Absolute Deviation (MAD)'] = round(np.mean(np.abs(actual - np.mean(actual))), 3)
- except: metrics['Mean Absolute Deviation (MAD)'] = -1
- try: metrics['Max Error'] = round(max_error(actual, predicted), 3)
- except: metrics['Max Error'] = -1
- 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)
- except: metrics['Huber Loss'] = -1
- try: metrics['Log-Cosh Loss'] = round(np.mean(np.log(np.cosh(predicted - actual))), 3)
- except: metrics['Log-Cosh Loss'] = -1
- try: metrics['Mean Squared Log Error (MSLE)'] = round(mean_squared_log_error(actual, predicted), 3)
- except: metrics['Mean Squared Log Error (MSLE)'] = -1
- try: metrics['Symmetric Mean Absolute Percentage Error (sMAPE)'] = round(100 * np.mean(2 * np.abs(predicted - actual) / (np.abs(actual) + np.abs(predicted))), 3)
- except: metrics['Symmetric Mean Absolute Percentage Error (sMAPE)'] = -1
- try: metrics['Akaike Information Criterion (AIC)'] = round(2 * len(predicted) - 2 * np.log(mean_squared_error(actual, predicted)), 3)
- except: metrics['Akaike Information Criterion (AIC)'] = -1
- try: metrics['Bayesian Information Criterion (BIC)'] = round(len(predicted) * np.log(len(actual)) + np.log(mean_squared_error(actual, predicted)), 3)
- except: metrics['Bayesian Information Criterion (BIC)'] = -1
- try: metrics['Coefficient of Variation (CV)'] = round(np.std(predicted) / np.mean(predicted), 3)
- except: metrics['Coefficient of Variation (CV)'] = -1
- try: metrics['Fraction of Variance Explained (FVE)'] = round(explained_variance_score(actual, predicted), 3)
- except: metrics['Fraction of Variance Explained (FVE)'] = -1
- return metrics
- # Initialize an empty list to collect all results
- all_results = []
- # Iterate over all CSV files in the input folder
- for filename in os.listdir(input_folder):
- if filename.endswith('.csv'):
- file_path = os.path.join(input_folder, filename)
- data = pd.read_csv(file_path)
- # Extract actual and predicted values
- actual = data['actual'].values
- predicted = data['predicted'].values
- # Compute metrics
- metrics = compute_metrics(actual, predicted)
- metrics['Filename'] = filename # Add filename to identify each set of metrics
- # Append the metrics to all_results list
- all_results.append(metrics)
- # Convert results to a DataFrame
- output_df = pd.DataFrame(all_results)
- # Reorder columns to have 'Filename' as the first column
- cols = ['Filename'] + [col for col in output_df.columns if col != 'Filename']
- output_df = output_df[cols]
- # Save the consolidated metrics to a CSV file
- output_df.to_csv('metrics.csv', mode='a', index=False, header=not os.path.exists('metrics.csv'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement